0

This is my Servlet Class.I put my index.html in project>webcontent>index.html. I have used Welcome file list into my web.xml file but still my servlet is not running properly and shows HTTP Status 404 error

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet2 extends HttpServlet
{

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException
{
    // TODO Auto-generated method stub
    super.doPost(req, resp);

    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    try {

        String user=req.getParameter("user");
        out.println("<h2> Welcome "+user+"</h2>");
    } finally {            
        out.close();
}


}

}

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <welcome-file-list>
<welcome-file>msg.html</welcome-file>
</welcome-file-list>
 </web-app>
Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82

1 Answers1

0

You have to configure your servlet in the web.xml descriptor:

<servlet>
    <servlet-name>Servlet Name</servlet-name>
    <servlet-class>package.of.MyServlet</servlet-
</servlet>
<servlet-mapping>
    <servlet-name>Servlet Name</servlet-name>
    <url-pattern>/myservlet</url-pattern>
</servlet-mapping>

and when you access http://localhost:8080/my-app-name/myservlet in the browser, the doGet method of your servlet will be executed.

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82