Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP displaying source code instead of executing

Tags:

java

jsp

tomcat

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:

 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?

like image 889
Evan Siroky Avatar asked Jan 15 '11 02:01

Evan Siroky


2 Answers

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.

like image 163
Nina Koch Avatar answered Sep 28 '22 05:09

Nina Koch


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.

like image 41
BalusC Avatar answered Sep 28 '22 03:09

BalusC