实现功能:
1、用户登陆、注销
2、利用session记录用户登陆信息
3、在JSP中展示已登陆用户信息
实现原理:
登陆后通过判断用户名和密码是否和存储的一致,如果一致,就把用户信息放到session中储存;如果不一致就提示信息,并且返回登陆页面。
显示信息页面上固定从session中找用户登陆信息,找到就显示用户信息,没找到就显示登陆框。
注销很简单,就是清空session信息。
主要文件:
1、LoginAction:struts2的Action类,用于处理JAVA端的主要登陆和登出逻辑。
2、login.jsp:用户登陆页面,用户输入用户名和密码,如果登陆失败显示失败信息。
3、page.jsp:登陆成功后显示用户信息。
4、struts.xml:struts的配置文件。
LoginAction:struts2的Action类,用于处理JAVA端的主要登陆和登出逻辑
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
package luju.me.teach.struts2.login;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;
/**
* @author 鲁炬 http://luju.me
*
*/
public class LoginAction {
private String loginname;
private String password;
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this .msg = msg;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this .loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
/** 用户登陆 */
public String login() {
if (StringUtils.isBlank( this .loginname)) {
return Action.INPUT;
}
/*
这里写你自己通过用户名查找用户信息的业务逻辑
比如:使用手机号登陆
Citizen user = prmService.queryEGovCitizenByMobile(this.loginname);
....
*/
if(user == null || user.getPwd() == null || !user.getPwd().getValue().equals(this.password)) {
//登陆失败
this.msg = "用户不存在或密码错误!";
return Action.INPUT;
} else {
//登陆成功
//设置session
this.getSession().setAttribute("_USER_INFO_LOGIN_NAME_", this.loginname);
this.getSession().setAttribute("_USER_INFO_USER_ID_", user.getId().getValue());
this.getSession().setAttribute("_USER_INFO_USER_INFO_", user);
//设置cookie
this.getResponse().addCookie(new Cookie("_USER_INFO_LOGIN_NAME_", this.loginname));
this.getResponse().addCookie(new Cookie("_USER_INFO_USER_ID_", user.getId().getValue()));
return Action.SUCCESS;
}
}
/**
* 注销
*/
public String loginout() {
//清空session
this .getSession().invalidate();
return Action.SUCCESS;
}
public HttpSession getSession() {
return ServletActionContext.getRequest().getSession();
}
public HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}
public HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
}
|
struts.xml:struts的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
< package name= "common" namespace= "/common" >
<action name= "login" class = "luju.me.site.common.action.LoginAction"
method= "login" >
<result name= "input" >login.jsp</result>
<result name= "success" type= "redirect" >/page.jsp</result>
</action>
<action name= "loginout" class = "luju.me.site.common.action.LoginAction"
method= "loginout" >
<result name= "success" type= "redirect" >login.action</result>
</action>
</ package >
</struts>
|
login.jsp:用户登陆页面,用户输入用户名和密码,如果登陆失败显示失败信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<%@ page language= "java" pageEncoding= "UTF-8" %>
<%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %>
<html>
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
<title>欢迎登录</title>
</head>
<body>
<div id= "login" >
<span>${msg}</span>
<form name= "form1" method= "post" action= "<c:url value=" /common/login.action " />" >
<span>
<label>用户名:</label>
<input name= "loginname" id= "loginname" type= "text" value= "admin" />
</span>
<span>
<label>密码:</label>
<input type= "password" name= "password" id= "password" value= "123" />
</span>
<span>
<input type= "submit" value= "登陆" />
</span>
</form>
</div>
</body>
</html>
|
page.jsp:登陆成功后显示用户信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<%@ page language= "java" pageEncoding= "UTF-8" %>
<%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %>
<%
boolean isLogin = false ;
String loginName = (String)request.getSession().getAttribute( "_USER_INFO_LOGIN_NAME_" );
if (loginName != null && ! "" .equals(loginName)){
isLogin = true ;
}
request.setAttribute( "isLogin" ,isLogin);
request.setAttribute( "loginName" ,loginName);
%>
<c: if test= "${isLogin}" >
你好:${loginName}
<a href= "<c:url value=" /common/loginout.action " />" >注销</a>
</c: if >
<c: if test= "${!isLogin}" >
<form name= "login_form" method= "post" action= "<c:url value=" /common/login.action " />" >
<span>
<label>手机号:</label>
<input name= "loginname" id= "loginname" type= "text" value= "" />
</span>
<span>
<label>密码:</label>
<input type= "password" name= "password" id= "password" value= "" />
</span>
<span>
<input type= "submit" value= "登陆" />
</span>
</form>
</c: if >
|
以上所述是小编给大家介绍的Java Web用户登录实例代码,希望对大家有所帮助!