0

Below is my Servlet and jsp code. I am trying to login with username "admin" and password "pass", but I am getting a NullPointerException. Below is also the error which I receive when using a browser to log in.

Servlet Code:

package org.fproject;

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

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

/**
 * Servlet implementation class AdminLogin
 */
@WebServlet(value="/AdminLogin",initParams={@WebInitParam (name="user", value="admin"),
        @WebInitParam(name="pass",value="pass")})
public class AdminLogin extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLogin() {
        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
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String iuser=getInitParameter("user");
        String ipass=getInitParameter("pass");
        String user=req.getParameter("user");
        String pass=req.getParameter("pass");
        res.setContentType("text/html");
        PrintWriter out=res.getWriter();
        out.println("<html><body>");
        if(user.equals("admin") && pass.equals("pass")){
            out.println("<b>Welcome<b>");
        }
        else{
            out.println("<b>Invalid Login<b>");

        }
        out.println("</body></html>");
        out.close();
    }

    }

JSP Code

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="org.fproject.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="AdminLogin"  method="post">
     <table border="0">
     <tr>
<td>Username<td>
<td><input type="text" name="user" size="50"/></td>
</tr>
<tr>
<td>Password<td>
<td><input type="password" name="password" size="50"/></td>
</tr>
<tr>
<td><input type= "submit" value="SUBMIT"/></td>
</tr>
</table>
</form>
</body>
</html>

Error:-

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException org.fproject.AdminLogin.doPost(AdminLogin.java:48) javax.servlet.http.HttpServlet.service(HttpServlet.java:754) javax.servlet.http.HttpServlet.service(HttpServlet.java:847) note The full stack trace of the root cause is available in the JBoss Web/7.0.13.Final logs.

JBoss Web/7.0.13.Final

devlin carnate
  • 7,947
  • 7
  • 46
  • 77

1 Answers1

0

replace

<input type="password" name="password" size="50"/>

by

<input type="password" name="pass" size="50"/>

From jsp page

OR

Change

String pass=req.getParameter("pass");

by

String pass=req.getParameter("password");

from Servlet

ELITE
  • 5,675
  • 3
  • 17
  • 25