2

I am trying to make a simple code for servlet to accept a input from user and print it. this is my Servlet clas-:

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

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

/**
 * Servlet implementation class welcome
 */
//@WebServlet("/welcome")
public class welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public welcome() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());

        response.setContentType("text/html");  
        PrintWriter pw=response.getWriter();  

        String name=request.getParameter("name");//will return value  
        pw.println("Welcome "+name);  

        pw.close();  
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

My Html page code-:Newfile101.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./welcome" method="get">  
Enter your name<input type="text" name="name"><br>  
<input type="submit" value="login">  
</form> 
</body>
</html>

My web.xml file-:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>T3</display-name>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>



  <servlet>
<servlet-name>son</servlet-name>
<servlet-class>welcome</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>son</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

  <welcome-file-list>  

   <welcome-file>NewFile101.html</welcome-file>  
  </welcome-file-list>  
  </web-app>

Now eventhough my web.xml contains welcome file list it is still not working. I am using Tomcat 8.5 in eclipse oxigen.

Please help.

user3262269
  • 113
  • 1
  • 4
  • 13

1 Answers1

0

you have error in your mapping not in welcome file list if you change to simple /welcome it will work

or in simple words remove the dot**(.)** from your form where you have added before/welcome`

and one more thing, remove

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

as this is not a good practice

To make it work :)