Hi Friend,
Try the following code:
1)login.jsp:
<html>
<head>
</head>
<body>
<form name="form" method="post" action="../Authenticate">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#efefef;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2> </td></tr>
<tr>
<td><b>Login Name</b></td>
<td><input type="text" name="username" value=""></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr><td colspan=2> </td></tr>
</table>
</form>
</body>
</html>
2)Authenticate.java:
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Authenticate extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
String user=request.getParameter("username");
String pass=request.getParameter("password");
String user1="";
String pass1="";
HttpSession session = request.getSession(true);
try{
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+"'");
while(rs.next()){
user1 = rs.getString("username");
pass1 = rs.getString("password");
session.setAttribute("username",user1);
}
if((user1.equals(user))&&(pass1.equals(pass))){
response.sendRedirect("jsp/welcome.jsp");
}
else{
response.sendRedirect("jsp/login.jsp");
}
}
catch(Exception e){}
}
}
3)
<%
String user = session.getAttribute("username").toString();
%>
Welcome <%out.println(user);%>
<br>
<br>
<a href="#">Add</a><br>
<a href="#">Edit</a><br>
<a href="#">Delete</a>
Thanks