• Session小案例------完成用户登录


                                  Session小案例------完成用户登录

        在项目开发中,用户登陆功能再平常只是啦,当用户完毕username和password校验后。进入主界面,须要在主界面中显示用户的信息,此时用session来记住用户是最为合适只是了。

    功能实现例如以下:
    1,完毕用户登陆功能
    2,在主界面记住用户
    3,完毕用户注销功能

    用户类:
    package cn.itcast.login;
    
    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;
    	}
    }

    登陆:
    package cn.itcast.login;
    
    import java.io.IOException;
    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 {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		//request.getAttribute("username");  //从域中取username
    		String username = request.getParameter("username");
    		String password = request.getParameter("password");
    		
    		//检測username与password在数据库中是否存在
    		List<User> list = DB.getAll();
    		for(User user : list){
    			if(user.getUsername().equals(username)  && user.getPassword().equals(password)){
    				//从数据库中找到匹配用户。让用户登陆成功
    				request.getSession().setAttribute("user", user);
    				response.sendRedirect("/day07/index.jsp");
    				return;
    			}
    		}
    		response.setCharacterEncoding("UTF-8");
    		response.setContentType("text/html;charset=UTF-8");
    		response.getWriter().write("用户名或密码错误。!");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request, response);
    	}
    }
    
    class DB{
    	private static List<User> 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.login;
    
    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 {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		HttpSession session = request.getSession(false);
    		if(session!=null){ 
    			session.removeAttribute("user");
    		}
    		
    		response.sendRedirect("/day07/index.jsp");
    	}
    
    	public void doPost(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		doGet(request, response);
    	}
    
    }
    

    登陆页面:
    	<form action="/day07/servlet/LoginServlet" method="post">
      		用户名:<input type="text" name="username"><br/>
      		密码:<input type="password" name="password"><br/>
      		<input type="submit" value="登陆">
      	</form>
      

    主页面:
     <body>
        欢迎您:
        <% 
        	User user = (User)session.getAttribute("user");
        	if(user!=null)
        		out.write(user.getUsername());
        %>
        
        <a href="/day07/servlet/LogoutServlet">注销</a>
      </body>


    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    遗传算法优化BP神经网络——非线性函数拟合
    BP神经网络的数据分类——语音特征信号分类
    HTML5新特性
    HTML5——FileReader详解
    javaScript构造函数显式提供返回值
    react截图组件react-cropper的使用方法
    Ant Design of React的安装和使用方法
    flex布局
    CSS常用的元素居中方法
    javaScript异步编程
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4802151.html
Copyright © 2020-2023  润新知