專案結構常見為:
/ProjectRoot/
|-- servlet/
| |-- Servlet1
| |-- Servlet2
|-- myJsp1.jsp
|-- myJsp2.jsp
response.sendRedirect(String targetUrl);
setAttribute() 傳遞任意物件。
RequestDispatcher rd = request.getRequestDispatcher(String targetUrl);
rd.forward(request, response);
在 JSP 中測試各路徑 API:
<%= "request.getContextPath() = " + request.getContextPath() + "<BR />" %>
<%= "request.getServletPath() = " + request.getServletPath() + "<BR />" %>
<%= "request.getRequestURI() = " + request.getRequestURI() + "<BR />" %>
<%= "request.getRequestURL() = " + request.getRequestURL() + "<BR />" %>
<% String realPath = session.getServletContext().getRealPath("/"); %>
<%= "getRealPath(\"/\") = " + realPath %>
根據專案結構選擇正確相對/絕對路徑,避免資源載入失敗(如 JS、CSS 找不到)。