29

I am writing a Java Servlet, and I am struggling to get a simple HelloWorld example to work properly.

The HelloWorld.java class is:

package crunch;

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 {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

I am running Tomcat v7.0, and have already read similar questions, with responses referring to changing the invoker servlet-mapping section in web.xml. This section actually doesn't exist in mine, and when I added it the same problem still occurred.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Tom Haddad
  • 377
  • 2
  • 5
  • 12

7 Answers7

24

Try this (if the Java EE V6)

package crunch;

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

@WebServlet(name="hello",urlPatterns={"/hello"}) // added this line

public class HelloWorld extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.println("Hello World");
  }
}

now reach the servlet by http://127.0.0.1:8080/yourapp/hello

where 8080 is default Tomcat port, and yourapp is the context name of your applciation

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • I can't get access in my html file, please help!!http://stackoverflow.com/questions/24967510/the-requested-resource-xxx-html-is-not-available-when-redirect/24967768#24967768 – JaskeyLam Jul 26 '14 at 07:57
11

You definitely need to map your servlet onto some URL. If you use Java EE 6 (that means at least Servlet API 3.0) then you can annotate your servlet like

@WebServlet(name="helloServlet", urlPatterns={"/hello"})
public class HelloWorld extends HttpServlet {
     //rest of the class

Then you can just go to the localhost:8080/yourApp/hello and the value should be displayed. In case you can't use Servlet 3.0 API than you need to register this servlet into web.xml file like

<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>crunch.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>
Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
7

Writing Java servlets is easy if you use Java EE 7

@WebServlet("/hello-world")
public class HelloWorld extends HttpServlet {
  @Override
  public void doGet(HttpServletRequest request, 
                  HttpServletResponse response) {
   response.setContentType("text/html");
   PrintWriter out = response.getWriter();
   out.println("Hello World");
   out.flush();
  }
}

Since servlet 3.0

The good news is the deployment descriptor is no longer required!

Read the tutorial for Java Servlets.

Roman C
  • 49,761
  • 33
  • 66
  • 176
  • The link to the Servlet tutorial changes to [http://docs.oracle.com/javaee/7/tutorial/servlets.htm#BNAFD](http://docs.oracle.com/javaee/7/tutorial/servlets.htm#BNAFD) – Campfire Apr 13 '17 at 12:26
2

this is may be due to the thing that you have created your .jsp or the .html file in the WEB-INF instead of the WebContent folder.

Solution: Just replace the files that are there in the WEB-INF folder to the Webcontent folder and try executing the same - You will get the appropriate output

prasanth
  • 41
  • 1
0

For those stuck with "The requested resource is not available" in Java EE 7 and dynamic web module 3.x, maybe this could help: the "Create Servlet" wizard in Eclipse (tested in Mars) doesn't create the @Path annotation for the servlet class, but I had to include it to access successfuly to the public methods exposed.

vladiastudillo
  • 407
  • 1
  • 10
  • 23
0

You have to user ../../projectName/Filename.jsp in your action attr. or href

../ = contains current folder simple(demo.project.filename.jsp)

Servlet can only be called with 1 slash forward to your project name..

0

My problem was in web.xml file. In one <servlet-mapping> there was an error inside <url-pattern>: I forgot to add / before url.

parsecer
  • 4,758
  • 13
  • 71
  • 140