Unable to run a simple servlet
I am making my first servlet following this link http://www.roseindia.net/servlets/
This is my servlet in Eclipse ServletDemo->Java Resources->src->(default package)->HelloServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest开发者_JS百科;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
}
This is my web.xml file
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
I am using Tomcat server, I had also set Environment variable for servlet-api.jar. But while running the same I am getting following error:
HTTP Status 404-Servlet Hello is not available.
Servlets may no longer live in the default package; create a package and move the servlet in to it (depends on what version on Tomcat you're using).
It also matters how you're deploying/running the servlet, as compared to how you're attempting to access it from the browser--you need to specify a context, if you're deploying it to something other than the root context.
(I also tend to point people away from tutorials from that site as they're often... misleading. And while I know this is just a tutorial, bear in mind that generating HTML from inside a servlet like this is essentially never what you want to do :)
精彩评论