There are numerous no. of reason why we would want, a simple "remember me" (checkbox based) functionality in JSP/Servlet based application.
I for one, would want this just.. so that i can display a jsp page based on known/anonymous user..
To implement this we need 3 file as follows:
1. Default.html
2. User_Serv.java
3. View.jsp
1. Default.html
Note - above Default.html and below User_Serv.java obviously needs mapping in web.xml
User_Serv.java
View.jsp
I for one, would want this just.. so that i can display a jsp page based on known/anonymous user..
To implement this we need 3 file as follows:
1. Default.html
2. User_Serv.java
3. View.jsp
1. Default.html
<form action="RememberMe_Serv" method="post">
Please enter your name:<input name="name" type="text" />
Remember Me<input name="RememberMe" type="checkbox" value="RemMe" />
<input type="submit" />
</form>
Note - above Default.html and below User_Serv.java obviously needs mapping in web.xml
User_Serv.java
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//we get "name" parameter from html
String name = request.getParameter("name");
//we check, if "RememberMe" parameter is not null,
//then set above "name" value accordingly
if(request.getParameter("RememberMe") != null){
Cookie c = new Cookie("cookieName", name);
c.setMaxAge(5);
response.addCookie(c);
}else{
Cookie c = new Cookie("cookieName", "anonymous User");
c.setMaxAge(5);
response.addCookie(c);
}
//we forward the altered response object to "View.jsp"
RequestDispatcher view = request.getRequestDispatcher("View.jsp");
view.forward(request, response);
}
View.jsp
//we call set cookie with its name(i.e is cookieName)
//and pass "value" attribute
Hello.. ${cookie.cookieName.value}
2 comments:
i need full program
You can do pull from following github repo.
http://goo.gl/khwe3
Post a Comment