一、关于 jsp 中的超链接路径问题我们假设你的项目路径也就是 web应用程序的根目录为 /webapp
代码语言:javascript复制代码语言:javascript复制上面两种写法是相同的,都是指向 webapp 应用程序下的 login.jsp 页面。
如果以"/"开头,代表相对于web服务器的根目录如果不以"/"开头,代表相对于webapp 应用程序的根目录我们知道,web服务器可以包含多个 web应用程序,而/代表服务器的根目录,也就是Tomcat 的根目录,加上webapp就是告诉它我要访问的是哪一个应用程序,如果不加就默认是当前的应用程序。
二、关于 jsp 中请求路径的问题一般我们会在 jsp 页面中放一个 form 表单,这样当我们启动项目的时候请求可以直接跳转到指定的请求路径上面去,这里的规则和超链接一样,只不过要重点注意 Servlet 的路径。
例如:
如果你的 jsp 页面直接在项目的根目录下的话,表单跳转如下:
代码语言:javascript复制
对用的 Servlet 接口:代码语言:javascript复制@WebServlet(urlPatterns = "/customer.do")
public class Servlet01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
Customer customer = new Customer(name, email, phone);
HttpSession session = request.getSession();
synchronized (session) {
session.setAttribute("customer", customer);
}
request.getRequestDispatcher("/demo/displayCustomer.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}但是如果你的项目结构是这样的:
也就是说 jsp 文件在项目的根目录下的一个包下。
代码语言:javascript复制
对应的 Servlet 接口:代码语言:javascript复制@WebServlet(urlPatterns = "/demo/customer.do")
public class Servlet01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String email = request.getParameter("email");
String phone = request.getParameter("phone");
Customer customer = new Customer(name, email, phone);
HttpSession session = request.getSession();
synchronized (session) {
session.setAttribute("customer", customer);
}
request.getRequestDispatcher("/demo/displayCustomer.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}三、总结需要填写路径的地方有四处: