JavaWeb_使用Servlet + HttpSession实现简单用户登陆


学习Servlet之后,觉得HttpSession的作用还是比较大的,可以通过HttpSession机制往页面传值,以下是一个简单的例子

1.新建登陆页


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登陆页



请登陆




2.登陆成功后跳转至首页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




欢迎页


登陆成功,当前用户:${userName}

登陆时间:${logindate }



3.登陆失败信息提示页面,可以通过超链接跳转至登陆页

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


 

${msg }


点此重新登陆


4.创建用户模型

package com.evian.model;

public class User {
	
	private String uname;
	private String password;
	private String sex;

	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}


	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public User(String uname, String password, String sex) {
		super();
		this.uname = uname;
		this.password = password;
		this.sex = sex;
		
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	

}

5.用户Service,我只是做个小例子,所以没有链接数据库,只是通过Java数组存储几个用户

package com.evian.util;

import com.evian.model.User;

public class UserService {

	User user1 = new User("张三", "123123", "男");
	User user2 = new User("李白", "110110", "女");
	User user3 = new User("王名", "123456", "男");
	User[] users = { user1, user2, user3 }; //创建数组用于保存user
    
	/**
	 * 根据用户名匹配user对象并返回给servlet
	 * @param username
	 * @return
	 */
	public User userlogin(String username) {
		User u = null;
		for (User user : users) {
			if (username.equals(user.getUname())) {
				u = user;
			}
		}
		return u;
	}

}
6.登陆Action

package com.evian.controller;


import java.io.IOException;
import java.util.Date;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import com.evian.model.User;
import com.evian.util.UserService;


@SuppressWarnings("serial")
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {


	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");// 设置请求信息字符编码
		response.setCharacterEncoding("utf-8");// 设置响应信息字符编码
		String userName = request.getParameter("username");// 获取登陆页用户名信息
		String password = request.getParameter("pwd");// 获取登陆页用户密码信息
		UserService us = new UserService();
		User user = us.userlogin(userName);// 调用UserService方法,返回用户名等于登陆页用户名的User对象
		HttpSession session = request.getSession();// 创建Session对象


		/**
		 * 判断密码是否正确
		 */
		if (user != null) {


			if (user.getPassword().equals(password)) {


				session.setAttribute("userName", userName);
				session.setAttribute("logindate", new Date());
				response.sendRedirect("/TestSession/pages/index.jsp");// 密码正确跳转至index页面
			} else {
				session.setAttribute("msg", "密码错误");
				response.sendRedirect("/TestSession/pages/error.jsp");// 密码错误停留在login页面
			}
		} else {
			session.setAttribute("msg", "用户名不存在");
			response.sendRedirect("/TestSession/pages/error.jsp");// 用户名错误停留在login页面
		}
	}


}
7.web.xml配置文件



  TestSession
  
    /pages/login.jsp
  
  
    userLogin
    com.evian.controller.UserServlet
  
  
    userLogin
    /login.do
  

8.运行效果图 JavaWeb_使用Servlet + HttpSession实现简单用户登陆_第1张图片 JavaWeb_使用Servlet + HttpSession实现简单用户登陆_第2张图片 JavaWeb_使用Servlet + HttpSession实现简单用户登陆_第3张图片 JavaWeb_使用Servlet + HttpSession实现简单用户登陆_第4张图片 JavaWeb_使用Servlet + HttpSession实现简单用户登陆_第5张图片

你可能感兴趣的:(Servlet)