学习Servlet之后,觉得HttpSession的作用还是比较大的,可以通过HttpSession机制往页面传值,以下是一个简单的例子
1.新建登陆页
<%@ 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>登陆页</title>
</head>
<body>
<center>
<div style="margin-top:100px">
<h2>请登陆</h2>
<form action="login.do" method="post">
<input type="text" name="username" id="username"/><br>
<input type="password" name="pwd" id="pwd"/><br>
<input type="submit" value="登陆"/>
</form>
</div>
</center>
</body>
</html>
<%@ 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>欢迎页</title>
</head>
<body>
<h1>登陆成功,当前用户:${userName}</h1>
<h2>登陆时间:${logindate }</h2>
</body>
</html>
3.登陆失败信息提示页面,可以通过超链接跳转至登陆页
<%@ 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>
<p>${msg }</p><br>
<a href="../pages/login.jsp"><p>点此重新登陆</p></a>
</body>
</html>
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配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>TestSession</display-name>
<welcome-file-list>
<welcome-file>/pages/login.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>userLogin</servlet-name>
<servlet-class>com.evian.controller.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userLogin</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>
8.运行效果图