在网上搜一些方法 都不行 比如 存取时URLEncoder.encode进行编码 读取时URLDecoder.decode进行解码!!!
附上源码 希望大家能帮帮忙 最好能把解决的方案修改到源码上 谢谢大家!!!
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.net.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String welcome = "第一次访问";
String[] info = new String[] {"", "", ""};
Cookie[] cook = request.getCookies();
if (cook != null) {
for (int i = 0; i < cook.length; i++) {
if (cook.getName().equals("mrCookInfo")) {
info = cook.getValue().split("#");
welcome = ", 欢迎回来!";
}
}
}
info[0] = URLDecoder.decode(info[0], "gbk"); //对读取的内容进行解码
%>
<%=info[0] + welcome%>
<form action="show.jsp" method="post">
<ul style="line-height: 23px">
<li>
姓 名:
<input name="name" type="text" value="<%=info[0]%>" />
</li>
<li>
出生日期:
<input name="birthday" type="text" value="<%=info[1]%>" />
</li>
<li>
邮箱地址:
<input name="mail" type="text" value="<%=info[2]%>" />
</li>
<li>
<input type="submit" value="提交" />
</li>
</ul>
</form>
</body>
</html>
show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.net.URLDecoder" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<%
String name = URLEncoder.encode(request.getParameter("name"),"gbk"); //存取时进行编码
String birthday = URLEncoder.encode(request.getParameter("birthday"),"gbk");
String mail = URLEncoder.encode(request.getParameter("mail"),"gbk");
Cookie myCook = new Cookie("mrCookInfo", name+"#"+birthday+"#"+mail);
myCook.setMaxAge(60*60*24*365); //设置cookie有效期
response.addCookie(myCook);
name = URLDecoder.decode(name,"gbk"); //读取时进行解码
birthday = URLDecoder.decode(birthday,"gbk");
mail = URLDecoder.decode(mail,"gbk");
%>
表单提交成功
<ul style="line-height:24px">
<li>姓名:<%=name %></li>
<li>出生日期:<%=birthday %></li>
<li>电子邮箱:<%=mail %></li>
<li><a href="index.jsp">返回</a></li>
</ul>
</body>
</html> |
|