Thuc Hung
Thuc Hung

Reputation: 130

HTTP Status 404 - Servlet .... is not available

I use eclise to create a servlet like this :

package hello;
public class NewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("doPost");

    String name = request.getParameter("textField");

    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.print("<html><head></head><body><center>");
    pw.print("Hello " + name + "!");
    pw.print("</center></body></html>");
 }
}

and a html file like :

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="NewServlet">
    <p align="center">
        <font>Input some text</font> <br> <input type="text"
            name="textFiled"> <br> <input type="submit"
            value="submit"> <br>
    </p>
</form>
</body>
</html>

when i run the servlet, met an error :

HTTP Status 404 - Servlet NewServlet is not available

--------------------------------------------------------------------------------

type Status report

message Servlet NewServlet is not available

description The requested resource (Servlet NewServlet is not available) is not available.

i checked the folder : WEB-INF or any folder else and can't see file .class

How is this caused and how can I solve it?

Upvotes: 0

Views: 19095

Answers (4)

Mohamed Albohy
Mohamed Albohy

Reputation: 1

edite this: form method="post" action="NewServlet"

to this
form method="post" action="/(your project name ) /NewServlet"

i had the same problem and this worked for me

Upvotes: 0

irshad.ahmad
irshad.ahmad

Reputation: 291

Format in web.xml should be like this.

          <servlet-name>NewServlet</servlet-name>
           <servlet-class>PackageName.JavaClass</servlet-class>

which in your case is

          <servlet-name>NewServlet</servlet-name>
           <servlet-class>Hello.NewServlet</servlet-class>

Upvotes: 0

agaonsindhe
agaonsindhe

Reputation: 594

You should check web-inf folder in your IDE and map your servlet in web.xml file

      <servlet>
              <servlet-name>NewServlet</servlet-name>
               <servlet-class>NewServlet</servlet-class>
      </servlet>
       <servlet-mapping>
               <servlet-name>NewServlet</servlet-name>
               <url-pattern>/NewServlet</url-pattern>
        </servlet-mapping>

make sure that this mapping is done properly and also your servlet is not in any package or folder if so then in servlet tag write that class name followed by . and your servlet name.

If problem still arises just make sure that you delete that .class file of your servlet and build your project again.(Net beans have an option of clean and build and then run) haven't use eclipse but i am sure it has similar option as well

Upvotes: 1

BalusC
BalusC

Reputation: 1109502

Servlets needs to be registered and mapped on a specific URL pattern in order to be able to execute them by a HTTP request. Given your HTML code you seem to expect the servlet to listen on an URL of /NewServlet.

If you're using Tomcat 7, then just put the @WebServlet annotation on the class with exactly that URL pattern:

@WebServlet("/NewServlet")
public class NewServlet extends HttpServlet {
    // ...
}

If you're still on Tomcat 6 or older for some reason, then you'd need to do it by the old fashioned web.xml way. A concrete example can be found in our servlets wiki page.

Eclipse won't show .class files in the project explorer. It will only show them in the navigator, in the /build folder. But this should not be something to worry about right now.

Upvotes: 0

Related Questions