Cookie can not save Chinese solution

A.jsp it is assumed that the client, b.jsp client, c.jsp a server

To do now is submit a.jsp client page form information to the server c.jsp, c.jsp then save the form information to the cookie, and forwarded or redirected to b.jsp client, so

We reached the end of the service generated cookie saved to the role of the client, but will complain when the Chinese input saved cookie

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.net.URLDecoder"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户端a页面</title>
</head>
<body>
    <%
        String uname="";
        Cookie[] cookies = request.getCookies();
        if(cookies!= for)
        Zero(Cookie cookie:cookies){
            if("uname".equals(cookie.getName())){
                //客户端获取cookie 后 解  码
                uname= URLDecoder.decode(cookie.getValue(), "UTF-8");
            }
        }
    %>
    <form action="./c.jsp" method="get">
        姓名:<input type="text" name = "uname" value="<%=uname %>"/><br/>"<the INPUT of the type =
        Password:password " name = " UPSD " /> a 
        <INPUT type = " Submit " value = " submit " /> 
        <! - send a message to the server, then the server returns to the client cookie stored -> 
    </ form> 
        
</ body> 
</ HTML>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户端b页面</title>
</head>
<body>
    c已经将cookie给我了
</body>
</html>
<% @ Page Language = " Java " contentType = " text / HTML; charset = UTF-. 8 " 
    the pageEncoding = " UTF-. 8 " Import = "java.net.URLEncoder"%> 
<! DOCTYPE HTML> 
<HTML> 
<head > 
<Meta charset = " UTF-8 " > 
<title> server c page </ title> 
</ head> 
<body> 
    I c server pages, I want to save the cookie, then forwarded back to the b client page <br /> 
    <% 
        Request.setCharacterEncoding ( " UTF-. 8 " );
        String name = request.getParameter("uname"); 
        String PSD = request.getParameter ( " UPSD " );
         cookies C1 = new new cookies ( "the uname", the URLEncoder.encode (name, "UTF-. 8"));
         // ck.setPath ( "/");                  / / set cookie path to the file contains only this path (to the path prefix) can access this cookie 
        c1.setMaxAge (Integer.MAX_VALUE);             // set the cookie maximum storage time 
        response.addCookie (c1); 
        cookie c2 = new new cookies ( " UPSD " , PSD);
         OUT .print (c1.getName () + " ================ " +  c1.getValue ());
        Response.addCookie(c1);
        response.addCookie(c2);
        response.sendRedirect("./b.jsp"); 
    %>
</body>
</html>

 

Guess you like

Origin www.cnblogs.com/19322li/p/11967966.html