JSP case _ pass Cookie value, jsp case _ cookie
JSP case _ Cookie value passing
1. Case requirements
Create a producer.
When the user enters the secondary node for the second time within 30 seconds
2. Case Analysis
Use the Cookie to pass the value and set the Cookie time, that is, 30 seconds. In the time range, enter
3. Code Analysis
Code implementation is not difficult. It mainly involves the use of Cookie APIs and a logical relationship.
4. Code
Https://blog.csdn.net/ag_nevergiveup/article/details/a.jsp:
<% @ Page import = "java.net. URLDecoder "%> <% @ page import =" java.net. URLEncoder "%> <% @ page import =" org. apache. taglibs. standard. tag. common. core. forEachSupport "%> <% @ page language =" java "import =" java. util. * "pageEncoding =" UTF-8 "%> <% request. setCharacterEncoding ("UTF-8"); Cookie [] cookis = request. getCookies (); String username = ""; String userpassword = ""; if (cookis. length> 1) {for (int I = 0; I
Register
D. jsp:
<%@page import="java.net.URLEncoder"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%request.setCharacterEncoding("utf-8");String name = request.getParameter("name");name = URLEncoder.encode(name, "utf-8");String password = request.getParameter("password");Cookie c1 = new Cookie("name",name);Cookie c2 = new Cookie("password",password);c1.setMaxAge(30);c2.setMaxAge(30);response.addCookie(c1);response.addCookie(c2);%>goto https://blog.csdn.net/ag_nevergiveup/article/details/a.jsp
5. Problem Analysis
Cookie cannot pass Chinese characters,
To pass a value, use the following code:
String s = URLEncoder (String, encoding)
Encode and then pass the value;
The value must be similar:
String s = URLDecoder (String, encoding)
Decoded to obtain the value;