I am writing a web application that has a servlet that should handle incoming HTTP request. Using the getServletPath() method, the servlet checks if the URL is of valid format. The servlet performs some processing, and then forwards the request to a JSP file using:
The valid URL format should be of the form, for example, http://machine/mywebapp/2004/02/10/filename.
So in effect, the getServletPath() method should return url of format /####/##/##/aFilename before the servlet can consider the request valid; otherwise request will be forwarded to error.jsp.
I am using Tomcat 3.3.1a, and JDK 1.4.2. My Tomcat has the following directory structure:
|- templates <-- where jsp and html files reside
and I have the following in my web.xml:
The application works fine, until I started including an HTML page using JSP action. I am not allowed to use the include directive so that whenever header.html gets updated, the right content is displayed with my.jsp. I encounter the following error message:
javax.servlet.ServletException: Cannot forward because the response has already been committed
... etc ...
... etc ...
my.jsp contains the following lines:
<jsp:include page="header.jsp" flush="true"/>
<jsp:include page="header.html" flush="true"/>
JSP-including another JSP file works fine, output is displayed correctly. JSP-including an HTML causes the problem. When I traced and debugged the application, I noticed that when you include an HTML, Tomcat (somehow) treats that as another request, and since I have "/" in my <url-pattern>, the request goes back to MyServlet (for the SECOND time). Making the JSP page autoFlush=false, and increasing the buffer size did not help.
When the user types in the correct URL, the request goes to the servlet, and servlet forwards the request to my.jsp, and thus committing the response; so the second time it calls forward(), IllegalStateException is thrown. Another thing that I noticed, on the second time when it enters MyServlet, calling getServletPath() returns "/templates/my.cbc", and the second call to forward() actually attempts to forward to error.jsp.
Here are my questions:
1. What's the difference between JSP-including another JSP and an HTML? Why does jsp-including an HTML file treats it as another request?
2. Is there any way that i can change web.xml? Or is there any other configuration task that I need to do? I've tried creating another context in server.xml, but all it does is to create another context of the same application.
3. If I have "/whatever/*" as my <url-pattern> in web.xml, the request should be of the format http://machine/my_web_app/whatever/2004/02/10/filename
before Tomcat can dispatch the request to MyServlet. And for this case, the URL format does not follow the business requirement of the project.
Thank you very much for the help.