java获取前端页面发送的cookie中的信息

package com.xalo.cookie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/getcookie")
public class GetCookieServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		//获取cookie
		Cookie cookies[] = req.getCookies();
		
		PrintWriter out = resp.getWriter();
		if(cookies==null || cookies.length == 0){
			System.out.println("没有cookie");
		}else{
			for (Cookie cookie : cookies) {
				out.write("----------------------------------------"+"
"); //获取cookie的解释内容 String comment = cookie.getComment(); out.write("comment:"+comment+"
"); //获取cookie的键 String key = cookie.getName(); out.write("key:"+key+"
"); //获取cookie的值 String value = cookie.getValue(); out.write("value:"+value+"
"); //获取cookie的有效时间。 int time = cookie.getMaxAge(); out.write("time:"+time+"
"); //获取服务器的IP对应的域名 String domain = cookie.getDomain(); out.write("domain:"+ domain+"
"); //获取有效路径 String path = cookie.getPath(); out.write("path:"+ path+"
"); //设置cookie是否加密,和https有关 // cookie.setSecure(true);//默认为fale // cookie.getSecure(); } } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }

你可能感兴趣的:(java)