Jsp LOGIN with Session management

Login page in jsp with session


login.html
<form action="authlogin.jsp" method="post">

<input type="text" name="uname" >
<input type="text" name="pass" >
<input type="submit">
</form>



authlogin.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import ="java.sql.*" %>
<%
try{
    String userid = request.getParameter("uname");  
    String pwd = request.getParameter("pass");
 
 
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/inventory", "root","root");

    Statement st = con.createStatement();
 
    String query="select * from customer where user_Name=? and pass=?";
    PreparedStatement ps= con.prepareStatement(query);
ps.setString(1, userid);
ps.setString(2, pwd);

ResultSet rs;
rs = st.executeQuery("select * from customer where user_Name='" + userid + "' and pass='" + pwd + "'");  
 
    if (rs.next()) {
    session.setAttribute("userid", userid);
    response.sendRedirect("index.jsp");

    }
   else {
  String q="select * from staff where user_Name=? and password=?";
   PreparedStatement s= con.prepareStatement(q);
s.setString(1, userid);
s.setString(2, pwd);

ResultSet rs1;
rs1 = st.executeQuery("select * from staff where user_Name='" + userid + "' and password='" + pwd + "'");  

if(rs1.next()){
session.setAttribute("userid", userid);
response.sendRedirect("../staff/staff_profile.jsp");
}
else{
  response.sendRedirect("#mymodel");
  }
   }
}

catch(Exception e){
out.println("exception is=" +e);
}
%>

jsp login easily.
how to work Prepared statement in jsp.
how to session work

Post a Comment

0 Comments

Recent in Recipes

3/Food/post-list