struts2+hibernate+ajax实现登陆验证
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Author:zhenghui
Login.jsp
<%@page language="java"import="java.util.*"pageEncoding="utf-8"%> <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.get ServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>struts中使用ajax技術</title>
<meta http-equiv="Content-Type"content="text/html; charset=utf-8"/>
<link rel="stylesheet"type="text/css"href="css/style.css">
<script type="text/javascript">
function trim(str){ //删除左右两端的空格
return str.replace(/(^\s*)|(\s*$)/g, "");
}
</script>
<script type="text/javascript">
function ajaxLogin()
{
var xmlhttp;
var text;
var username=document.getElementById("username").value;
var password=document.getElementById("password").value;
//检测是否为空可以将其交给前台处理;
if(username.length==0){
alert("用户名不能为空");
return false;
}
else if(password.length==0){
alert("密码不能为空");
return false;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
text=xmlhttp.responseText;
if(trim(text)=="success"){
alert("登陆成功!");
window.location.href="home.jsp";
}
else{
alert("登录失败,请重新登陆");
}
}
}
xmlhttp.open("POST","UserAction_ajaxLogin.action?ername="+ username+"&user.password="+password,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="form">
<input type="text"id="username"name="ername"/>
<input type="password"id="password"name="user.password"/>
<input id="submit"type="submit"value="submit"
onclick="ajaxLogin()"/>
</form>
</body>
</html>
User.java
package blog.model;
public class User {
private int id;
private String username;
private String password;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setPassword(String password) { this.password = password;
}
public String getPassword() {
return password;
}
public void setUsername(String username) { ername = username;
}
public String getUsername() {
return username;
}
}
UserAction.java
package blog.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import er;
import erService;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class UserAction extends ActionSupport {
private UserService userService = new UserService();
private User user;
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
public String ajaxLogin() throws Exception {
try {
String responseText = null;
//logincheck(User user)根据传入的user,查询是否已经存在返回boolean;
if (userService.logincheck(user)) {
//以下隔开的是登陆的同时将登陆者的信息放进session里面;
/////////////////////////////////////////////////////////////////
//getUserbyName(String username),根据传入的名字查询该对象出来
user = userService.getUserbyName(user.getUsername());
HttpSession httpSession = ServletActionContext.getRequest()s
.getSession();
System.out.println(user.getUsername()
+ user.getPrivilege().getId());
httpSession.setAttribute("username",
user.getUsername());
httpSession.setAttribute("privilege_id",
user.getPrivilege()
.getId());
httpSession.setAttribute("user_id", user.getId());
///////////////////////////////////////////////////////////////// responseText = "success";
} else {
System.out.println("fail");
responseText = "fail";
}
// 取得当前原始的PrintWriter对象,以便于直接输出响应结果,而不用跳转到某个视图;
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println(responseText);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public User getUser() {
return user;
}
public void setUser(User user) {
er = user;
}
}
Struts.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"/dtds/struts-2.1.dtd">
<struts>
<package name="action"extends="struts-default">
<action name="*_*"class="blog.action.{1}"method="{2}"> <result></result>
</action>
</package>
</struts>
仅供参考,仅供参考。
如对您造成不适,请原谅。