09-利用session完成用户登陆
/***********************************************login.html*****************************************/
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/day07/LoginServlet" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">;
</form>
</body>
</html>
/*************************************LoginServlet***********************************************/
package session;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name = request.getParameter("username");
String pwd = request.getParameter("password");
List<User> list = Db.getAll();
for(User user:list){
if(user.getName().equals(name) && user.getPassword().equals(pwd)){
//登录成功就是向session存一个登录标记
request.getSession().setAttribute("user", user);//值是user对象
//跳到首页
response.sendRedirect(request.getContextPath()+"/index.jsp");
return;
}
}
out.print("用户名密码不正确");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
class Db{
private static List list = new ArrayList();
static{
list.add(new User("aaa","123"));
list.add(new User("bbb","123"));
list.add(new User("ccc","123"));
}
public static List getAll(){
return list;
}
}
/*****************************************************************index.jsp*******************************************************************8/
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
welcome:${user.name}<a href="/day07/login.html">登录</a><a href="/day07/LogoutServlet">退出登录</a>
</body>
</html>
/***********************************************LogoutServlet*************************************************/
package session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//注销用户登录
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
//先判断session是否为空,空就代表没有用户登录,这是就不做处理,继续跳回到首页
if(session == null){
response.sendRedirect("/day07/index.jsp");
return;
}
//不为空就摧毁session跳回到首页
session.removeAttribute("user");
response.sendRedirect("/day07/index.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
09-利用session完成用户登陆的更多相关文章
- 利用session完成用户登陆
package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.L ...
- 利用Session完成用户的登录和注销
用户的登录和注销是最常见的Web应用案例,当一个应用的客户登录了以后,其他所有的会话都得知道这个用户已经登录还很有可能得提取用户的昵称予以显示等等,所以,只有把登录成功的用户的信息放入到Session ...
- PHP会话(Session)实现用户登陆功能 转自#落人间#
对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用. 由于 Session 是以文本文件形式存储在 ...
- JavaWeb 基于Session的用户登陆注销实现
通过Session来存储用户的部分登陆信息来验证用户是否在线,这应该时最容易实现的一种Web端方案,本文以SSM(Spring.SpringMVC.myBatis)框架为载体,来具体实现这套登陆系统. ...
- PHP会话(Session)实现用户登陆功能
对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用. 由于 Session 是以文本文件形式存储在 ...
- 利用session防止用户未经登录而直接访问
在编写项目的时候,突然想如果按常理出牌,不首先进入登录界面而直接访问网页内容,可不可以呢?如此一来便尝试了一下,整的可以直接进入管理员页面,获取完全的管理权限.于是在网上查看了一下解决方案,学习了一下 ...
- flask中利用session实现用户记住密码
“记住密码”的实质,实际上就是把cookie的有效期设置的长一点,当用户没有选择记住密码时,cookie的有效期为会话结束,选择记住密码后,会根据服务器的设置延长cookie的有效期,默认是31天.在 ...
- 【Web】Tomcat中利用Session识别用户的基本原理
HTTP无状态的特性与Session.Cookie的存在 HTTP有一个特性:无状态的,就是前后两个HTTP事务它们并不知道对方的信息. 而为了维护会话信息或用户信息,一般可用Cookie或Sessi ...
- [转]mvc3 使用session来存储类来存储用户登陆信息
mvc3 使用session来存储类来存储用户登陆信息 2013-08-26 09:48:56| 分类: NET开发 |举报 |字号 订阅 项目之前的登陆机制是这样的:用户登陆后初始化一个类,类 ...
随机推荐
- .net web api ioc unity usage
1.use nuget to install unity.webapi 2.add configurations in application_start folder using Microsoft ...
- EOJ Monthly 2018.2
A. 坑爹的售票机 题意 用\(1,5,10,25,50,100\)的纸币买\(n\)张单价为\(p\)的船票,且一次性最多买\(k\)张,求钱数恰好时最少需要多少张纸币. Hard: \(n,k,p ...
- 享元模式FlyweightPattern(转)
解释一下概念:也就是说在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象.比如说一个文本系统,每个字母定一个对象,那么大小写字母一共就是52个,那么就要定义52个对象 ...
- RSTP介绍
1. 介绍 RSTP(Rapid Spanning Tree Protocol),快速生成树协议,标准为802.1w(已合入802.1D-2004)RSTP是对STP技术的修改和补充,最大特点就是快速 ...
- springboot 邮件
<!-- 邮件end --><dependency> <groupId>org.springframework.boot</groupId> <a ...
- syslog/rsyslog的使用
syslogd是Linux下的一个记录日志文件服务.从结构来说,可以理解为这个服务下面有一系列的子服务,例如mail.auth.cron.kern等等,这些子服务对外提供日志记录的功能,而当其它的程序 ...
- springBoot 编写接口
@Slf4j @RestController public class testController { @Autowired StringRedisTemplate redis; @RequestM ...
- AC日记——黑魔法师之门 codevs 1995
1995 黑魔法师之门 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 经过了16个工作日的紧张 ...
- CentOS7安装部署jumpserver0.5
组件说明 Jumpserver为管理后台,管理员可以通过Web页面进行资产管理.用户管理.资产授权等操作; Coco为SSH Server和Web Terminal Server.用户可以通过使用自己 ...
- centos7的时间同步机制:chrony使用
配置时间同步方法如下: 1.安装chrony时间同步服务(系统默认安装) #yum install chrony 可以先查询一下是否有安装: [root@localhost etc]# rpm -qa ...