Servlets and JSPs. A simple request?

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.

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 dden features of JSP/Servlet.

Yes, this would work too, unless you need to have direct access to some jsp page, in which case only move the jsp's that are referenced by your controller (ProcessServlet). – Crollster Nov 10 at 1:31 @Crollster: that's also clearly the OP's intent :) – BalusC Nov 10 at 1:32 Won't /content/* make all requests for resources pass through the Servlet? I have css and images under there as well that don't need any sort of pre-processing.

– Matt Nov 10 at 1:48 I know it might be a bit OCD but I would like to keep the directory structure if possible. I like that my URLs map directly to the actual server directory (well close enough anyway). – Matt Nov 10 at 1:51 Just put them in another folder?

Otherwise change URL pattern to *. Jsp if you need all JSPs to be preprocessed. Otherwise you really can't go around some conditional checks.

– BalusC Nov 10 at 1:51.

You can also use Sevlet Filter instead of Servet. This is a good option if your servlet only adds some parameters to request. And you don't have to manually dispatch your request to JSP.

– Matt Nov 10 at 1:59 It depends, as for MVC I would consider using Spring MVC – Igor Nikolaev Nov 10 at 2:06 @user1038812 According to the spec, filters aren't supposed to serve resources, just filter. This has caused some problems in the Struts 2 world. S point was that if all you're doing is putting stuff in session/etc. A filter is another option-but it shouldn't act as a controller.

– Dave Newton Nov 10 at 2:40 Nevertheless some new frameworks tend to use filters instead of servlets :) For example Apache Wicket, but it's a different story :) – Igor Nikolaev Nov 10 at 2:49 @Igor Nikolaev MVC was not considered when the app was first built so using Spring would require a re-architecture.It's not a bad idea though. – Matt Nov 10 at 3:06.

Why not, instead of mapping to *. Jsp, map to something like *. Page (or whatever term you like), and then your process servlet can do its processing, and replace the .

Page with . Jsp and instruct the RequestDispatcher to forward() to that page. As long as all links on the pages, that you wish to go through the ProcessServlet, use the .

Page name, then it will probably work.

Since the JSPs already exist, there are a lot of links throughout the site that refer to them as page. Jsp (for example). This would require changing all links to them on the site.

I wonder if this would work in reverse. Could I change all the JSPs to the . Page extension and have the Servlet translate .

Jsp into . Page? – Matt Nov 10 at 1:41 @user1038812 You can make the container use whatever extension you want, if you don't care about following web app conventions.

– Dave Newton Nov 10 at 2:27.

As many have suggested its better to use Filters in this case. Put following snippet into web. Xml Filter definition ProcessFilter my.filter.

ProcessFilter Filter mapping ProcessFilter /content/*. Jsp ProcessFilter MyServlet OncePerRequestFilter If you would want to execute the filter only once you could store an attribute in request scope for the first time, and next time you could check if the attribute is set in which case do not process further. If you are using Spring framework you can either use one of the sub classes of OncePerRequestFilter or extend it and just implement doFilterInternal().

Otherwise you could refer to OncePerRequestFilter. Java : raw and implement/extend your filter. Here is a simplified version of it.

Public class ProcessFilter extends Filter { public final void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (!(request instanceof HttpServletRequest) ||!(response instanceof HttpServletResponse)) { throw new ServletException("OncePerRequestFilter just supports HTTP requests"); } HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; String alreadyFilteredAttributeName = "ALREADY_PROCESSED_BY_PROCESS_FILTER"; if (request. GetAttribute(alreadyFilteredAttributeName)! = null) { // Proceed without invoking this filter... filterChain.

DoFilter(request, response); } else { // Do invoke this filter... request. SetAttribute(alreadyFilteredAttributeName, Boolean. TRUE); try { doFilterInternal(httpRequest, httpResponse, filterChain); } finally { // Remove the "already filtered" request attribute for this request.Request.

RemoveAttribute(alreadyFilteredAttributeName); } } } protected void doFilterInternal( HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) { throws ServletException, IOException /* * * * Put your processing logic here * * */ } }.

According to me you should try using in your web. Xml to make some of your requests bypass the servlet.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions