0

If I made a stupid mistake and asking a stupid question then my apologize. I have a servlet called HelloWorld - it is a simple servlet that implements the Servlet interface that is part of "1stapp" project. Its code is the following:

 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.*;
 public class helloworld implements Servlet
 {
     private ServletConfig config;
     public void init(ServletConfig config) 
        throws ServletException {
         this.config=config;
     }
     public void destroy(){}
     public ServletConfig getServletConfig() {
         return config;
     }
     public String getServletInfo() {
         return "this is simple hello World Servlet";
     }
     public void service(ServletRequest request, ServletResponse response)
             throws ServletException, IOException {
         response.setContentType("text/html");
         PrintWriter out=response.getWriter();
         out.println("<html><head>");
         out.println("<title>Simple Servlet</title>");
         out.println("</head>");
         out.println("<body>");
         out.println("<h1>Hello, World</h1>");
         out.println("</body></html>");
         out.close();
      }
  }

The HelloWorld servlet was successfully compiled into classes directory within WEB-INF. The deployment descriptor is the following:

  <?xml version="1.0" encoding="ISO-8859-1"?>
  <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
          "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
  <web-app>
        <servlet>
             <servlet-name>hello</servlet-name>
             <servlet-class>helloworld</servlet-class>
        </servlet>

        <servlet-mapping>
             <servlet-name>hello</servlet-name>
             <url-pattern>/hello</url-pattern>
        </servlet-mapping>
  </web-app>

Now, when I type localhost:8080/1stapp/hello firefox geves me problem loading page message. Can anybody say what i did wrong?

7
  • Can you please add the error message and the missing part of your web.xml Commented Feb 6, 2017 at 9:41
  • Can you add what exact error Firefox is displaying. Commented Feb 6, 2017 at 9:43
  • Do you specifically need the web.xml? it works without that. Commented Feb 6, 2017 at 9:47
  • It is just "Unbale to connect. FIrefox can't estableish a connection to the server at localhost:8080" message Commented Feb 6, 2017 at 9:54
  • 1
    If firefox says it can't connect than the WebContainer / Tomcat is either not running or your application was not deployed correctly. Have a look at the Tomcat (whatever WebContainer you are using) log files and the console. What's also possible is that the container is listening to a port other than 8080. Commented Feb 6, 2017 at 10:10

2 Answers 2

1

There seem to be least three issues

Classes are searched in WEB-INF/classes rather than in WEB-INF

Use the HttpServlet instead of the generic Servlet

Overwrite the doXXX methods of the HttpServlet and not the generic service method. The service method dispatches to the corresponding doXXX method

Sign up to request clarification or add additional context in comments.

2 Comments

what I am doing now is trying to understand simple servet interface implmentation
If u look carefully at my fist post, I wrote I complied the class file into classes subdirectory of web-inf
0

This is not the way to map Servlet in web.xml

First of all you have to declare your Servlet in web.xml as below

<servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>HelloWorld</servlet-class>
</servlet>

Then map your URL pattern to the Servlet as

<servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/hello</url-pattern>
</servlet-mapping>

And you better extend HttpServlet rather than Servlet. HttpServlet is the convenient class to work with HTTP protocol.

Change your Servlet class as below.

public class HelloWorld extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html><head>");
        out.println("<title>Simple Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello, World</h1>");
        out.println("</body></html>");
        out.close();
    }
}

In addition your Servlet class name should be HelloWorld instead of helloWorld.

5 Comments

I did not use any package, I just wrote HelloWorld.java put servlet code there and compiled into classes directory
Okay, if you dont have any package thats fine (answer edited). To be specific you need to extend HttpServlet instead of implementing Servlet interface.
That is the point I am trying to understand. For web pages do i need to extend httpservlet or it will work with any class implementing servlet interface. Can u tell me about that?
apart from extending http servlet, your servlet mapping is exactly the same as mine
Okay thats an exact question now. Yes. It should work. I tested your code in my context and It runs as expected. though I dont know what exact error firefox giving u. Could u try with another browsers and make it sure your localhost:8080/ is running and Its not the error about your tomcat configuration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.