赞
踩
- package test09_2Servlet;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class EncodeServlet extends HttpServlet {
-
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
-
- }
-
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- //设置响应的编码格式
- response.setContentType("text/html;charset=UTF-8");
- //设置请求的编码格式
- request.setCharacterEncoding("UTF-8");
- //获取输出的字符流
- PrintWriter out = response.getWriter();
- //获取用户提交的用户名
- String name = request.getParameter("name");
- //cookie的name如果是中文进行url编码
- name = URLEncoder.encode(name,"UTF-8");
- //创建cookie,保存用户名称到vlue中
- Cookie ck = new Cookie("ckname",name);
- //设置cookie过期时间为60分钟
- ck.setMaxAge(60*60);
- //设置cookie可以访问的路径为项目下的组件都可以访问
- ck.setPath(request.getContextPath());
- //保存cookie到本地
- response.addCookie(ck);
-
- //读取cookie,输出cookie信息。
- Cookie[] cc = request.getCookies();
- if(cc!=null){
- for(Cookie c:cc){
- String names = c.getName(); //输出cookie的name
- String value = c.getValue();//输出cookie的key
- value = URLDecoder.decode(value, "UTF-8");
- /*
- * 客户端第一次登陆后,服务器端传输cookie并写到客户端。
- 第二次登陆,request请求传给服务端,但在这个request中是不包含maxage值的,所以服务端取到的值还是默认值-1。
- 也就是说设置完maxage值后,maxage不会再被服务端读取和修改了。这一过程由浏览器完成,浏览器判断maxage的值,
- 从而判断cookie是否过期。
- */
- int maxAge = c.getMaxAge();
- //读取path永远是null。对于cookie来说,path是受保护的
- String path = c.getPath();
-
- out.print("<hr/>name:"+names+",value:"+value+",maxAge:"+maxAge+",path:"+path);
- }
- }
-
-
- }
-
- }
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
-
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
-
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
-
- <body>
- This is my JSP page. <br>
- <form action="EncodeServlet" method="post">
- <input type="text" name="name"/><br/>
- <input type="submit" value="提交"><br/>
-
- </form>
- </body>
- </html>
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="3.0"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
- <servlet>
- <description>This is the description of my J2EE component</description>
- <display-name>This is the display name of my J2EE component</display-name>
- <servlet-name>EncodeServlet</servlet-name>
- <servlet-class>test09_2Servlet.EncodeServlet</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>EncodeServlet</servlet-name>
- <url-pattern>/EncodeServlet</url-pattern>
- </servlet-mapping>
-
- </web-app>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。