1. 程式人生 > >Servlet之Cookie相關操作

Servlet之Cookie相關操作

這裡主要講解cookie的新增和讀取

工程中涉及到的servlet的配置

<?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">
	<display-name></display-name>
	<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>AServlet</servlet-name>
		<servlet-class>AServlet</servlet-class>
	</servlet>
	<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>BServlet</servlet-name>
		<servlet-class>BServlet</servlet-class>
	</servlet>
	<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>CServlet</servlet-name>
		<servlet-class>CServlet</servlet-class>
	</servlet>
	<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>DServlet</servlet-name>
		<servlet-class>DServlet</servlet-class>
	</servlet>
	<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>LoginServlet</servlet-name>
		<servlet-class>LoginServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>AServlet</servlet-name>
		<url-pattern>/a</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>BServlet</servlet-name>
		<url-pattern>/b</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>CServlet</servlet-name>
		<url-pattern>/c</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>DServlet</servlet-name>
		<url-pattern>/d</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>LoginServlet</servlet-name>
		<url-pattern>/loginServlet</url-pattern>
	</servlet-mapping>

</web-app>

會話session屬性的設定與讀取

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class AServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		session.setAttribute("name", "李婉琳");
		
		request.getRequestDispatcher("b").forward(request, response);
//		response.sendRedirect("/web/b");
	}

}

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class BServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");

		System.out.println("session : "+request.getSession().getAttribute("name"));
		request.getRequestDispatcher("/index.jsp").forward(request, response);
		
	}

}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  
  <body>
  name:
     <%=session.getAttribute("name") %>
  </body>
</html>

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

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

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		Cookie cookie=new Cookie("p1", "v1");
		cookie.setMaxAge(10);
		
		response.addCookie(cookie);
		
		request.getRequestDispatcher("d").forward(request, response);
	}

}

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

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

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		Cookie[] cookies=request.getCookies();
		for(Cookie cookie:cookies){
			System.out.println("**********************************");
			System.out.println("cookie name : "+cookie.getName());
			System.out.println("cookie value : "+ cookie.getValue());
			System.out.println("----------------------------------");
		}
	}

}


通過cookie儲存登入資訊

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

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

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		Cookie cookie1=new Cookie("username", username);
		Cookie cookie2=new Cookie("password", password);
		response.addCookie(cookie1);
		response.addCookie(cookie2);
		
	}

}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
</head>

<body>
	<%
		Cookie[] cookies = request.getCookies();

		String username = "";
		String password = "";
		if (cookies != null) {
			for (Cookie cookie : cookies) {

				if (cookie.getName() != null
						&& cookie.getName().equals("username")) {
					username = cookie.getValue();
				}
				if (cookie.getName() != null
						&& cookie.getName().equals("password")) {
					password = cookie.getValue();
				}
			}
		}
	%>

	<form action="loginServlet">
		<input type="text" name="username" value="<%=username%>" /><br>
		<input type="password" name="password" value="<%=password%>" /><br>
		<input type="submit" value="提交" />
	</form>
</body>
</html>