how to login a user according to roles and page will should be same content will be different
1)index.jsp:
<a href="login.jsp">User Login</a>    <a href="adminPage.jsp">Admin Login</a>
2)login.jsp:
<html> <script> function validate(){ var username=document.form.user.value; var password=document.form.pass.value; if(username==""){ alert("Enter Username!"); return false; } if(password==""){ alert("Enter Password!"); return false; } return true; } </script> <form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();"> <table> <tr><td>Username:</td><td><input type="text" name="user"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html> <%String msg=request.getParameter("msg"); if(msg!=null){ %> <label><font color="red"><%=msg%></font></label> <% } %>
3)check.jsp:
<%@page import="java.sql.*"%> <% String user=request.getParameter("user"); String pass=request.getParameter("pass"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'"); int count=0; while(rs.next()) { count++; } if(count>0) { out.println("welcome "+user); } else { response.sendRedirect("login.jsp?msg=Invalid Username or Password"); } %>
continue..
4)adminPage.jsp:
<html> <title>Admin login Page</title> <head> <script language="JavaScript"> function isProper(string) { if (!string) return false; var iChars = "*|,\":<>[]{}`\';()@&$#%"; for (var i = 0; i < string.length; i++) { if (iChars.indexOf(string.charAt(i)) != -1) return false; } return true; } function validateForm(theForm){ if(theForm.userid.value==""){ //Please enter accountholder alert("Enter the User Name."); theForm.userid.focus(); return false; } if (isProper(theForm.userid.value) == false) { alert("Please enter a valid User Name."); theForm.userid.focus(); return false; } if(theForm.userid.value.length > 20) { alert("Maximum 20 characters allowed for 'First Name'.") theForm.userid.focus() ; return false; } if(theForm.password.value==""){ //Please enter accountholder alert("Enter the password."); theForm.password.focus(); return false; } if (isProper(theForm.password.value) == false) { alert("Please enter a valid password."); theForm.password.focus(); return false; } if(theForm.password.value.length > 20) { alert("Maximum 20 characters allowed for 'Password'.") theForm.password.focus() ; return false; } return true; } </script> </head> <body> <H1>Admin login Page</H1> <form method="POST" action="adminPageAction.jsp" onsubmit="return validateForm(this);"> <table border="1" ALIGN="LEFT" cellspacing="0" cellpadding="0" width="50"> <tr> <td width="25%"><b>User-Id:</b></td> <td width="25%"> <input type="text" name="userid" size="20"> </td> </tr> <tr> <td width="25%"><b>Password:</b></td> <td width="25%"> <input type="password" name="password" size="20"> </td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> </table> </form> </body> </html>
5)adminPageAction.jsp
<%@ page language="java" import="java.sql.*,java.io.UnsupportedEncodingException,java.security.MessageDigest,sun.misc.BASE64Encoder,sun.misc.CharacterEncoder,java.security.MessageDigest" %> <% Connection con = null; String url = "jdbc:mysql://localhost:3306/";; String db = "register"; String driver = "com.mysql.jdbc.Driver"; try{ Class.forName(driver); con = DriverManager.getConnection(url+db,"root","root"); try{ Statement st = con.createStatement(); String userid=request.getParameter("userid"); String password=request.getParameter("password"); MessageDigest d = MessageDigest.getInstance("SHA"); d.update(password.getBytes("UTF-8")); byte raw[] = d.digest(); String hash = (new BASE64Encoder()).encode(raw); System.out.println(hash); con.close(); out.println("Successfully"); } catch (SQLException s){ System.out.println("SQL statement is not executed!"); } } catch (Exception e){ e.printStackTrace(); } %>
Ads