1.留言簿页面:liuYan.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" %> 3 4 <%@ page import= "java.net.URLDecoder" %> 5 6 <%@ page import="java.util.*" %> 7 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 8 <html> 9 <head> 10 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 11 <title>留言薄</title> 12 </head> 13 <body> 14 <h3>留言薄</h3> 15 <% 16 Cookie[] co = request.getCookies(); 17 18 String nc = ""; 19 String ly = ""; 20 21 //ArrayList<String> ar = new ArrayList<>(); 22 23 if(co != null) 24 { 25 for(int i = 0; i < co.length; i++) 26 { 27 if(co[i].getName().equals("myco")) 28 { 29 //String mes = ""; 30 31 nc = URLDecoder.decode(co[i].getValue().split("#")[0]); 32 33 ly = URLDecoder.decode(co[i].getValue().split("#")[1]); 34 35 //ar.add(mes); 36 37 out.print("<br>昵称:" + nc +"<br> 留言:" + ly + "<br>"); 38 } 39 } 40 41 /* 42 for(int i = 0; i < ar.size(); i ++) 43 { 44 out.print("<br>昵称:" + ar.get(i).split("#") +"<br> 留言:" + ar.get(i).split("#")[1] + "<br>"); 45 } 46 */ 47 } 48 else 49 { 50 out.print("暂时没有留言"); 51 } 52 %> 53 <br> 54 <form name = "form2" action = "xianShi.jsp" method="post"> 55 请输入昵称:<input type = "text" name = "nc"><br> 56 57 请输入您的留言:<br> 58 <textarea rows="10" cols="100" name = "ly"></textarea> 59 <br> 60 <input type = "submit" value = "提交"> 61 62 </form> 63 </body> 64 </html>
2,留言处理页面, xianShi.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8" errorPage="error1.jsp"%> 3 4 <%@ page import="java.net.URLEncoder" %> 5 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>留言处理页</title> 11 </head> 12 <body> 13 <% 14 //request.setCharacterEncoding("GB18030"); 15 16 //String nc = URLEncoder.encode(request.getParameter("nc"),"UTF-8"); 17 String nc = URLEncoder.encode(new String(request.getParameter("nc").getBytes("ISO-8859-1"),"UTF-8")); 18 String ly = URLEncoder.encode(new String(request.getParameter("ly").getBytes("ISO-8859-1"),"UTF-8")); 19 20 Cookie co = new Cookie("myco",nc + "#" + ly); 21 22 co.setMaxAge(60*60*24*30); 23 24 response.addCookie(co); 25 26 //out.print(); 27 28 response.sendRedirect("liuYan.jsp"); 29 %> 30 31 32 </body> 33 </html>