3
votes

while developing servlet program i suffered from a probem i used netbeans ide and used glassfish server here is my code for the index.html file:-

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <body>
        <form action="NewServlet">
            name:<input type="text" name="t1">
            password:<input type="password" name="t1">
            <input type="submit" value="login">
        </form>
    </body>
</html>

and code for servlet is:-

import java.io.*;
import javax.servlet.*;

/**
 *
 * @author Prathamesh
 */
public class NewServlet extends GenericServlet {
    @Override
    public void service(ServletRequest req,ServletResponse res)throws ServletException,IOException
    {
        res.setContentType("text/html");
        PrintWriter pw=res.getWriter();
        String un="abc";
        String pwd="xyz";
        String p1=req.getParameter("t1");
        String p2=req.getParameter("t2");
        if((p1.equals(un))&&(p2.equals(pwd)))
            pw.println("<h1>Welcome to home page</h1>");
        else
            pw.println("Invalid");
        pw.close();    
    }
}

i think the problem is occuring while parsing the variables is servlet... please any one can help me?? and also the index file executes perfectly but when i directly click on button without typing anything in textbox it shows correct output but when i enter username and password specified in programs(abc & xyz) it shows error:

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1 logs. GlassFish Server Open Source Edition 4.1

3

3 Answers

1
votes

Problem here is that you failed to pass correct name to password field:

password:<input type="password" name="t1">

In servlet, you used the code, which returns p2 as null

String p2=req.getParameter("t2");

p2.equals(pwd)//results in NPE

Try this in html

password:<input type="password" name="t2">
1
votes

You HTML can not distinguish between name = t1for Name and name="t1" for password. Try changing it to t1 and t2. In your code you are not sending any t2 to the servlet. Also try to use HttpServlet instead GenericServlet.

1
votes

GenericServlet is for protocol-independent servlet. For Web, use HttpServlet. So, you should extends HttpServlet instead of GenericServlet and override doGet or doPost method. As you have password field so override doPost method, which is recommended for security reason. Than configure servlet at web.xml or use @WebServlet annotation

@WebServlet
public class NewServlet extends HttpServlet{
   public void doPost(...){}
}

At form you need to define post method

<form action="NewServlet" method="post">
      ...
</form>