Hi, 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: getServletContext().getRequestDispatcher("/templates/my.jsp").forward(req,res) 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: <TOMCAT_HOME> |- webapps |- mywebapp |- MEAT-INF |- templates <-- where jsp and html files reside |- WEB-INF |- classes |- lib and I have the following in my web.xml: <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.company.MyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
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 at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:460) at templates.cbc_18._jspService(cbc_18.java:146) ... 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.
1) they are both treated as 'another request', because you are using jsp:include. It's just that you're not noticing it, because your default servlet is not being invoked for the *.jsp includes. Because the container recognizes that request... it's a JSP page. It does not, on the other hand, recognize the .html. That is: recognize it as a mapped servlet, or as a mapped *.jsp (*.jsp is a mapping provided for you in conf/web.xml). Therefore, it's handled by your default servlet, which is where your problems start.
3) I have a project with much the same requirement. No extra stuff on the address bar...
here's a post I made to tomcat-user regarding a similar difficulty with images and css. There were 3 suggested work-arounds, on top of how I did it.