0

I have the following code (Servlet):

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

public class YourServletName         implements Servlet{  
ServletConfig config=null;  

public void init(ServletConfig config){  
this.config=config;  
System.out.println("servlet is initialized");  
}  

public void service(ServletRequest    req,ServletResponse res)  
throws IOException,ServletException{  

res.setContentType("text/html");  

PrintWriter out=res.getWriter();  
out.print("<html><body>");  
out.print("<b>hello simple servlet</b>");  
out.print("</body></html>");  

}  
public void destroy(){
System.out.println("servlet is destroyed");}  
public ServletConfig getServletConfig(){return config;}  
public String getServletInfo(){return "copyright 2007-1010";}  

}  

When I run it in Eclipse Luna, I got the following error:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.


Apache Tomcat/8.0.21

Could somebody please tell me why?

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • Why are you implementing your own Servlet instead of extending already created HttpServlet? – Pshemo Apr 11 '15 at 12:27
  • Also how are you trying to access this servlet? Can you show us URL used and where you set up this URL to be handled by your servlet? – Pshemo Apr 11 '15 at 12:28
  • I know, but i want to experience implements Servlet... You can help me...? – Hiếu Đại Nhân Apr 11 '15 at 12:30
  • You need a web.xml file. Map the servlet to releavent context. – aluz Apr 11 '15 at 12:35
  • I'm not an expert in JEE but from what I remember 404 error means that resource can't be found. Did you set mapping in your deployment descriptor (web.xml) to map your URL you used to this servlet? – Pshemo Apr 11 '15 at 12:35
  • can you show web.xml ?? – Prashant Apr 11 '15 at 12:42
  • Try adding `@WebServlet("/servletAddress")` right before its `public class YourServletName` and use `http://yourServer/yourProject/servletAddress`. – Pshemo Apr 11 '15 at 14:01

1 Answers1

0

You need to add a server mapping to your web.xml to map the url pattern to the servlet, otherwise tomcat just thinks you're looking for a subdirectory.