servlet to database login example
843842Mar 24 2008 — edited Mar 25 2008Hi,
I wrote a small login example program by using servlets.This application is compiled without errors, but when i deploy it in apache-tomcat server it gives wrong output.My program is:
1)
import javax.servlet.*;
import java.sql.*;
import javax.sql.*;
import oracle.jdbc.pool.*;
import java.io.*;
import java.lang.reflect.*;
public class Loginservlet extends GenericServlet
{
String userid;
private Connection con;
public void init()throws ServletException
{
System.out.println("INIT");
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.100:1521:ssanthu","scott","scott");
}//try
catch(Exception e)
{
e.printStackTrace();
throw new ServletException("Intialization failed,unable to get DB connection");
}//catch
}//init
public void service(ServletRequest req,ServletResponse res)throws IOException,ServletException
{
System.out.println("in service");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
PreparedStatement ps=null;
try
{
String userid=req.getParameter("userid");
String pass=req.getParameter("pass");
if(userid==null||userid.equals("")||pass==null||pass.equals(""))
{
out.println("<html><body>");
out.println("<i>UserName/Password are empty,they cannot be empty </i>");
out.println("</body></html>");
return;
}//if
ps=con.prepareStatement("select * from udetails where userid=2 and pass=abc");
ps.setString(1,userid);
ps.setString(2,pass);
ResultSet rs=ps.executeQuery();
out.println("<html><body>");
if(rs.next())
out.println("valid user");
else
out.println("invalid user");
out.println("</body></html>");
}//try
catch(Exception e)
{
out.println("unable to process the request please try later");
}//catch
finally {
try{ ps.close(); }
catch(Exception e) {}
}//finally
}//service
public void destroy()
{
System.out.println("In destroy");
try { con.close();
}
catch(Exception e) {
}
}//destroy
}//class
2)I created a table udetails (userid,pass)
After deploying my application i got the message:
"UserName/Password are empty,they cannot be empty "
can any one suggest me for my problem.