RegisterServlet
package servlets;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Register
*/
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String registerSatate="Fail";
String targetUrl="/registerFail.jsp";
PreparedStatement ps=null;
Connection conn=null;
String account=request.getParameter("account");
String pwd=request.getParameter("password");
String cname=request.getParameter("cname");
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/aoligei","root","");
Statement stat=conn.createStatement();
String sql="select * from T_CUSTOMER where account=?";
ps=conn.prepareStatement(sql);
ps.setString(1, account);
ResultSet rs=ps.executeQuery();
if(!rs.next()){
String sql2="insert into T_CUSTOMER(account,password,cname)values(?,?,?)";
ps=conn.prepareStatement(sql2);
ps.setString(1, account);
ps.setString(2, pwd);
ps.setString(3, cname);
int i=ps.executeUpdate();
if(i>0){
registerSatate="Success";
targetUrl="/registerSuccess.jsp";
}
}
request.setAttribute("registerSatate", registerSatate);
ServletContext application=this.getServletContext();
RequestDispatcher rd=application.getRequestDispatcher(targetUrl);
rd.forward(request, response);
ps.close();
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LoginServlet
package servlets;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String account = request.getParameter("account");
String password = request.getParameter("password");
PreparedStatement ps=null;
Connection conn=null;
String loginState = "Fail";
String targetUrl = "/loginFail.jsp";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/aoligei","root","");
Statement stat=conn.createStatement();
String sql="select * from t_customer where account=? and password=?";
ps=conn.prepareStatement(sql);
ps.setString(1, account);
ps.setString(2, password);
ResultSet rs=ps.executeQuery();
if(rs.next()){
loginState = "Success";
targetUrl = "/loginSuccess.jsp";
HttpSession session = request.getSession();
session.setAttribute("account", account);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
request.setAttribute("loginState", loginState);
ServletContext application = this.getServletContext();
RequestDispatcher rd =
application.getRequestDispatcher(targetUrl);
rd.forward(request, response);
}
}
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'register.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<script type="text/javascript">
function register(){
var id=document.registerForm.id.value;
var pwd=document.registerForm.password.value;
var pwds=document.registerForm.passwords.value;
var cname=document.registerForm.cname.value;
if(id==""||pwd==""||pwds==""||cname==""){
alert("请填写完整的信息");
}else{
if(pwd!=pwds){
alert("两次密码输入不一致");
}else{
var xmlHttp=new XMLHttpRequest();
var url="servlets/RegisterServlet?account="+id+"&password="+pwd+"&cname="+cname;
url=encodeURI(url);
xmlHttp.open("post", url, true);
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
resultDiv.innerHTML=xmlHttp.responseText;
}
else{
resultDiv.innerHTML+="正在注册,请稍后......"
}
}
xmlHttp.send();
}
}
}
</script>
欢迎注册学生管理系统<br><br/>
<div id="resultDiv">
<form name="registerForm">
请输入账号:<input name="account" type="text" ><br><br>
请输入密码:<input name="password" type="password" ><br><br>
请确认密码:<input name="passwords" type="password" ><br><br>
请输入姓名:<input name="cname" type="text" ><br><br>
<input type="button" value="注册" onclick="register()">
</form>
</div>
</body>
</html>
注意!type不能是submit
registerFail
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'REgisterFail.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>注册失败,可能是两次密码不一致,信息填写不完全,账号已被使用等原因</h3>s <br>
</body>
</html>
registerSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'registerSuccess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h3>注册成功 !!!</h3><br>
</body>
</html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
function login(){
var account = document.loginForm.account.value;
var password = document.loginForm.password.value;
var xmlHttp=new XMLHttpRequest();
var url = "servlets/LoginServlet?account="+account+"&password="+password;
xmlHttp.open("POST", url, true);
xmlHttp.onreadystatechange=function() {
if (xmlHttp.readyState==4) {
resultDiv.innerHTML = xmlHttp.responseText;
}
else{
resultDiv.innerHTML += "正在登录,请稍候......";
}
}
xmlHttp.send();
}
</SCRIPT>
<h2>欢迎登陆学生管理系统</h2><br><hr><br>
<div id="resultDiv">
<form name="loginForm">
请输入您的账号:<input type="text" name="account"><br><br>
请输入您的密码:<input type="password" name="password"><br><br>
<input type="button" value="登录" onclick="login()">
</form>
</div>
</body>
</html>
loginFail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginFail.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
对不起,登录失败!<BR>
请您检查是否:<BR>
账号名写错<BR>
密码写错
</body>
</html>
loginSuccess.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'loginSuccess.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
欢迎${account}登录成功!<BR>
您可以选择以下功能:<BR>
<a href="">查询学生</a><BR>
<a href="">修改学生资料</a><BR>
<a href="">修改用户资料</a><BR>
<a href="">退出</a><BR>
</body>
</html>