Login With Session and Database.
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class Login extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
try{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("name");
String p=request.getParameter("pass");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","navjyoti","jyoti7");
System.out.println(""+con);
PreparedStatement pst=con.prepareStatement("select * from employee where password=?");
pst.setString(1,p);
ResultSet rs=pst.executeQuery();
if(rs.next())
{
HttpSession ses=request.getSession(true);
ses.setAttribute("user","p");
out.println("id:'"+ses.getId()+"'");
out.println("id:'"+ses.getAttributeNames()+"'");
out.println("id:'"+ses.getCreationTime()+"'");
out.println("id:'"+ses.getLastAccessedTime()+"'");
//out.println("'"+ses.getValue('user')+"'");
out.println("welcome:'"+n+"'");
RequestDispatcher rd=request.getRequestDispatcher("main.html");
rd.include(request,response);
}
else
{
out.println("invalid username&password"+n);
RequestDispatcher rd=request.getRequestDispatcher("index.html");
rd.include(request,response);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}