通过cookies,可以保存用户的使用习惯,优化用户体验,同时能减轻服务端压力.下面说下在Servlet中cookies机制的使用

就用保存用户登录数据来举例子:

打开网页的处理Servlet:

package com.zhangwei;

import java.io.*;
import javax.servlet.http.*; public class CookiesPreCheck extends HttpServlet{ //处理get请求
public void doGet(HttpServletRequest req, HttpServletResponse res){
res.setContentType("text/html; charset=GBK");
Cookie[] cookiearray = req.getCookies();
if(cookiearray != null){
String username = null;
String password = null;
try{
PrintWriter pw = res.getWriter();
for(int i = 0; i<cookiearray.length;i++){
Cookie temp = cookiearray[i];
if(temp.getName().equals("username")){
username = new String(temp.getValue());
}
if(temp.getName().equals("password")){
password = new String(temp.getValue());
}
}
pw.print(username+password);
if(username!=null && password!= null){
HttpSession hs = req.getSession(true);
hs.setAttribute("username",username);
hs.setAttribute("password",password);
res.sendRedirect("account");
return ;
}
}catch(Exception e){
e.printStackTrace();
}
}
try{
res.sendRedirect("login");
}catch(Exception e){
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse res){ this.doGet(req,res);
}
}

主要做了一件事情,检测本地的cookies,看里面是否保存了用户信息,如果有,那么就将用户信息写到session,如果没有就跳转到登录页面.

用户信息页面主要是从会话中获取并显示用户信息

登录页面主要是实现用户登录,并且将用户账号密码通过表单传递到登录处理页面

登录处理页面:

package com.zhangwei;

import java.io.*;
import javax.servlet.http.*; public class LoginCL extends HttpServlet{ //处理get请求
public void doGet(HttpServletRequest req, HttpServletResponse res){
res.setContentType("text/html; charset=GBK");
this.doPost(req,res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res){ String username = req.getParameter("username");
String passwd = req.getParameter("passwd");
//验证,并且将用户数据写入session和cookies
try{
HttpSession hs = req.getSession(true);
hs.setAttribute("username",username);
hs.setAttribute("password",passwd);
Cookie c = new Cookie("username",username);
Cookie e = new Cookie("password",passwd);
//不设置有效时间的话cookies就不会被保存
c.setMaxAge(30);
e.setMaxAge(30);
//res.sendRedirect("account");
res.addCookie(c);
res.addCookie(e);
}
catch (Exception e){
e.printStackTrace();
}
}
}

用户处理页面主要做的事情是验证用户信息,如果通过,就将用户信息写入session和cookies,然后跳转到用户页面(当然也可以只写入cookies,然后跳转到根目录)

Servlet-cookies机制的更多相关文章

  1. servlet运行机制、Request内置对象和服务器端跳转

    servlet运行机制: 当发送一个请求到服务器的时候,容器(Tomcat)会判断该路径属于哪一个 Servlet 进行处理,Servlet 有一个抽象父类“HttpServlet”,这个类是一个模板 ...

  2. Yii2.0 Cookies机制和使用方法

    在实际的项目开发过程中,用到了Yii2.0 Cookies机制!但是遇到一个十分奇葩的问题,同一个YII框架,backend下Cookies能够正常存储于客户端,但是frontend始终不行.文章的最 ...

  3. JavaWeb之servlet管理机制

    一.什么是Servlet 简单的说,浏览器发出请求到tocat服务器,服务器就会初始化一个servlet实例(servlet采取生命托管的方式实现单例,不存在时才会创建实例),servlet示例会启动 ...

  4. Google考虑抛弃Cookies机制

    根据华尔街日报的报道,Google 正在考虑抛弃古老的浏览器 cookies 来追踪用户信息的机制.作为替代,Google 将开发一种「个人匿名标识机制」.Google 早前已经计划在 IE 和 iP ...

  5. servlet运作机制

    最近研究zipkin,在研究客户端brave的时候,才算开始理解servlet了.    servlet只是tomcat被实例化一次:    之后每次访问其实都是对同一个servlet示例操作:所以, ...

  6. Servlet Cookies

    Cookie是在多个客户端请求之间持久存储的一小段信息. Cookie具有名称,单个值和可选属性,例如注释,路径和域限定符,生存周期和版本号. Cookie工作原理 默认情况下,每个请求都被视为新的请 ...

  7. Servlet基础知识(三)—— 会话机制Session,Session和Cookie的异同

    Servlet会话机制: Http是一种无状态协议,它是无记忆的.也就是说,服务器不会保存用户的任何信息,当同一用户再次去访问时,服务器是不认识你的,它还是会建立新的连接. 但有时候我们需要服务器保留 ...

  8. tomcat中Servlet的工作机制

    在研究Servlet在tomcat中的工作机制前必须先看看Servlet规范的一些重要的相关规定,规范提供了一个Servlet接口,接口中包含的重要方法是init.service.destroy等方法 ...

  9. Servlet的Cookies处理

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/cookies-handling.html: Cookies是存储在客户端计算机上的文本文件,用于 ...

随机推荐

  1. (转载)String.IsNullorEmpty()方法的使用

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  2. Mobile data

    1.Consume REST web services from app 2.De-serialize JSON into an in-memory object collection 3.Save ...

  3. github仓库的克隆、修改、上传方法(图)

  4. AlertDialog使用时遇到问题

    1.其内de子控件高宽度为0? AlertDialog.Builder builder = new AlertDialog.Builder(StoryActivity.this); View view ...

  5. [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字

    Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...

  6. android-ImageView及其子类

    一.知识概要 ImageView继承自View,能显示任何Drawable对象; ImageView支持的常用XML属性及相关方法: android:adjustViewBounds    设置Ima ...

  7. Hadoop.2.x_简单的测试文件读取与上传

    代码如下, 后备参考: package com.bigdata.hadoop.hdfs; import java.io.File; import java.io.FileInputStream; im ...

  8. JS中的window.setTimeout()详解

    相关用法: setTimeout (表达式,延时时间)setInterval (表达式,交互时间)其中延时时间/交互时间是以豪秒为单位的(1000ms=1s) setTimeout 在执行时,是在载入 ...

  9. CKPT进程工作机制

    CKPT进程工作示意图 2.CKPT进程工作机制 检查点进程被触发的条件为: a> 当发生日志组切换时: b>  用户提交了事务时(commit): c>  Redo log buf ...

  10. 采用Hibernate框架的研发平台如何能够真正兼容Oracle和sqlServer数据库

    都说Hibernate框架的使用可以很容易的让你的研发平台支持多种不同类型的数据库,但实践表明,这里的“容易”,是相对的. 想让研发平台支持多种数据库,并不是一件简单的事,也可以这么说:并不是只要使用 ...