This is my login JSP. And its working - means: values are going to servlet.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login || Dreamer</title>
</head>
<style>
h1{
color:chocolate;
}
label{
color:royalblue;
font-family:Arial;
font-size: 20px;
}
#ms{
color:green;
font-size: 20px;
}
#ms2{
color:red;
font-size: 20px;
}
</style>
<body>
<center>
<h1>Login Page</h1>
</center>
<br>
<br>
<center>
${error}
<div id="ms">
</div>
<br>
<form action="Controller" method="post">
<tr> <label >
Username:
<label><input type="text" placeholder="Example@domain.com" name="username" />
</tr><br><br>
<tr>
<label >
password:
<label><input type="password" placeholder="Password" name="password" />
</tr>
<br><br>
<input type="checkbox" name="remember" />Remember me<label>
<br><br>
<input type="submit" value="Login"/>
</form>
<a href="register.jsp">Create new User</a>
</center>
</body>
</html>
This is my servlet .
public class Controller extends HttpServlet {
String uname="root";
String pass="root";
String url="jdbc:mysql:///e_learning";
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String action=request.getParameter("action");
HttpSession session=request.getSession();
if(action==null){
Account account=checkCookie(request);
if(action==null)
if(account==null)
request.getRequestDispatcher("login.jsp").forward(request, response);
else{
ConnectionTest ct=new ConnectionTest();
Connection conn=ct.getConn();
try{
psmt=conn.prepareStatement(qry);
psmt.setString(1,account.getUsername());
psmt.setString(2,account.getPassword());
rs=psmt.executeQuery();
if(rs.next()){
out.print("yes outer");
session.setAttribute("username", account.getUsername());
request.getRequestDispatcher("main.jsp").forward(request, response);
}else{
request.setAttribute("error", "Account's Invalid");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}catch(Exception e){
}
}
}else{
if(action.equals("logout")){
session.removeAttribute("username");
Cookie[] cookies=request.getCookies();
for(Cookie ck: cookies){
if(ck.getName().equals("username")){
ck.setMaxAge(0);
response.addCookie(ck);
}
if(ck.getName().equals("password")){
ck.setMaxAge(0);
response.addCookie(ck);
}
}
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
private Account checkCookie(HttpServletRequest request){
Cookie[] cookies=request.getCookies();
Account account=null;
if(cookies==null)
return null;
else{
String username="",password="";
for(Cookie ck: cookies){
if(ck.getName().equals("usernamec"));
username=ck.getValue();
if(ck.getName().equals("passwordp"));
password=ck.getValue();
}
if(!username.isEmpty()&& !password.isEmpty())
account=new Account(username,password);
}
return account;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String action=request.getParameter("action");
HttpSession session=request.getSession();
if(action==null){
String username=request.getParameter("username");
String password=request.getParameter("password");
boolean remember=request.getParameter("remember")!=null;
**This is the Connection class i have write the connection string here and I am making the object of this class then accessing the method of connection here**
ConnectionTest cn=new ConnectionTest();
Connection con=cn.getConn();
try{
psmt=con.prepareStatement(qry);
psmt.setString(1, username);
psmt.setString(2, password);
rs=psmt.executeQuery();
if(rs.next()){
out.print("yessssss");
session.setAttribute("username",username);
if(remember){
Cookie cku=new Cookie("usernamec", username);
cku.setMaxAge(3600);
response.addCookie(cku);
Cookie ckp=new Cookie("passwordp", password);
cku.setMaxAge(3600);
response.addCookie(ckp);
}
request.getRequestDispatcher("main.jsp").forward(request, response);
}else{
request.setAttribute("error", "Acchhhhhhount's Invalid");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}catch(Exception e){
}
}else{
}
}
This is sending the cookies to browser. But its not remembering the login even though I have checked the "Remember me" check box. Can any one tell me where I am wrong in this code.