0

I need to run my servlet on a server with a running tomcat.

I create my HelloWorld servlet from the java file(HelloWorld.java).

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException
{
    response.setContentType( "text/html" );
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Hello World</title></head>");
    out.println("<body><h1>HELLO WORLD</h1></body>");
    out.println("</html>");
    out.close();
}

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

}

Than I upload it on a server and run

javac HelloWorld.java

This command creates HelloWorld.class file which i put into WEB-INF/classes folder

Than I add some code to the web.xml file in the WEB-INF directory, so it looks like this

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
        <display-name>My first Servlet</display-name> 
    <servlet> 
        <servlet-name>HelloWorldServlet</servlet-name> 
        <servlet-class>HelloWorld</servlet-class> 
    </servlet> 
    <servlet-mapping> 
        <servlet-name>HelloWorldServlet</servlet-name> 
            <url-pattern>/HW</url-pattern> 
    </servlet-mapping> 
</web-app>

Than i run a command

touch ~WEB-INF/web.xml

Now i try to access my HelloWorld servlet by entering URL like ~\HW.

But i get an error:

type Status report

message /group05/HW

description The requested resource (/group05/HW) is not available.

What would you recommend to do to fix it?

Thanks for considering my question.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Denys
  • 4,287
  • 8
  • 50
  • 80

1 Answers1

1

There are in this particular case 2 potential causes:

  1. Tomcat isn't configured to hotdeploy after a web.xml edit. You need to restart Tomcat manually.

  2. The particular Tomcat setup doesn't support servlet classes in the default package. You should always put the class in a package if the class is intented to be used by another classes which are by itself inside a package (such as Tomcat internal classes).

See also:


Unrelated to the concrete problem, I understand that you're just getting started with servlets, I will however point out that this isn't the "best practice" to use servlets. I suggest to take a look in our servlets wiki page to get some concrete examples, learn about the canonical approaches and find links to proper tutorials.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Oh, it's a 3rd party host? Well, best would be to read their developer guide or to contact their support and ask it. Also, some hosts require that servlets are placed in a specific package and/or are called by a specific URL pattern. If I recall correctly, Godaddy for example requires an additional `/servlet` path in the URL or something. – BalusC Dec 04 '11 at 19:07
  • Yes, it's a 3rd party host :) – Denys Dec 04 '11 at 19:44
  • Okay. Is there any progress? You haven't told if putting it in a package solved it, nor anything about the host. – BalusC Dec 05 '11 at 02:25
  • Apparently that was a server issue. Today, on Monday, servlets are running just fine:) – Denys Dec 05 '11 at 17:51
  • In other words, the server has been restarted (most elcheapo hosts do that once daily, usually overnight). This confirms cause #1 in my answer. – BalusC Dec 05 '11 at 17:58
  • does it mean that every time i edit my web.xml i need to wait another server restart?? – Denys Dec 05 '11 at 19:13
  • As you still haven't told which host you're using, your current best bet is to contact host support yourself for the definitive answer. – BalusC Dec 05 '11 at 19:29