Html1 code:

<html>
<HEAD>
<title> Employee Login Page</title>

<BODY>
<H1> EMPLOYEE LOGIN PAGE </H1>
Enter User name: <input type=text >
Enter password: <input type =password>
Enter Employee id: <input type=text >
<form method="Post" action="http://localhost:8080/sonu1/ServletOne">
<input type=submit value=Login!>

</form>


</body>
</html>

ServletOne.java code:

import java.sql.*;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.io.PrintWriter;

public class ServletOne extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  doPost(request, response);
		}

        
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	    response.setContentType("text/html");
		PrintWriter out = response.getWriter (); 
        String name = request.getParameter ("name");
        String pwd = request.getParameter ("pwd");
        String empid=request.getParameter("empid");
        
	
        Connection conn = null;
   	   try
	      {
   
        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); 

        conn = DriverManager.getConnection 
		   ("jdbc:odbc:acc"); 
		   
		  
        Statement stmt = conn.createStatement (); 
        ResultSet rs = stmt.executeQuery ("select * from mysearch");        
        
      
		  	   
		   if (rs.next()) 
		   {
		   	
		   if((name.equals(rs.getString ("name")))
		   && (pwd.equals(rs.getString("pwd"))) && ( empid.equals(rs.getString("empid"))))
				  
				   {
		
			   out.println("Valid User");
			   RequestDispatcher rd=request.getRequestDispatcher("ServletTwo"); 
		       rd.forward(request,response);   
		   
           }
        
        else 
        	{
          response.sendRedirect("html2.html");
        }
       rs.close();
       }
	      }
	    catch(NumberFormatException e)
		{
            out.println("Number Format Exception");
            return;
     }
		
   	catch(SQLException e)
   	{
      	out.println(e.getMessage());
      	while((e = e.getNextException()) != null)
         	out.println(e.getMessage());
   	}
   	catch(ClassNotFoundException e)
   	{
      out.println(e.getMessage());
   	}
		
   	finally
   	{
      
      	if(conn != null)
      	{
         	try
         	{
           		 conn.close();
         	}
         	catch (Exception ignored) {}
         }
 	} 
 } 
 
}

Html2 code:

<!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>Incorrect</title>
</head>
<body>
<H2>Incorrect login information</H2>
<a href="html1.html"><H3>Return to Login page</H3></a>
</body>
</html>

ServletTwo code:

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

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


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



public class ServletTwo extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		response.setContentType("text/html");
		PrintWriter out = response.getWriter(); 
		out.println("Welcome");
	   
	    Connection conn = null;
	    
	    
	    try{

	        Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); 

	        conn = DriverManager.getConnection ("jdbc:odbc:acc"); 
			   
			  
	        Statement stmt = conn.createStatement (); 
	        ResultSet rs = stmt.executeQuery ("select * from mytable where empid=?");  
	        String empid=request.getParameter("empid");
	        
	     
	        //int emprow=rs.getRow();
	      

	       while(rs.next()) 
	        {
	        	
	    	for(empid=request.getParameter("empid");;)
	    	{
	        String a=rs.getString(1);
	        out.println("Employee name: "+ a);
	        
	        int b=rs.getInt(3);
	    	out.println("Employee age: "+ b);
	    	
	    	int c=rs.getInt(4);
	    	out.println("Employee salary: "+ c);
	    	
	    	}
       
	        
	        }
	       rs.close();
	       }
	    catch(Exception e)
	    {
	    	System.out.println(e.getMessage());
	    }
	}

}

Web.xml code:

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
		<description>
		</description>
		<display-name>ServletOne</display-name>
		<servlet-name>ServletOne</servlet-name>
		<servlet-class>ServletOne</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ServletOne</servlet-name>
		<url-pattern>/ServletOne</url-pattern>
	</servlet-mapping>
	<servlet>
		<description>
		</description>
		<display-name>ServletTwo</display-name>
		<servlet-name>ServletTwo</servlet-name>
		<servlet-class>ServletTwo</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>ServletTwo</servlet-name>
		<url-pattern>/ServletTwo</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>form.html</welcome-file>

	</welcome-file-list>
</web-app>

Error that I get on the browser when I run ServletOne.java :

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
ServletOne.doPost(ServletOne.java:46)
ServletOne.doGet(ServletOne.java:15)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.29 logs.


Not able to understand the error in my code .Please help or give guidance :( . Thanks in advance

Recommended Answers

All 4 Replies

Without even looking at the code since it is not in code tags - Press the button (Code) and put your code inside the tags that will appear - I took a look at the error:

java.lang.NullPointerException
ServletOne.doPost(ServletOne.java:46)
ServletOne.doGet(ServletOne.java:15)

Try to read them from the begining. As soon as you reach a file that you created you found the source of the problem. At the file ServletOne, inside the doPost method at line 46 of the ServletOne.java file, you are trying to use an object which is null. Hence the NullPointerException. Go to that line and find what is null.

Thanks for the reply,javaAddict! My code is working now:) I hadn't entered the name and pwd inside the form tag (html code),which is why I got a NullPointerException.

Thanks for the reply,javaAddict! My code is working now:) I hadn't entered the name and pwd inside the form tag (html code),which is why I got a NullPointerException.

That is why we use validation directly on the page (with JavaScript) or do it on server side once submitted

Oh,okay :) Thankyou for letting me know. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.