在Servlet中向客户端写Cookie信息

在Servlet中向客户端写Cookie信息


应用Servlet API 中提供的Cookie类,用户把表单信息提交给Servlet后,在Servlet中获取用户请求的信息并添加到Cookie对象中,在通过HttpServletResponse对象把Cookie信息返回给客户端,然后在JSP页面中通过request内置对象来获取客户端的Cookie信息。

在JSP中使用request对象获取的是一个Cookie对象的数组,需要在循环中遍历所有Cookie对象,并通过Cookie对象的getName()方法查找所有Cookie对象的名称,然后根据找到的Cookie名称获得Cookie对象中的值。

一. 新建用户登录表单页index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.net.URLDecoder"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 在index.jsp页中读取所有客户端的Cookie,通过循环Cookie数组找到保存用户名的Cookie -->
<%
    String userName = null;     // 用于保存从Cookie中读取的用户名
    Cookie cookieArr[] = request.getCookies();      // 通过转发到客户端的Cookie,request获取客户端的所有的Cookie
    if(cookieArr!=null && cookieArr.length>0){
        for(Cookie c:cookieArr){
            if(c.getName().equals("userName")){     // 如果Cookie中有一个名为userName的Cookie
                userName = URLDecoder.decode(c.getValue(),"UTF-8");     // 将字符串解码,获得此Cookie的值
            }
        }
    }
 %>
<form action="CookieServlet" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <!-- 将获取到的用户名Cookie的值赋值给“用户名”文本框 -->
            <td><input type="text" name="name" value="<%if(userName!=null){out.print(userName);}%>"/></td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="password" name="pwd"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="登 录"/></td>
        </tr>
    </table>
</form>
</body>
</html>

二. 新建名为CookieServlet的Servlet类,在该类的doPost()方法中获取用户名信息,然后添加到Cookie对象中并保存到客户端

import java.io.IOException;

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 CookieServlet extends HttpServlet {

    /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");     // 获得用户名
        name = java.net.URLDecoder.decode(name, "UTF-8");   // 将用户名进行格式编码
        Cookie nameCookie = new Cookie("userName",name);    // 创建一个Cookie对象,并将用户名保存到Cookie对象中
        nameCookie.setMaxAge(60);   // 设置Cookie的过期之前的时间,单位为秒
        response.addCookie(nameCookie);     // 通过response的addCookie()方法将此Cookie对象保存到客户端浏览器的Cookie中
        request.getRequestDispatcher("index1.jsp").forward(request, response);  // 请求响应转发到JSP页面

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值