Switch to full style
What's behind JSP & Servlets
Post a reply

develop a login page- login servlet - ServletContext

Sun Feb 10, 2013 5:52 am

i m trying to develop a login page and following is the coding of the login servlet but when i try to run this it throws a NullPointerException .i dont know why but this statement ServletContext sc= getServletContext(); is throwing this exception can anyone tell me the reason for this.
thanking u in anticipation
java code
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class LoginEx extends HttpServlet{
Connection conn;

public void init(ServletConfig s) throws ServletException
{
try
{

System.out.println("hello1");
ServletContext sc=getServletContext();
System.out.println("hello1");
String st=sc.getInitParameter("dbclass");

Class.forName(st);
conn = DriverManager.getConnection(sc.getInitParameter("dbconn"),sc.getInitParameter("dbuser"),sc.getInitParameter("dbpass"));

}
catch(Exception cn)
{
System.out.println("error"+cn);throw new ServletException();
}
}
protected void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{

resp.setContentType("text/html");
PrintWriter pw = resp.getWriter();
String n=req.getParameter("name");
String p=req.getParameter("pass");
try{

Statement st=conn.createStatement();
ResultSet rs= st.executeQuery("select * from login where ename="+n+"and pass="+p);
while(rs.next())
{
pw.println("<html><body>welcome</body></html>");
}
}
catch(Exception e){
System.out.println("error"+e);
}
pw.close();

}



}




Post a reply
  Related Posts  to : develop a login page- login servlet - ServletContext
 Login page using sessions and mysql in php     -  
 track session from login page-J2EE project     -  
 login using jsp     -  
 Ajax Login     -  
 ASP.NET 2.0 Login Controls     -  
 php login with session     -  
 login using Ajax     -  
 Login using jsp,Servlets and Database following MVC     -  
 Cookie based login in php     -  
 How to Show a message before the login ?     -