0

I have created a login page using html where I take username and password and display the username. I have created a servlet for displaying the username, where I use request.getParameter(<fieldName>) to display the username. I am using Eclipse IDE and Tomcat to deploy the application. I am unable to do so since the method returns null. The code is given below :

PageLogin.java

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

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PageLogin() {
        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());
        processRequest(request, response);
    }

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

    }

    public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String user = request.getParameter("username");
        String pass = request.getParameter("password");
        PrintWriter out = response.getWriter();

        System.out.println("username : "+user);
        System.out.println("password : "+pass);

        String htmlResponse = "<html>";
        htmlResponse += "<h2>Username : " + user + "</h2>";
        htmlResponse +="</html>";

        out.println(htmlResponse);
    }

}

index.html

    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>

<form action="PageLogin" method="post" >
    Username :<input type="text" name="username"><br>
    Password :<input type="password" name="password"><br>

    <input type="submit" value="Login">

</form>

</body>
</html>

web.xml

    <?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>LoginQ3</display-name>

  <servlet>
  <servlet-name>loginpage</servlet-name>
  <servlet-class>PageLogin</servlet-class>
  </servlet>

  <servlet-mapping>
  <servlet-name>loginpage</servlet-name>
  <url-pattern>/display</url-pattern>
  </servlet-mapping>

  <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>
</web-app>

When I use the url

http://localhost:8080/LoginQ3/login

I get the output as :

Username : null

Pritom Mazumdar
  • 327
  • 4
  • 18
  • 1
    Are you talking about `POSTING` or `GETTING` ? – Scary Wombat Aug 02 '18 at 07:23
  • I am new to this, so I just want to display the username when I use the url given – Pritom Mazumdar Aug 02 '18 at 07:27
  • What will be the difference if I `GET` or `POST` ? – Pritom Mazumdar Aug 02 '18 at 07:27
  • 2
    @PritomMazumdar https://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get – Ivar Aug 02 '18 at 07:28
  • I think that the problem is that you are sending a GET directly to the servlet without going through your JSP. But the GET request doesn't have the `username` and `password` parameters in the URL. So, when your `processRequest` method attempts to access them, it gets `null`. – Stephen C Aug 02 '18 at 07:30
  • @Ivar, so if I want to display something on screen should I use the `POST` method or the `GET` method. Like in this code I have used the contents of the method `processRequest()` inside `GET` method and it returns `null`, on the other hand if I put the contents of the method `processRequest()`, there's a blank screen(nothing is displayed) – Pritom Mazumdar Aug 02 '18 at 07:32
  • 1
    If you visited `http://localhost:8080/LoginQ3/login?username=fred&password=secret` you might have more success. That replicates what the "Login" button in your JSP should be doing ... – Stephen C Aug 02 '18 at 07:33
  • 1
    It depends on what you are trying to achieve. If you want to submit a form and then show it back to the user, this is fine (although you should remove the `processRequest(request,response);` from your `doGet`). If you want to show the username that you in an earlier request submitted, then you need to store it somewhere first. Like in a session. – Ivar Aug 02 '18 at 07:36
  • @StephenC, I have tried your way and it worked, so now what should I change in my files so that after I click on the `login` button, it displays the username? – Pritom Mazumdar Aug 02 '18 at 07:36

0 Answers0