当前位置:K88软件开发文章中心网站服务器框架Servlet → 文章内容

Servlet Cookies 处理

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-25 14:29:15

" <li><b>名字</b>:" + request.getParameter("first_name") + "\n" + " <li><b>姓氏</b>:" + request.getParameter("last_name") + "\n" + "</ul>\n" + "</body></html>"); }} 编译上面的 Servlet HelloForm,并在 web.xml 文件中创建适当的条目,最后尝试下面的 HTML 页面来调用 Servlet。 <html><body><form action="HelloForm" method="GET">名字:<input type="text" name="first_name"><br />姓氏:<input type="text" name="last_name" /><input type="submit" value="提交" /></form></body></html> 保存上面的 HTML 内容到文件 hello.htm 中,并把它放在 <Tomcat-installation-directory>/webapps/ROOT 目录中。当您访问 http://localhost:8080/Hello.htm 时,上面表单的实际输出如下所示: 尝试输入名字和姓氏,然后点击"提交"按钮,名字和姓氏将显示在屏幕上,同时会设置 firstName 和 lastName 这两个 Cookies,当下次您按下提交按钮时,会将这两个 Cookies 传回到服务器。 下一节会讲解如何在 Web 应用程序中访问这些 Cookies。 通过 Servlet 读取 Cookies 要读取 Cookies,您需要通过调用 HttpServletRequest 的 getCookies( ) 方法创建一个 javax.servlet.http.Cookie 对象的数组。然后循环遍历数组,并使用 getName() 和 getValue() 方法来访问每个 cookie 和关联的值。 实例 让我们读取上面的实例中设置的 Cookies // 导入必需的 java 库import java.io.*;import javax.servlet.*;import javax.servlet.http.*; // 扩展 HttpServlet 类public class ReadCookies extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie = null; Cookie[] cookies = null; // 获取与该域相关的 Cookies 的数组 cookies = request.getCookies(); // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading Cookies Example"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" ); if( cookies != null ){ out.println("<h2>查找 Cookies 名称和值</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; out.print("名称:" + cookie.getName( ) + ","); out.print("值:" + cookie.getValue( )+" <br/>"); } }else{ out.println( "<h2 class="tutheader">未找到 Cookies</h2>"); } out.println("</body>"); out.println("</html>"); }} 编译上面的 Servlet ReadCookies,并在 web.xml 文件中创建适当的条目。如果您已经设置了 first_name cookie 为 "John",last_name cookie 为 "Player" ,尝试运行 http://localhost:8080/ReadCookies,将显示如下结果: 查找 Cookies 名称和值 名称:first_name,值:John 名称:last_name,值:Player 通过 Servlet 删除 Cookies 删除 Cookies 是非常简单的。如果您想删除一个 cookie,那么您只需要按照以下三个步骤进行: 读取一个现有的 cookie,并把它存储在 Cookie 对象中。 使用 setMaxAge() 方法设置 cookie 的年龄为零,来删除现有的 cookie。 把这个 cookie 添加到响应头。 实例 下面的例子将删除现有的名为 "first_name" 的 cookie,当您下次运行 ReadCookies 的 Servlet 时,它会返回 first_name 为空值。 // 导入必需的 java 库import java.io.*;import javax.servlet.*;import javax.servlet.http.*; // 扩展 HttpServlet 类public class DeleteCookies extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Cookie cookie = null; Cookie[] cookies = null; // 获取与该域相关的 Cookies 的数组 cookies = request.getCookies(); // 设置响应内容类型 response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Delete Cookies Example"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n" ); if( cookies != null ){ out.println("<h2>Cookies 名称和值</h2>"); for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; if((cookie.getName( )).compareTo("first_name") == 0 ){ cookie.setMaxAge(0); response.addCookie(cookie); out.print("已删除的 cookie:" + cookie.getName( ) + "<br/>"); } out.print("名称:" + cookie.getName( ) + ","); out.print("值:" + cookie.getValue( )+" <br/>"); } }else{ out.println( "<h2 class="tutheader">No cookies founds</h2>"); } out.println("</body>"); out.println("</html>"); }} 编译上面的 Servlet DeleteCookies,并在 web.xml 文件中创建适当的条目。现在运行 http://localhost:8080/DeleteCookies,将显示如下结果: Cookies 名称和值 已删除的 cookie:first_name 名称:first_name,值:John 名称:last_name,值:Player 现在尝试运行 http://localhost:8080/ReadCookies,它将只显示一个 cookie,如下所示: 查找 Cookies 名称和值 名称:last_name,值:Player 您可以手动在 Internet Explorer 中删除 Cookies。在"工具"菜单,选择"Internet 选项"。如果要删除所有的 Cookies,请按"删除 Cookies"。

上一页  [1] [2] 


Servlet Cookies 处理