利用session完成用户登录与注销

Cookie:cookie是客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器。
当用户使用浏览器再去访问服务器中的
web资源时,就会带着各自的数据区,这样,web资源处理的就是各自的数据了。
Session:session是服务器端技术,利用session技术,服务器在运行时可以为每一个用户的浏览器创建其
独享的session对象,由于session为用户浏览器独享,所以用户在访问服务器的web资源时,可以把
各自的数据放在session中,当用户再次去访问服务器中的其他web资源时,其他web资源再从用户各自的session
取出数据为用户服务。

Session和Cookie的主要区别:
Cookie是把用户的数据写给用户的浏览器
Session技术把用户的数据写到用户独占的session中。
Session对象由服务器创建,开发人员可以调用request对象的getSession方法得到session对象。

例子:
//实现利用session完成用户登录与注销功能:

新建javabean//User类:

package cn.lsh.bean;

public class User {
	private String username;
	private String password;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String username, String password) {
		super();
		this.username = username;
		this.password = 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;
	}
}

新建DB类,提供用户登陆数据:

package cn.lsh.bean;

import java.util.ArrayList;
import java.util.List;

public class Db {
	private static List list = new ArrayList();
	
	static{
		list.add(new User("aaa","123"));
		list.add(new User("bbb","456"));
		list.add(new User("ccc","678"));
		list.add(new User("ddd","789"));
		
	}
	public static List getAllUser(){
		return list;
	}
}	
新建login.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
    <form action="/session/LoginServlet" method="post">
    	用户名:<input type="text" name="username"/><br/>
    	密码:<input type="text" name="password"/><br/>
    		<input type="submit" value="登陆"/><br/> 
    </form>
  </body>
</html>

新建LoginServlet:

package cn.lsh.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.lsh.bean.Db;
import cn.lsh.bean.User;

public class LoginServlet 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 = Db.getAllUser();
		for(User user : list){
			if(user.getUsername().equals(username)&& user.getPassword().equals(password)){
				//登陆成功,把user存入session域对象中
				request.getSession().setAttribute("user", user);
				//重定向到index.jsp
				response.sendRedirect("/session/index.jsp");
				return;
			}
		}
		//登陆失败
		out.print("登录失败:用户名或密码错误");
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}
欢迎页面index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
   	<!-- 从域对象中获取用户的登陆 -->
   	欢迎您: ${user.username }
   	<a href="/session/OutServlet">退出登录</a>
  </body>
</html>


新建OutServlet::

package cn.lsh.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 OutServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//false代表:不创建session对象,只是从request中获取。
		HttpSession session = request.getSession(false);
		if(session==null){
			return;
		}
		session.removeAttribute("user");
		//从定向到index.jsp
		response.sendRedirect("/session/index.jsp");
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}

发布,部署到Tomcat服务器后,在浏览器地址栏输入http://localhost:8080/session/login.jsp

浏览器输出效果图如下:




阅读更多
版权声明:转载请注明出处:http://blog.csdn.net/u010143291 https://blog.csdn.net/u010143291/article/details/51597507
个人分类: JavaWeb
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭