Summary of path in Servlet&JSP

Keywords: Java

Continuous learning & continuous updating

Learning attitude: keep breaking away


There are many places in Servlet and JSP that need to use the path. Many beginners don't know it very well. In order to facilitate themselves and others, write an article to discuss it and attract jade.

This article will be continuously updated and improved. If you find anything wrong with the author, you are welcome to comment and comment.

Path in Servlet

There are two places where paths are used in servlets:

  1. forward
  2. redirect

Let's look at it one by one.

1, Redirect

Redirection can be redirected to other people's websites or, of course, to your own projects.

Redirect to your own project

There are generally two ways to write:

  1. Use context_path

/lp_resume / is context path

Since the context path may be modified, you can also use the dynamic path writing method:

response.sendRedirect(req.getContextPath() + "/website.html");
  1. Write full path
Redirect to someone else's website:

Generally, there is only one way to write full path:


2, Forward

Forwards can only be forwarded to their own items.

Keep in mind:

  • Point 1: http://host:port/context/ Corresponding webapp directory in the project
    in other words: http://localhost:8080/lp_resume / corresponding LP_ webapp directory under resume project (root directory)

  • Point 2: forwarding can only use relative paths
    When writing relative path, if it starts with "/", it refers to the root directory directly relative to "webapp".

Since forwarding can only use relative paths, what is a relative path? Who is the relative path?

Look down:

Let's draw a conclusion first: the relative path refers to the path in the browser address bar at this time

Let's verify it.

Create three new JSP files in the webapp directory. The content of each Servlet is its file name:

  • a.jsp
  • b/b.jsp
  • c/c/c.jsp

Create three more servlets:

  • AServlet
  • BServlet
  • CServlet


public class AServlet extends HttpServlet {



public class BServlet extends HttpServlet {



public class CServlet extends HttpServlet {


When the browser accesses AServlet, BServlet and CServlet, the path in the browser address bar:

  • Access AServlet: / / browser path: http://localhost:8080/context_path/a.jsp
  • Access BServlet: / / browser path: http://localhost:8080/context_path/b/b_servlet
  • Access CServlet: / / browser path: http://localhost:8080/context_path/c/c/c_servlet

In AServlet, you can forward successfully by using the following code:

		  // It is currently in the root directory, so you can directly access the files in the root directory
//        req.getRequestDispatcher("a.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("b/b.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("c/c/c.jsp").forward(req, resp); // T

However, to forward successfully, BServlet and CServlet must be written as follows:

        // It is currently in directory b under the root directory, so you need to jump out of directory b
//        req.getRequestDispatcher("../a.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("../b/b.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("../c/c/c.jsp").forward(req, resp); // T
        // It is currently in the "root directory / c/c /" directory, so you need to jump out of the / c/c directory
//        req.getRequestDispatcher("../../a.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("../../b/b.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("../../c/c/c.jsp").forward(req, resp); // T

Therefore, we now know that when forwarding, the relative path refers to the path in the browser address bar at this time.

It is worth mentioning that during forwarding, whether AServlet, BServlet or CServlet can be written as follows:

        // Relative path (with /) (/ represents webapp root directory)
//        req.getRequestDispatcher("/a.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("/b/b.jsp").forward(req, resp); // T
//        req.getRequestDispatcher("/c/c/c.jsp").forward(req, resp); // T

Because if it starts with "/", it refers to the root directory relative to "webapp".

Path in JSP

  • In Java Web development, URL s in jsp and html should use / context_ Absolute path starting with path

  • Since context path may change, dynamic context path should be used

  • The path in the directive uses a relative path

Assuming that a JSP file is in the common directory, there are two ways to write it:

Writing method 1:

<%@ include file="head.jsp" %>

Method 2:

<%@ include file="/common/head.jsp" %>

At the end of this article, thank you for your attention and support!

Posted by sullyman on Sun, 31 Oct 2021 06:14:48 -0700