package cn.itcast.cookie;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import cn.itcast.Db;
import cn.itcast.Db1;
import cn.itcast.User; public class SessionDemo extends HttpServlet { public 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 username = request.getParameter("username");
String password = request.getParameter("password"); List<User> list = Db1.getAll();
for (User user : list) {
if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
request.getSession().setAttribute("user", user);//登录成功,向session存入一个登录标记
response.sendRedirect("/ServletDemo/index.jsp");
return;
}
} out.write("用户名或密码错误!");
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast.cookie;

 import java.io.IOException;
import java.io.PrintWriter; 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 SessionDemo2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter(); HttpSession session = request.getSession();
if(session==null){
response.sendRedirect("/ServletDemo/index.jsp");
return;
} session.removeAttribute("user");
response.sendRedirect("/ServletDemo/index.jsp"); } public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }
 package cn.itcast;

 import java.util.ArrayList;
import java.util.List; public class Db1 { public static List list = new ArrayList();
static{
list.add(new User("aaa","123"));
list.add(new User("bbb","123"));
list.add(new User("ccc","123"));
list.add(new User("ddd","123"));
} public static List getAll(){
return list;
} }
 package cn.itcast;

 public class User {
private String username; public User() {
super();
} public User(String username, String password) {
super();
this.username = username;
this.password = password;
} private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
欢迎您:${user.username }<a href="/ServletDemo/register.html">登录</a> <a href="/ServletDemo/servlet/SessionDemo2">退出登录</a>
</body>
</html>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register.html</title> </head> <style>
</style> <body>
<form action="/ServletDemo/servlet/SessionDemo" method="get">
用户名:
<input type="text" name="username">
<br />
密码:
<input type="password" name="password">
<br />
<input type="submit" value="注册" />
</form>
</body>
</html>

利用session完成用户登陆的更多相关文章

  1. 利用Session完成用户的登录和注销

    用户的登录和注销是最常见的Web应用案例,当一个应用的客户登录了以后,其他所有的会话都得知道这个用户已经登录还很有可能得提取用户的昵称予以显示等等,所以,只有把登录成功的用户的信息放入到Session ...

  2. PHP会话(Session)实现用户登陆功能 转自#落人间#

    对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用. 由于 Session 是以文本文件形式存储在 ...

  3. JavaWeb 基于Session的用户登陆注销实现

    通过Session来存储用户的部分登陆信息来验证用户是否在线,这应该时最容易实现的一种Web端方案,本文以SSM(Spring.SpringMVC.myBatis)框架为载体,来具体实现这套登陆系统. ...

  4. PHP会话(Session)实现用户登陆功能

    对比起 Cookie,Session 是存储在服务器端的会话,相对安全,并且不像 Cookie 那样有存储长度限制,本文简单介绍 Session 的使用. 由于 Session 是以文本文件形式存储在 ...

  5. 利用session防止用户未经登录而直接访问

    在编写项目的时候,突然想如果按常理出牌,不首先进入登录界面而直接访问网页内容,可不可以呢?如此一来便尝试了一下,整的可以直接进入管理员页面,获取完全的管理权限.于是在网上查看了一下解决方案,学习了一下 ...

  6. flask中利用session实现用户记住密码

    “记住密码”的实质,实际上就是把cookie的有效期设置的长一点,当用户没有选择记住密码时,cookie的有效期为会话结束,选择记住密码后,会根据服务器的设置延长cookie的有效期,默认是31天.在 ...

  7. 【Web】Tomcat中利用Session识别用户的基本原理

    HTTP无状态的特性与Session.Cookie的存在 HTTP有一个特性:无状态的,就是前后两个HTTP事务它们并不知道对方的信息. 而为了维护会话信息或用户信息,一般可用Cookie或Sessi ...

  8. 09-利用session完成用户登陆

    /***********************************************login.html*****************************************/ ...

  9. 利用Node.js实现模拟Session验证的登陆

    1.身份验证和用户登陆 在一般的Web应用上,假设要实现用户登陆,最经常使用,也是最简单的方法就是使用Session,主要的思路是在Session中保留一些用户身份信息,然后每次在Session中取, ...

随机推荐

  1. DSP using MATLAB 示例Example3.21

    代码: % Discrete-time Signal x1(n) % Ts = 0.0002; n = -25:1:25; nTs = n*Ts; Fs = 1/Ts; x = exp(-1000*a ...

  2. C#函数与SQL储存过程

    一点点小认识作为memo,求指正. C#的函数与SQL的储存过程有很多的相似性, 它们都是一段封闭的代码块,来提高代码的重用性,虽然现在复制粘贴很方便,但是我们在写多个函数的时候频繁的复制粘贴相同的内 ...

  3. 【CLR in c#】参数

    1.可选参数和命名参数 设计一个参数时,可为部分或全部参数分配默认值,调用这些方法的代码可以选择不指定部分实参,接受默认值,还可以通过制定参数名称的方式传递实参.如下 class CLR可选参数 { ...

  4. docker不稳定 short running containers with -rm failed to destroy

    正常运行以下命令 sudo docker run --rm busybox echo helloworld /var/log/upstart/docker.log 日志如下: // :: POST / ...

  5. POJ2135 Farm Tour(最小费用最大流)

    题目问的是从1到n再回到1边不重复走的最短路,本质是找1到n的两条路径不重复的尽量短的路. #include<cstdio> #include<cstring> #includ ...

  6. Shell 编程基础之 Case 练习

    一.语法 case $变量 in "第一个变量内容") # 每个变量内容建议用双引号括起来,关键字则为小括号 ) # 执行内容 ;; # 每个类别结尾使用两个连续的分号来处理! & ...

  7. 获取DLL中的方法名称

      OpenFileDialog obj = new OpenFileDialog(); if (obj.ShowDialog() == System.Windows.Forms.DialogResu ...

  8. mac笔记

    chrome F12快捷键 option+command+i updatedb: http://www.jeffkit.info/2010/04/66/ /usr/libexec/locate.upd ...

  9. BZOJ4209 : 西瓜王

    首先求出区间前$k$大数中奇数的个数和偶数的个数. 如果都是偶数,那么答案就是前$k$大数的和. 否则,要么去掉最小的偶数,加上最大的奇数,要么去掉最小的奇数,加上最大的偶数. 主席树维护即可. 时间 ...

  10. linux中用shell获取时间,日期

    linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...