Enable Private JSP from Servlet

I have my servlet that responds to all the "/" url-pattern. Inside I need to display html sometimes, so I would like to. Include a JSP page, but I would like .jsp to be inaccessible from the outside. Also, how can I pass a model object to it.

+7
java jsp servlets
source share
2 answers

I would like to include a JSP page, but I would like .jsp to be inaccessible from the outside.

Put it in the /WEB-INF folder. The client cannot access it, but RequestDispatcher can access it.

 request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response); 

Also, how can I pass a model object to it.

Set it as a request attribute.

 request.setAttribute("bean", bean); // It'll be available as ${bean} in JSP. 

See also:

  • Servlet tag details page (contains a Hello World example and useful links)
  • Hidden JSP / Servlet Functions
  • Web Application Template Design

However, keep in mind that mapping a servlet to / takes on the execution of the servletcontainer builtin DefaultServlet function to serve static content. You will have to process all static files such as JS / CSS / images. Consider choosing a more specific url-pattern as /pages/* or *.do for JSP views. Bring in the end Filter in front, as described in this answer .

+13
source share

This is easy:

  • Place the JSP file in the WEB-INF folder.
  • In your servlet, execute getServletContext().getRequestDispatcher("/WEB-INF/path/your.jsp").forward(request, response);
+2
source share

All Articles