Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not execute servlet code

Tags:

java

jsp

servlets

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

like image 662
Prathamesh Koshti Avatar asked Feb 20 '26 03:02

Prathamesh Koshti


1 Answers

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">