package test; 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; publicclass CookieTest extends HttpServlet ...{ /** *//** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @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 */ publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ Cookie cookieName =new Cookie("name","tom"); cookieName.setMaxAge(3600); Cookie cookiePassword =new Cookie("password","123456"); cookiePassword.setMaxAge(3600); response.addCookie(cookieName); response.addCookie(cookiePassword); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** *//** * 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 */ publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ this.doGet(request, response); } }
真正自己编写的只有几句话: Cookie cookieName = new Cookie("name","tom"); cookieName.setMaxAge(3600); Cookie cookiePassword = new Cookie("password","123456"); cookiePassword.setMaxAge(3600);
package test; 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; publicclass GetCookie extends HttpServlet ...{ /** *//** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @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 */ publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ Cookie[] cookies = request.getCookies(); String name =""; String password =""; for (int i =0; i < cookies.length; i++) ...{ if (cookies[i].getName().equals("name")) ...{ name = cookies[i].getValue(); } if (cookies[i].getName().equals("password")) ...{ password = cookies[i].getValue(); } } response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println("<br>"); out.println("name:"+ name); out.println("<br>"); out.println("password:"+ password); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** *//** * 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 */ publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException ...{ this.doGet(request, response); } }
主要语句: Cookie[] cookies = request.getCookies(); String name = ""; String password = "";
for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("name")) { name = cookies[i].getValue(); } if (cookies[i].getName().equals("password")) { password = cookies[i].getValue(); } }