12 Replies - 7500 Views - Last Post: 09 July 2012 - 01:56 AM

#1 Paci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 18-January 12

Deploying basic Java Servlet: Tomcat 7

Posted 06 July 2012 - 03:17 AM

Hi guys

I can not seem to get the following basic java servlet to run, this was given as an example.Can someone please tell me if I am missing something, when I run the basic tomcat index.jsp file using: http://localhost:8080/index.jsp, then it works perfectly,so I think Ive set tomcat7 up correctly. When I type in the following url: http://localhost:8080/HelloWorld.java then the browser asks if I want to download the file and if I choose to open the file with a browser then it just shows me the source code.
All my created .jsp files run but servlets are all giving me this behavior, I have reinstalled several times, I think this is probably due to my ignorance in some department, can anyone please give me advice?

This servlet(HelloWorld.java) and its compiled class(HelloWorld.class) are located within the default ROOT folder within tomcat:
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\

The web.xml located below file is located within the following older:
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\


My HelloWorld.java servlet:
package org.kodejava.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    public HelloWorld() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head><title>Hello World Servlet</title></head>");
        writer.println("<body>Hello World! How are you doing?</body>");
        writer.println("</html>");
        writer.close();
    }
}


The web.xml file:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

</web-app>

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>kodejava-example</display-name>
	<servlet>
		<description></description>
		<display-name>HelloWorld</display-name>
		<servlet-name>HelloWorld</servlet-name>
                <servlet-class>
                        org.kodejava.example.servlet.HelloWorld
                </servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/HelloWorld</url-pattern>
	</servlet-mapping>
</web-app>


Is This A Good Question/Topic? 0
  • +

Replies To: Deploying basic Java Servlet: Tomcat 7

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1131
  • View blog
  • Posts: 2,484
  • Joined: 05-May 05

Re: Deploying basic Java Servlet: Tomcat 7

Posted 06 July 2012 - 08:09 AM

Quote

When I type in the following url: http://localhost:8080/HelloWorld.java then the browser asks if I want to download the file and if I choose to open the file with a browser then it just shows me the source code.


It supposed to do that. You're requesting a Java file directly, so it's asking you if you want to download it, just as with an .exe or .zip file, for example. If you're trying to access a Servlet, you should be using a URL whose mapping is specified in web.xml

http://localhost:8080/HelloWorld

Aside from that, no one should be able to download your source code, so I'd be concerned about that. I think it's because you've put the class files in webapps\ROOT\, when they should be in webapps\ROOT\WEB-INF\classes. The fact that your Servlets aren't in the proper location may also contribute to the issues you're having.

To summarize:

(1) Access the Servlet without the .java extension in the URL.
(2) Place the classes files in their proper location.
Was This Post Helpful? 1
  • +
  • -

#3 Paci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 18-January 12

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:04 AM

View Postblackcompe, on 06 July 2012 - 08:09 AM, said:

Quote

When I type in the following url: http://localhost:8080/HelloWorld.java then the browser asks if I want to download the file and if I choose to open the file with a browser then it just shows me the source code.


It supposed to do that. You're requesting a Java file directly, so it's asking you if you want to download it, just as with an .exe or .zip file, for example. If you're trying to access a Servlet, you should be using a URL whose mapping is specified in web.xml

http://localhost:8080/HelloWorld

Aside from that, no one should be able to download your source code, so I'd be concerned about that. I think it's because you've put the class files in webapps\ROOT\, when they should be in webapps\ROOT\WEB-INF\classes. The fact that your Servlets aren't in the proper location may also contribute to the issues you're having.

To summarize:

(1) Access the Servlet without the .java extension in the URL.
(2) Place the classes files in their proper location.


Okay I made the classes folder within webapps\Root\web-INF\, I then proceeded to put the class file within the 'classes' folder.

I started tomcat again and typed in:' http://localhost:8080/HelloWorld ' and it gave me the following error:
HTTP Status 404 - /HelloWorld

type Status report

message /HelloWorld

description The requested resource (/HelloWorld) is not available.
Apache Tomcat/7.0.28

This post has been edited by Paci: 09 July 2012 - 12:06 AM

Was This Post Helpful? 0
  • +
  • -

#4 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:13 AM

You should use the web.xml and declare your servlet mapping there.

  <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>packageName.anotherPackageName.HelloWorld</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/helloWorld</url-pattern>
  </servlet-mapping>



Then up above add:
<welcome-file>/helloWorld</welcome-file>


By the way, why are you using a Servlet as an entry point? Use JSP instead.
Was This Post Helpful? 0
  • +
  • -

#5 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:20 AM

I'm sorry, on the <welcome-file> please omit the extra "/".

<welcome-file>helloWorld</welcome-file>


Was This Post Helpful? 0
  • +
  • -

#6 Paci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 18-January 12

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:30 AM

Thanks for the reply, yeah I would use a jsp but I am following my course rules, and I have to use a servlet.

Is this what you were asking me to do? Sorry this isnt in my textbook so it may look a bit messy:
I get the same error


<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>

</web-app>

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>kodejava-example</display-name>
	
	<welcome-file>HelloWorld</welcome-file>
	
	<servlet>
		<description></description>
		<display-name>HelloWorld</display-name>
		<servlet-name>HelloWorld</servlet-name>
                <servlet-class>
                        org.kodejava.example.servlet.HelloWorld
                </servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/HelloWorld</url-pattern>
	</servlet-mapping>
</web-app>


Was This Post Helpful? 0
  • +
  • -

#7 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:37 AM

Whoa. That xml is messy. I see you already have a servlet-mapping. Here's what mine looks like:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HelloJavaEE</display-name>
  <welcome-file-list>
    <welcome-file>helloWorld</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>HelloWorld</servlet-name>
  <servlet-class>aPackage.anotherPackage.HelloWorld</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>HelloWorld</servlet-name>
  <url-pattern>/helloWorld</url-pattern>
  </servlet-mapping>
  
</web-app>



That's it. Looks like your web.xml has two xml's in it, which is a no - no.
Was This Post Helpful? 0
  • +
  • -

#8 Paci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 18-January 12

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:39 AM

I saw I didnt have a welcome file list so I did the following:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>kodejava-example</display-name>
	<welcome-file-list>
		<welcome-file>HelloWorld</welcome-file>
	</welcome-file-list>
	<servlet>
		<description></description>
		<display-name>HelloWorld</display-name>
		<servlet-name>HelloWorld</servlet-name>
                <servlet-class>
                        org.kodejava.example.servlet.HelloWorld
                </servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>HelloWorld</servlet-name>
		<url-pattern>/HelloWorld</url-pattern>
	</servlet-mapping>
</web-app>

Was This Post Helpful? 0
  • +
  • -

#9 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:43 AM

And what happened? :)
Was This Post Helpful? 0
  • +
  • -

#10 Paci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 18-January 12

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:45 AM

Now no matter what url I type in, I get a blank white page....fml
Was This Post Helpful? 0
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 12:58 AM

Don't panic dude! Are you using eclipse? Your code works for me. Posted Image

Are you using Eclipse? Try cleaning your workspace. Do:

1. Right click on your Tomcat Server, click "Clean Tomcat Work Directory...".
2. Right click on it again, then click "Clean...".
3. Click Project, click "Clean...", check if "Clean All Projects" is selected then click "OK".
4. Right click on your project, click "Refresh".

Then run again.

This post has been edited by fromTheSprawl: 09 July 2012 - 12:59 AM

Was This Post Helpful? 0
  • +
  • -

#12 Paci  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 18-January 12

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 01:44 AM

I did a reinstall of Tomcat 7 and now i can access its default index.jsp.
I think if it works on your pc then it must be a fault with my pc.

Here is my setup and I still get error 404 requested resource not found:

I use textpad as per course instructions
HelloWorld.java: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT
HelloWorld.class: C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\WEB-INF\classes

This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <welcome-file-list>
    <welcome-file>HelloWorld</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>HelloWorld</servlet-name>
  	<servlet-class>org.kodejava.example.servlet.HelloWorld</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>HelloWorld</servlet-name>
  	<url-pattern>HelloWorld</url-pattern>
  </servlet-mapping>
  
</web-app>



This is my HelloWorld.java:
package org.kodejava.example.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    public HelloWorld() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");

        PrintWriter writer = response.getWriter();
        writer.println("<html>");
        writer.println("<head><title>Hello World Servlet</title></head>");
        writer.println("<body>Hello World! How are you doing?</body>");
        writer.println("</html>");
        writer.close();
    }
}


And i get the error:
HTTP Status 404 - /HelloWorld

type Status report

message /HelloWorld

description The requested resource (/HelloWorld) is not available.
Apache Tomcat/7.0.28
Was This Post Helpful? 0
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,055
  • Joined: 28-December 10

Re: Deploying basic Java Servlet: Tomcat 7

Posted 09 July 2012 - 01:56 AM

Well, I'm using Eclipse. Is your course available online? I only know how to use Eclipse. I guess when you finish your course you'll have more experience than me. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1