• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

jsp:include page="header.html" causes IllegalStateException

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marlon tan:


Dir structure of the above problem
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi sir you are adding 2 jsp pages header.jsp and header.html
in these jsp there should not be any request dispatcher
RequestDispatcher rd = request.getRequestDispatcher("/a.jsp");
rd.forward(request,response);
remove this code from both the jsp you are including.
i hope it will help you
.
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by R K Singh:

<hr></blockquote>
Dir structure of the above problem[/QB]



Hi,
What do you mean by the directory structure has a problem? What difference would it make if i put in my JSP files inside [mywebapp] folder, or renaming templates as something else?
Thank you very much.
Regards,
Marlon
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lali lali:
hi sir you are adding 2 jsp pages header.jsp and header.html
in these jsp there should not be any request dispatcher
RequestDispatcher rd = request.getRequestDispatcher("/a.jsp");
rd.forward(request,response);
remove this code from both the jsp you are including.
i hope it will help you
.



Hi,
My header.jsp does not contain anything but HTML table definition. If for example I ONLY have <jsp:include ..."header.html"/> in my.cbc, IllegalStateException is thrown as well.
Thank you very much.
- Marlon
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
marlon,

R K was trying to say that your directory structure as given, was "too flat". It looks like all of those folders are all hanging off of TOMCAT_HOME


Also, I think my answer in the Apache/Tomcat forum will help you.
https://coderanch.com/t/83450/Tomcat/jsp-include-page-header-html
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marlon tan:
What do you mean by the directory structure has a problem? [/QB]


Can you see the difference between dir structure in your post and in mine.
No, it had nothing to do with problem, it was for others to not get incorrect dir structure.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Something else worth mentioning...

If you're able to upgrade to Tomcat 4.1.x or Tomcat 5.x, then you could actually use a <%@ include directive. This would benefit you in two ways:

1) You've got a much more stable, actively supported, and faster version of Tomcat
2) You've avoided the problem of the default servlet being invoked twice.

I use <%@ include on files that change periodically, and Tomcat 4.1.29 will recognize that this included file has changed, and recompile the parent file (without the parent file having to change).
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Curwen:
marlon,

R K was trying to say that your directory structure as given, was "too flat". It looks like all of those folders are all hanging off of TOMCAT_HOME


Also, I think my answer in the Apache/Tomcat forum will help you.
https://coderanch.com/t/83450/Tomcat/jsp-include-page-header-html


Hi,
I think I know what you mean. My directory is not "flat" I think when I posted the message, somehow my directories were all indented to the left.
- Marlon
 
marlon tan
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Curwen:
Something else worth mentioning...

If you're able to upgrade to Tomcat 4.1.x or Tomcat 5.x, then you could actually use a <%@ include directive. This would benefit you in two ways:

1) You've got a much more stable, actively supported, and faster version of Tomcat
2) You've avoided the problem of the default servlet being invoked twice.

I use <%@ include on files that change periodically, and Tomcat 4.1.29 will recognize that this included file has changed, and recompile the parent file (without the parent file having to change).



Hi Again,
Our company has been using Tomcat 3.3.1a, and has no plans to upgrade to a higher version in the near future.
For now, what I did in my.jsp was to open the included HTML file using a FileReader and BufferedFileReader, read the content of the HTML, then I just output the content using out.println() statement. I know this will drastically slow down the system. I've tried using HttpURLConnection, but still, it will treat it as another request. (1) Is there any other workaround that I can do (for version 3.3.1a) that will help me solve the problem?
I believe that for version 4x and 5x, the default servlet would pertain to org.apache.catalina.servlets.DefaultServlet. (2) For Tomcat 3.3.1a, is there a corresponding default servlet which I can extend, or add to my web.xml such as:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>... default servlet for version 3x... </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
I really appreciate your help. Thank you very much!

Regards,
Marlon
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic