Servlet-cookies机制
通过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机制的更多相关文章
- servlet运行机制、Request内置对象和服务器端跳转
servlet运行机制: 当发送一个请求到服务器的时候,容器(Tomcat)会判断该路径属于哪一个 Servlet 进行处理,Servlet 有一个抽象父类“HttpServlet”,这个类是一个模板 ...
- Yii2.0 Cookies机制和使用方法
在实际的项目开发过程中,用到了Yii2.0 Cookies机制!但是遇到一个十分奇葩的问题,同一个YII框架,backend下Cookies能够正常存储于客户端,但是frontend始终不行.文章的最 ...
- JavaWeb之servlet管理机制
一.什么是Servlet 简单的说,浏览器发出请求到tocat服务器,服务器就会初始化一个servlet实例(servlet采取生命托管的方式实现单例,不存在时才会创建实例),servlet示例会启动 ...
- Google考虑抛弃Cookies机制
根据华尔街日报的报道,Google 正在考虑抛弃古老的浏览器 cookies 来追踪用户信息的机制.作为替代,Google 将开发一种「个人匿名标识机制」.Google 早前已经计划在 IE 和 iP ...
- servlet运作机制
最近研究zipkin,在研究客户端brave的时候,才算开始理解servlet了. servlet只是tomcat被实例化一次: 之后每次访问其实都是对同一个servlet示例操作:所以, ...
- Servlet Cookies
Cookie是在多个客户端请求之间持久存储的一小段信息. Cookie具有名称,单个值和可选属性,例如注释,路径和域限定符,生存周期和版本号. Cookie工作原理 默认情况下,每个请求都被视为新的请 ...
- Servlet基础知识(三)—— 会话机制Session,Session和Cookie的异同
Servlet会话机制: Http是一种无状态协议,它是无记忆的.也就是说,服务器不会保存用户的任何信息,当同一用户再次去访问时,服务器是不认识你的,它还是会建立新的连接. 但有时候我们需要服务器保留 ...
- tomcat中Servlet的工作机制
在研究Servlet在tomcat中的工作机制前必须先看看Servlet规范的一些重要的相关规定,规范提供了一个Servlet接口,接口中包含的重要方法是init.service.destroy等方法 ...
- Servlet的Cookies处理
以下内容引用自http://wiki.jikexueyuan.com/project/servlet/cookies-handling.html: Cookies是存储在客户端计算机上的文本文件,用于 ...
随机推荐
- [LintCode] Median of Two Sorted Arrays 两个有序数组的中位数
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- [LintCode] Number of Islands 岛屿的数量
Given a boolean 2D matrix, find the number of islands. Notice 0 is represented as the sea, 1 is repr ...
- 自己做的一个小demo
上图: 主段代码: <script type="text/javascript"> var getRandomColor = function(){ return (f ...
- javax.servlet.jsp cannot be resolved to a type
参考链接 :http://www.tuicool.com/articles/7Njmqy
- Oracle中建表和指定表空间
--建一个表create table HH2( tid number primary key ,--主键设定 tname varchar2(20) ); --删除表drop table HH; --表 ...
- 本地数据库(SQL Server)远程连接服务器端服务器
本地数据库(SQL Server 2012) 连接外网服务器的数据库,外网的服务器端需要做如下配置: 1. 首先是要打开 数据的配置管理工具 2. 配置相关的客户端协议,开启TCP/IP 3. 数据库 ...
- c#中文转全拼或首拼
参考:http://www.jb51.net/article/42217.htmhttp://blog.csdn.net/cstester/article/details/4758172 Chines ...
- socket详解
<?php /* * * socket主要翻译为套接字 * socket_accept — Accepts a connection on a socket * 接受一个socket链接 * s ...
- flush vs ob_flush
刷新PHP程序的缓冲,而不论PHP执行在何种情况下(CGI ,web服务器等等).该函数将当前为止程序的所有输出发送到用户的浏览器. flush() 函数不会对服务器或客户端浏览器的缓存模式产生影响. ...
- JS中构造函数与函数
//构造函数中,如果返回的是一个对 象,那么就保留原意. 如果返回的是非对象,比如数字.布尔和字符串,那么就返回 this,如果没有 return 语句,那么也返回this. var myFun1 = ...