会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话。
1)每个用户在使用浏览器与服务器进行会话的过程中,不可避免各自会产生一些数据,服务器要想办法为每个用户保存这些数据
2)例如:多个用户点击超链接通过一个servlet各自购买了一个商品,服务器应该想办法把每一个用户购买的商品保存在各自的地方,以便于这些用户点结帐servlet时,结帐servlet可以得到用户各自购买的商品为用户结帐。
1.思考问题 - 抛砖引玉
大家在访问某个网站的时候,是否能看到提示你上次登录网站的时间,而且要注意的是不同用户上次登录的时间肯定是不一样的,这是怎么实现的?
大家在访回某个购物网站的时候,是否能看到提示你曾经浏览过的商品,不同用户浏览过的商品肯定不一样, 这是怎么实现的?
2.常用cookie技术完成
Cookie(小甜饼)是客户端技术,服务器把每个用户的数据以cookie的形式写给用户各自的浏览器。当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去。这样,web资源处理的就是用户各自的数据了。
1. Cookie是服务器在客户端保存用户的信息,比如登录名,密码等, 就可以以cookie方式保存。
2. Cookie信息就像是小甜饼(cookie中文)一样, 数据量并不大,服务器端在需要的时候可以从客户端读取
3.再次说明:cookie数据是保存在浏览器的。
1.保存.上次登录时间等信息
2.保存用户名,密码,在一定时间不用重新登录
3. 网站的个性化,比如定制网站的服务,内容。
1. Cookie有点象张表(K-V), 分两列,一个是名字, 一个是值,数据类型都是String
2.如何创建一个Cookie(在服务端创建的)
Cookie c= new Cookie( String name,String val);
c.setMaxAge:();/ /保存时间
3.如何将一个Cookie添加到客户端
response.addCookie(c);
4.如何读取cookie(在服务器端读取到cookie信息)
request.getCookies( );
创建
@WebServlet(name = "CreateCookieServlet",urlPatterns = "/createCookie")
public class CreateCookieServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
System.out.println("be invoke...");
//1. 创建一个Cookie对象
//1) username 该cookie的名字 是唯一, 可以理解成是key
//2) hsp : 该cookie的值
//3) 可以创建多个cookie,老师就创建了一个
//4) 这是cookie在服务器端, 还没有到浏览器
Cookie cookie = new Cookie("username", "yinhai");
Cookie cookie2 = new Cookie("email", "[email protected]");
response.setContentType("text/html;charset=utf-8");
//2. 将cookie发送给浏览器, 让浏览器将该cookie保存
response.addCookie(cookie);
response.addCookie(cookie2);
PrintWriter writer = response.getWriter();
writer.println("创建cookie成功~
");
writer.flush();
writer.close();
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request,response);
}
}
读取
@WebServlet(name = "ReadCookiesServlet", urlPatterns = "/readCookies")
public class ReadCookiesServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("be invoke readCookiesServlet");
//1. 通过request对象读取cookie信息
Cookie[] cookies = request.getCookies();
//2. 遍历cookie
if (cookies != null && cookies.length != 0) {
for (Cookie cookie : cookies) {
System.out.println("cookie name= " + cookie.getName()
+ " value= " + cookie.getValue());
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doPost(request, response);
}
}
其中的JESSIONID 相当于用于区分浏览器 便于tomcat去区分是哪个浏览器,标识一次会话
1.读取对应的cookies
@WebServlet(name = "ReadCookiesByNameServlet",urlPatterns = "/readCookiesByName")
public class ReadCookiesByNameServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("ReadCookieByNameServlet 被调用..");
//得到指定的cookie的value
//1. 先得到浏览器携带的所有cookie
Cookie[] cookies = request.getCookies();
//2. 使用工具类来获取指定的cookie
Cookie emailCookie = CookieUtils.readCookieByName("email", cookies);
if(null != emailCookie) {
System.out.println("得到cookie name=" + emailCookie.getName()
+ " value= " + emailCookie.getValue());
} else {
System.out.println("sorry, 没有这个cookie");
}
//3. 给浏览器返回信息
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("完成读取cookie的任务..
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
.修改cookie
1)给定一个cookie的name,找到该cookie,如果找到,则修改该cookie的值为hi
2)如果找不到指定的cookie,则提示,没有该cookie
设置同名cookie等于等价替换
@WebServlet(name = "UpdateCookieServlet")
public class UpdateCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("UpdateCookie 被调用...");
//需求
/**
* 1. 需求 演示如何修改Cookie
* 1) 给定一个cookie的name, 找到该cookie, 如果找到, 则修改该cookie的值为 hsp-hi
* 2) 如果找不到指定的cookie , 则提示, 没有该cookie
*/
//1. 根据name 去查找 cookie
String cookieName = "email";
Cookie[] cookies = request.getCookies();
//如果我们直接创建了一个同名的cookie 等价于替换.
Cookie userNameCookie = new Cookie("username", "hahaha");
Cookie cookie = CookieUtils.readCookieByName(cookieName, cookies);
if (null == cookie) {//在该浏览器没有email cookie
System.out.println("当前访问 服务端的 浏览器没有 该cookie");
} else {
cookie.setValue("hsp-hi");
}
System.out.println("=====修改后的cookies信息=======");
//2. 编写cookie
for (Cookie cookie1 : cookies) {
System.out.println("cookie name= " + cookie1.getName()
+ " value= " + cookie1.getValue());
}
//3. 给浏览器返回信息
response.setContentType("text/html;charset=utf-8");
//4. 如果希望我们的浏览器本地的cookie也修改,则需要使用response.addCookie(cookie);
if(cookie != null) {
response.addCookie(cookie);
}
//把 新创建的userNameCookie 重新保存到浏览器
//如果 保存的userNameCookie 和已经有的cookie同名,就等价于替换.
if(userNameCookie != null) {
response.addCookie(userNameCookie);
}
PrintWriter writer = response.getWriter();
writer.println("完成修改cookie的任务..
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.Cookie的生命周期指的是如何管理Cookie什么时候被销毁(删除)
2. setMaxAge()
正数,表示在指定的秒数后过期(浏览器回送消息不再携带该cookie)
负数,表示浏览器关闭,Cookie就会被删除(默认值是-1)
0,表示马上删除Cookie
@WebServlet(name = "CookieLiveServlet",urlPatterns = "/cookieLive")
public class CookieLiveServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("CookieLive 被调用...");
//演示创建一个cookie , 生命周期为 60s
Cookie cookie = new Cookie("job", "java");
// 1. 从创建该cookie开始计时, 60秒后无效
// 2. 浏览器来根据创建的时间,计时到60s秒,就认为该cookie无效
// 3. 如果该cookie无效,那么浏览器在发出http请求时,就不在携带该cookie
cookie.setMaxAge(60);
//讲cookie保存到浏览器
response.addCookie(cookie);
//演示如何删除一个cookie, 比如删除username
//1 先得到username cookie
Cookie[] cookies = request.getCookies();
Cookie usernameCookie =
CookieUtils.readCookieByName("username", cookies);
if(usernameCookie != null) {
//2. 将其生命周期设置为0
usernameCookie.setMaxAge(0);
//3. 重新保存该cookie, 因为你将其生命周期设置0, 就等价于让浏览器删除该cookie
//4. 说明:该cookie会被浏览器直接删除
// 返回一个Set-Cookie: xxxxx => 一会抓包.
// Set-Cookie: username=tom; Expires=Thu, 01-Jan-1970 00:00:10 GMT
response.addCookie(usernameCookie);//返回一个Set-Cookie: xxxxx => 一会抓包.
}else{
System.out.println("没有找到该cookie, 无法删除...");
}
/***********************
* 默认的会话级别的 Cookie [即浏览器关闭就销毁了]
* 前面我们讲课时,都是默认会话级别的生命周期
***********************/
Cookie cookie3 = new Cookie("dkey", "dkey_value");
/**
* 老韩解读 setMaxAge源码
* public void setMaxAge(int expiry) {
* this.maxAge = expiry;
* }
* private int maxAge = -1; 默认就是-1
*/
//cookie.setMaxAge(-1);//设置存活时间
response.addCookie(cookie3);
// 给浏览器返回信息
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("设置cookie生命周期
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1. Cookie有效路径Path的设置
2. Cookie的path属性可以有效的过滤哪些Cookie可以发送给服务器。哪些不发。path 属性是通过请求的地址来进行有效的过滤
3.规则如下:
cookie1.setPath = /工程路径
cookie2.setPath = /工程路径/aaa
请求地址: http://ip:端口/工程路径/资源
cookie1会发给服务器
cookie2不会发给服务器
请求地址: http://ip:端口/工程路径/aaa/资源
cookie1会发给服务器
cookie2会发给服务器
@WebServlet(name = "CookiePathServlet",urlPatterns = "/cookiePath")
public class CookiePathServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("CookiePath 被调用...");
//1. 创建两个cookie
Cookie cookie = new Cookie("address", "bj");
Cookie cookie2 = new Cookie("salary", "20000");
//2. 设置不同有效路径
// request.getContextPath() => /cookie_session
cookie.setPath(request.getContextPath());
// cookie2有效路径 /cookie_session/aaa
cookie2.setPath(request.getContextPath() + "/aaa");
//老师说明:如果我们没有设置cookie有效路径,默认就是 /工程路径
//3. 保存到浏览器
response.addCookie(cookie);
response.addCookie(cookie2);
//4. 给浏览器返回信息
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("设置cookie有效路径成功
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
需求:完成自动填写登录账户应用案例,如果用户登录成功,则下次登录自动填写登录账户(如图)
1)如果用户名是hspedu,密码是123456,则认为该用户合法, 登录成功,否则登录失败
2)要求实现如果登录成功,则该用户,在3天内登录,可以自动填写其登录名
3)老师提示:登录页面需要使用servlet返回,而不能使用html
1.动态的获取HTML,一个类返回html页面
@WebServlet(name = "UserUIServlet",urlPatterns = "/userUI")
public class UserUIServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("UserUIServlet .. 被调用..");
//String contextPath = request.getContextPath();
//1. 难度没有增加,-》文件多了 => 基础
//2. 读取从浏览器发送来的cookie
Cookie[] cookies = request.getCookies();
Cookie loginuserCookie = CookieUtils.readCookieByName("loginuser", cookies);
String username = "";
if (loginuserCookie != null) {//如果有
username = loginuserCookie.getValue();
}
response.setContentType("text/html;charset=utf-8");
//1. 得到writer
PrintWriter writer = response.getWriter();
writer.println("\n" +
"\n" +
"\n" +
" \n" +
" 登录页面 \n" +
"\n" +
"\n" +
"用户登录界面
\n" +
"\n" +
"\n" +
"");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
2.一个servlet 返回cookie给浏览器保存
@WebServlet(name = "LoginServlet",urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("LoginServlet 被调用...~~~");
//1. 接收表单提交用户名和密码
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
//2. 判断是否合法
if ("yh".equals(username) && "123456".equals(pwd)) {
//将登录成功的用户名,以cookie的形式,保存到浏览器
Cookie loginuserCookie = new Cookie("loginuser", username);
//设置该cookie生命周期
loginuserCookie.setMaxAge(3600 * 24 * 3);
response.addCookie(loginuserCookie);
//合法
writer.println("登录OK
");
} else {
//不合法
writer.println("登录失败
");
}
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.一个Cookie只能标识一种信息,它至少含有一个标识该信息的名称(NAME)和设置值(VALUE)。
2.一个WEB站点可以给一个浏览器发送多个Cookie,一个浏览器也可以存储多个WEB站点提供的
Cookie。
3. cookie的总数量没有限制,但是每个域名的COOKIE数量和每个COOKIE的大小是有限制的
(不同的浏览器限制不同,知道即可),Cookie不适合存放数据量大的信息。
4.注意,删除cookie时,path必须一致,否则不会删除
5. Java servlet中cookie中文乱码解决
如果存放中文的cookie,默认报错,可以通过URL编码和解码来解决
将中文设置为utf--8编码,在浏览器内显示中文url的编码,需要使用url解码才能看到
@WebServlet(name = "EncoderCookieServlet",urlPatterns = "/encoder")
public class EncoderCookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("EncoderCookie 被调用");
//1. 创建cookie, 有中文
// 1) 如果直接存放中文的cookie, 报错500 - Control character in cookie value or attribute.
//2) 解决方法,就是将中文 编程成 URL编码 英文: Encode=编码
//3) 编码后,再保存即可
String company = URLEncoder.encode("韩顺平教育", "utf-8");
Cookie cookie = new Cookie("company", company);
//2. 保存到浏览器
response.addCookie(cookie);
//3. 给浏览器返回信息
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("设置中文cookie成功
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
url解码,一个servlet解密url编码
@WebServlet(name = "ReadCookie2Servlet")
public class ReadCookie2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("ReadCookie2 被调用..");
//读取到中文cookie
Cookie[] cookies = request.getCookies();
Cookie companyCookie = CookieUtils.readCookieByName("company",cookies);
String companyVal = companyCookie.getValue();
System.out.println("companyVal= " + companyVal);//URL
//解码
companyVal = URLDecoder.decode(companyVal, "utf-8");
System.out.println("解码后 companyVal= " + companyVal);//中文
//3. 给浏览器返回信息
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("读取中文cookie解码成功~
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
1.不同的用户登录网站后,不管该用户浏览该网站的哪个页面,都可显示登录人的名字,还可以随时去查看自己的购物车中的商品,是如何实现的?
2.也就是说,一个用户在浏览网站不同页面时,服务器是如何知道是张三在浏览这个页面,还是李四在浏览这个页面?
解决之道一session技术
1. Session是服务器端技术,服务器在运行时为每一个用户 的浏览器创建一个其独享的session对象
2.由于session为各个用户浏览器独享,所以用户在访问服务器的不同页面时,可以从各自session中读取/添加数据,从而完成相应任务
1.当用户打开浏览器,访问某个网站,操作session时,服务器就会在内存(在服务端)为该浏览器分配一个session对象,该session对象被这个浏览器独占,如图
2.这个session对象也可看做是一个容器/集合,session对象默认存在时间为30min,也可修改(Tomcat/conf/web.xml)
1.网上商城中的购物车
2.保存登录用户的信息
3.将数据放入到Session中,供用户在访问不同页面时,实现跨页面访问数据
4.防止用户非法登录到某个页面
1. session存储结构示意图
2.你可以把session看作是一个容器类似HashMap,有两列(K-V),每一 行就是session的一 个属性。
3.每个属性包含有两个部分,一个是该属性的名字(String), 另外一个是它的值(Object)
1.创建和获取Session, API
HttpSession hs= request.getSession();
第1次调用是创建Session会话,之后调用是获取创建好的Session对象
2.向session添加属性
hs.setAttribute(String name,Object val);
3.从session得到某个属性
Object obj= hs.getAttribute(String name);
4.从session删除调某个属性:
hs.removeAttribute(String name);
5. isNew();判断是不是刚创建出来的Session
6.每个Session都有1个唯一标识Id值。通过getId()得到Session的会话id值
关于底层如何找到Jsessionid和如何创建的流程
@WebServlet(name = "CreateSessionServlet",urlPatterns = "/createSession")
public class CreateSessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("CreateSession 被调用...");
//1. 获取session, 同时也可能创建session
HttpSession session = request.getSession();
//2. 给session获取id
System.out.println("CreateSession 当前sessionid= " + session.getId());
//3. 给session存放数据
session.setAttribute("email", "[email protected]");
//4. 给浏览器发送一个回复
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("创建/操作session成功...
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
当有session之后,而且在服务端能找到该sessionID 响应头不会再返回session
当为新的时候,请求头不会携带session,响应头会返回对应的session
读取Session
@WebServlet(name = "ReadSessionServlet",urlPatterns = "/readSession")
public class ReadSessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("ReadSession 被调用...");
// 演示读取session
//1. 获取session, 如果没有sesion, 也会创建
HttpSession session = request.getSession();
//输出sessionId
System.out.println("ReadSession sessionid= " + session.getId());
//2. 读取属性
Object email = session.getAttribute("email");
if (email != null) {
System.out.println("session属性 email= " + (String) email);
} else {
System.out.println("session中没有 email属性 ");
}
//给浏览器回复一下
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("读取session成功...
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1. public void setMaxInactiveInterval(int interval)设置Session的超时时间(以秒为单位),超过
指定的时长,Session 就会被销毁。
2.值为正数的时候,设定Session的超时时长。
3.负数表示永不超时
4. public int getMaxInactiveInterval( ) 获取Session的超时时间
5. public void invalidate() 让当前Session会话立即无效
6.如果没有调用setMaxInactiveInterval()来指定Session的生命时长,Tomcat会以Session默认时长
为准,Session默认的超时为30分钟,可以在tomcat的web.xml设置
7. Session的生命周期指的是:客户端/浏览器两次请求最大间隔时长,而不是累积时长。即当客户端访问了自己的session, session的生命周期将从0开始重新计算。( 指的是同一个会话两次请求之间的间隔时间)
8.底层: Tomcat用一个线程来轮询会话状态,如果某个会话的空闲时间超过设定的最大值,则将该会话销毁
创建Session的生命周期
@WebServlet(name = "CreateSession2Servlet",urlPatterns = "/createSession2")
public class CreateSession2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("CreateSession2 被调用");
//创建session
HttpSession session = request.getSession();
System.out.println("CreateSession2 sid= " + session.getId());
//设置生命周期为 60s
session.setMaxInactiveInterval(60);
session.setAttribute("u", "jack");
//回复一下浏览器
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("创建session成功, 设置生命周期60s
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
读取session验证其生命周期
@WebServlet(name = "ReadSession2Servlet")
public class ReadSession2Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//System.out.println("ReadSession2 被调用...");
//1. 获取到session
HttpSession session = request.getSession();
System.out.println("ReadSession2 sid= " + session.getId());
//2. 读取session的属性
Object u = session.getAttribute("u");
if (u != null) {
System.out.println("读取到session属性 u= " + (String) u);
} else {
System.out.println("读取不到session属性 u 说明原来的session被销毁");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
删除session 立即删除session
@WebServlet(name = "DeleteSessionServlet")
public class DeleteSessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("DeleteSession 被调用...");
//演示如何删除session
HttpSession session = request.getSession();
session.invalidate();
//如果你要删除session的某个属性
//session.removeAttribute("xxx");
//回复一下浏览器
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("删除session成功
");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
1.登录界面
1)只要密码为666666,我们认为就是登录成功
2)用户名不限制
2.如果验证成功,则进入管理页面ManageServelt.java ,否则进入error.html
3.如果用户直接访问ManageServet.java,直接重定向到login.html
Manage管理Servlet用于判断是否登录过
@WebServlet(name = "ManageServlet",urlPatterns = "/manageServlet")
public class ManageServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//判断该用户/会话是否登录过
HttpSession session = request.getSession();
Object loginuser = session.getAttribute("loginuser");
if(loginuser == null){//说明该用户没有登录
//重新登录 请求重定向
String contextPath = request.getContextPath();
System.out.println("有个未经允许的访问");
response.sendRedirect(contextPath + "/userlogin.html");
return;
}
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println("用户管理页面
");
writer.println("welcome admin!!!");
writer.flush();
writer.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
一个判断 判断是否为正常用户并进行分发 并且传回一个session 该session内记录了登录成功的username
@WebServlet(name = "LoginCheckServlet",urlPatterns = "/loginCheckServlet")
public class LoginCheckServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
if ("666666".equals(pwd)){
//登录成功 将session存入
HttpSession session = request.getSession();
session.setAttribute("loginuser",username);
//请求转发到ManageServlet
request.getRequestDispatcher("/manageServlet").forward(request,response);
}else {
request.getRequestDispatcher("/error.html").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
登录页面
登录页面
用户登录界面
错误返回页面
登录失败
登录失败
点击重新登录