<?xml version="1.0" encoding="utf-8" ?>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>欢迎来到校园二手交易系统</title>
<script type="text/javascript">
function check() {
var jqueryObj1 = $("#usernum");
var usernum = jqueryObj1.val();
var jqueryObj2 = $("#password");
var password = jqueryObj2.val();
$.get("${pageContext.request.contextPath }/login.do?time="
+ new Date().getTime() + "&usernum=" + usernum + "&password="
+ password, null, checkback);
}
function checkback(data) {
if (data.length == 6) {
var resultObj = $("#result2");
resultObj.html(data);
} else {
window.location.href = "${pageContext.request.contextPath }/view/index.jsp";
}
}
function verify() {
var jqueryObj = $("#usernum");
var usernum = jqueryObj.val();
$.get("${pageContext.request.contextPath }/LoginAjaxServlet?usernum="
+ usernum, null, callback);
}
function callback(data) {
var resultObj = $("#result1");
resultObj.html(data);
}
</script>
<script type="text/javascript"
src="${pageContext.request.contextPath }/js/jquery-1.6.2.js"></script>
</head>
<body>
<table width="600" border="0" align="center">
<tr>
<td width="400" height="400">
<table width="400" border="0">
<tr>
<td width="100" class="label">学号:</td>
<td width="200" height="30"><input type="text" name="usernum"
id="usernum" style="width: 191px;" οnblur="verify()" /></td>
<td width="100"><span id="result1" class="warn"></span></td>
</tr>
<tr>
<td width="100" class="label">密码:</td>
<td width="200" height="30"><input type="password"
name="password" id="password" style="width: 191px;" /></td>
<td width="100"><span id="result2" class="warn"></span></td>
</tr>
<tr>
<td width="100" height="30"></td>
<td width="200" height="30"><div align="center">
<input name="button" type="button" class="loginbutton"
id="button" value="登录" οnclick="check()" />
</div></td>
<td height="100"></td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="55"></td>
</tr>
</table>
</body>
</html>
result1的servlet:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.CheckAjaxNum;
/**
* Servlet implementation class LoginAjaxServlet
*/
public class LoginAjaxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginAjaxServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
try {
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = response.getWriter();
String oldString = request.getParameter("usernum");
if (oldString == null || oldString.length() == 0) {
printWriter.println("不能为空");
} else {
String nameString = oldString;
if (!CheckAjaxNum.checkLoginNum(nameString))
printWriter.println("学号无效");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
result2的servlet:
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.LoginCheck;
/**
* Servlet implementation class LoginServlet
*
* 验证登录,成功后转入首页并显示相应数据
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String usernumString = request.getParameter("usernum");// 获取输入的用户名
String passwordString = request.getParameter("password");// 获取输入的密码
response.setContentType("text/html;charset=utf-8");
PrintWriter printWriter = response.getWriter();
if (!LoginCheck.check(usernumString, passwordString)) {// 检查用户名密码,匹配数据库
printWriter.println("密码错误");
} else {
HttpSession httpSession = request.getSession();
httpSession.setAttribute("usernum", usernumString);
printWriter.println("");
}
}
}