开发者

JSP displaying source code instead of executing

I'm new to jsp and have ran into some trouble. Initially, the jsp file and associated java classes were built and tested fine on a test Tomcat server. Now, they've been transitioned to another server of what I believe is the same setup (except it's linux now instead of windows). But when the jsp page is accessed the source code is displayed instead of the jsp actually executing. I've googled for a while but received no success.

Here is the code of the jsp file I am testing:

<HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML>

And here is what I see in my browser when navigating to the page: 开发者_Python百科

 Hello! The time is now <%= new java.util.Date() %> 

The source of the page is the exact code that is typed in the example file:

<HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML>

The server appears to be working. Here are is the response headers I obtained from Firebug:

Date    Sat, 15 Jan 2011 20:53:24 GMT
Server  Apache/2.2.3 (CentOS)
Last-Modified   Sat, 15 Jan 2011 02:20:18 GMT
Etag    "b385d8-55-499d931205c80"
Accept-Ranges   bytes
Content-Length  85
Content-Type    text/html; charset=UTF-8

I had thought that this page might solve the problem since there was no reference to the jsp file I was using or even the following snippets in my web.xml file in the WEB-INF folder:

<servlet>
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
    <init-param>
        <param-name>logVerbosityLevel</param-name>
        <param-value>WARNING</param-value>
    </init-param>
    <load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
</servlet-mapping>

I tried inserting these lines and restarting Tomcat, but no success. Any ideas?


From the response headers:

Server Apache/2.2.3 (CentOS)

This is not served by Apache Tomcat, but by Apache HTTPD. You did not deploy it to Tomcat at all.


I have just had this same problem, which is how I stumbled on this post. For me, it turned out that the issue was the difference between CATALINA_HOME and CATALINA_BASE. I think a lot of people don't realize this is two different places. On my Ubuntu, CATALINA_HOME was in /usr/share/tomcat6 but CATALINA_BASE was in /var/lib/tomcat6. It matters because you need to put your jsp files in CATALINA_BASE. So, in fact, I would say that Tomcat was not finding your code, even though your browser found the file and happily displayed it for you as it does other text files.

Hope this helps someone else who comes here with the same problem--it's my first attempt to post an answer on StackOverflow.


See where it says "logVerbosityLevel" and "WARNING"? Change WARNING to DEBUG. You can also find your catalina.sh script and find your java command and make sure the flag -DDEBUG is included in the startup options.

Looking in your log with full debug mode on will tell you if there are any problems starting Tomcat on the new server.

For instance, you could have a missing depencency or a dependency conflict that isn't showing up with less informative logging levels.

Lastly, have you tried a simple JSP test page?

      <% out.println("Hello"); %>

If you put that in a JSP and try to load it, do you see the JSP or the output?

Have you tried using the Tomcat port in your request? Usually, this is 8080. I think one of the commentors mentioned that as a possibility.


Migration java 6 --> 8, tomcat 5 --> 8 if you have had something like this in app web.xml

<!--need this so that this work:  jsp:include page="WEB-INF...xxx.jspf-->
<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jspf</url-pattern>
</servlet-mapping>

that would produce JSP displaying source code instead of executing at tomcat 8 as it seems it rolls over the thing about jsp inside tomcat web xml.

Solution is to erase this code in app web.xml and put everything into tomcat web.xml, like:

<servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspx</url-pattern>
    <url-pattern>*.jspf</url-pattern>
</servlet-mapping>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜