hỏi về dispatcher trong servlet

hoangdepzai95

New Member
12/4/16
3
0
1
24
mình có 1 file jsp nằm trong WebContent/WEB-INF/views/show.jsp
bây giờ mình muốn dispatcher đến nó.
có 2 cách mình xem hướng dẫn
cách 1 : getServletContext().getRequestDispatcher("/WEB-INF/views/show.jsp")
cách 2 : request.getRequestDispatcher("/WEB-INF/views/show.jsp")

cái mình chưa hiểu ở đây là đường dẫn truyền vào . theo như mình đọc ở oracle API
thì cách 1 "The pathname must begin with a "/" and is interpreted as relative to the current context root. " tức là bắt đầu bằng / và là tương đối với context root ( là thư mực gốc của web app) nhưng nếu thế phải là WebContent//WEB-INF/views/homeView.jsp chứ nhỉ


tương tự với cách 2 "The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as relative to the current context root. " lẽ ra phải bắt đầu với webcontent chứ nhỉ
 
Sửa lần cuối:

Joe

Thành viên VIP
21/1/13
2,742
1,257
113
Very good that you read the API and think about what it says. Let me explain:
- An absolute path is a path that starts from the very beginning. Example: an absolute path always starts with the disk name (e.g. on WINDOWS C:\myApp\WebContent\WEB-INF\views\homeView.jsp) or on LINUX /usr/myApp/WebContent/WEB-INF/views/homeView.jsp).
- A relative path always refers to a KNOWN path set by your computer (run set on a CMD/WINDOWS or echo $PATH on LINUX) and when your PATH, for example, contains the part c:\myApp\WebContent or /usr/myApp/WebContent) then your relative path is "/WEB-INF/views/show.jsp". This writing style is valid also on Windows within JAVA. Outside JAVA it must be "\WEB-INF\views\show.jsp". If your PATH won't contains the path c:\myApp\WebContent but only c:\myApp\ then you have to include the /WebContent/
Is that clear so far?
 
Sửa lần cuối:

hoangdepzai95

New Member
12/4/16
3
0
1
24
Very good that you read the API and think about what it says. Let me explain:
- An absolute path is a path that starts from the very beginning. Example: an absolute path always starts with the disk name (e.g. on WINDOWS C:\myApp\WebContent\WEB-INF\views\homeView.jsp) or on LINUX /usr/myApp/WebContent/WEB-INF/views/homeView.jsp).
- A relative path always refers to a KNOWN path set by your computer (run set on a CMD/WINDOWS or echo $PATH on LINUX) and when your PATH, for example, contains the part c:\myApp\WebContent or /usr/myApp/WebContent) then your relative path is "/WEB-INF/views/show.jsp". This writing style is valid also on Windows within JAVA. Outside JAVA it must be "\WEB-INF\views\show.jsp". If your PATH won't contains the path c:\myApp\WebContent but only c:\myApp\ then you have to include the /WebContent/
Is that clear so far?
thanks Joe so much :D