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?
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