Introduction Today we will make a Cookie Demo, which is also a small technology I just learned when writing a project today. Here we will show the simplest way to implement cookies, and the use of cookies in JAVAEE. When you need to pay attention to the Demo, the source code posted here is part of my project. You don't need to understand the meaning of some of these variables, but you just need to pay attention to Cook.
Introduction Today we will make a Cookie Demo, which is also a small technology I just learned when writing a project today. Here we will show the simplest way to implement cookies, and the use of cookies in JAVAEE. When you need to pay attention to the Demo, the source code posted here is part of my project. You don't need to understand the meaning of some of these variables, but you just need to pay attention to Cook.
IntroductionToday, we will create a Cookie Demo, which is also a small technology I just learned when writing a project today. Here we will show the simplest way to implement cookies, and the use of cookies in JAVAEE.
DemoNote that the source code posted here is part of my project. You do not need to understand the meaning of some of these variables, but you only need to pay attention to the Cookie section.
Process
- Click logon on the page to link to logon action.
- Select the "two-week login free" option during logon, enter the user name and password, and click log on.
- Store Cookie
- No Logon required next time
Logon page form
Note that the action is User_login.
Action Code
/*** User login ** @ return */public String login () {/*** get response */HttpServletResponse response = ServletActionContext. getResponse (); // if the query is empty if (userDao. login (username, password) = null) {return ERROR;} // if the user has else {// put the user into session. put ("user", userDao. login (username, password); // if useCookie is not empty if (useCookie! = Null) {// create a Cookie, store the user name and password, and use & as the separator Cookie cookie = new Cookie ("userCookie", username + "&" + password ); // set the Cookie survival time (two weeks here. setMaxAge (2*7*24*60*60); // Add Cookie response. addCookie (cookie) ;}return SUCCESS ;}}
Note that useCookie is a string that is the value of a single token received from the page form. If the selection box is checked, the Cookie is saved. If this box is not selected, the Cookie is not saved.
Corresponding struts. xml configuration
/index.jsp
/login.jsp
Here is an additional method for Cookie detection when you click Login connection
/*** Check the cookie before logon ** @ return */public String cookieDetection () {if (session. get ("user ")! = Null) {return SUCCESS;} else {return LOGIN ;}}
Corresponding struts. xml configuration
/index.jsp
/login.jsp
Homepage FilterWhen will the Cookie be checked? The normal assumption is that on the homepage, check whether there are users who have "two-week login-free", so there must be something to do, two mechanisms, filters and listeners, can be thought of in general. The listener intelligently monitors the corresponding actions and then performs the operations at the same time. The filter can directly filter the request and response, and the response and request will be used in Cookie usage, so it is best to select the filter.
Import java. io. IOException; import java. util. map; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. cookie; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. strut S2.ServletActionContext; import com. webstore. dao. IUserDao; import com. webstore. imp. userImp;/*** Author: Zhou lingyu time: 2014-7-4 Description: Cookie Filter */public class CookieFilter implements Filter {/*** configuration object */protected FilterConfig config; /*** initialize the filter */public void init (FilterConfig config) {this. config = config;}/*** rewrite the doFilter method to filter all requests and replies, add or retrieve cookies */public void doFilter (ServletRequest req, ServletResponse Resp, FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) resp; Cookie [] cookies = request. getCookies (); IUserDao userDao = new UserImp (); String [] info = null; if (cookies! = Null) {for (Cookie c: cookies) {info = c. getValue (). split ("&"); if (info. length = 2) {String username = info [0]; String password = info [1]; request. getSession (). setAttribute ("user", userDao. login (username, password) ;}} chain. doFilter (request, response);}/*** destroy filter */public void destroy () {this. config = null ;}}
Filter configuration
CookieFilter
com.webstore.util.CookieFilter
CookieFilter
/index.jsp
Note that only homepage filtering is configured here.