Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Servlets and JSPs. A simple request?

Tags:

java

jsp

servlets

I have a number of Java Server Pages set up already and I would like to use a Controller/View system by adding a Process Servlet (which extends HttpServlet).

I just want to basically process the requested JSPs as normal after the ProcessServlet has added some attributes.

Say all my JSPs are in a directory called /content/ and my web.xml file has a rule to map /content/*.jsp to my ProcessServlet

I have not been able to find a way short of moving all of my JSPs into a different directory (/content-JSPs/) so that they can be dispatched to without having to go through the ProcessServlet endlessly.

Is there a way to basically dispatch#forward() (some other method?) to the requested JSP without having it go through the ProcessServlet again?

It's a bit hard to believe that this lack of flexibility exists. Why can't the Servlet just act as a pass through to the JSP?

My goal here is to set everything up so that the web server does not have to have a separate directory for all JSPs and another directory for everything else i.e. CSS, JavaScript and images. I would like to keep the directory structure (and URL structure) as it is.

like image 975
Matthew Avatar asked Dec 27 '22 11:12

Matthew


1 Answers

Put them in /WEB-INF folder. This also effectively hides JSPs away from direct access. You only need to change the RequestDispatcher#forward() call to include /WEB-INF in the path.

request.getRequestDispatcher("/WEB-INF/content" + request.getPathInfo()).forward(request, response);

Please note that the URL pattern of /content/*.jsp is syntactically invalid. It should be /content/*. Perhaps you have also really used that. To skip static resources like images/css/JS, you should just not put them in /content, but in for example /resources, /static, etc.

Related:

  • Design Patterns web based applications
  • Hidden features of JSP/Servlet
like image 160
BalusC Avatar answered Jan 09 '23 09:01

BalusC