Ajax登陆注册
程序员文章站
2022-04-18 12:13:51
...
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>
推荐阅读
-
php模式设计之 注册树模式,php模式设计
-
使用php完成一个用户注册以及管理的demo(php实现单文件与多文件的上传)
-
ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI的界面错位的解决方法_jquery
-
php 用户注册并且设置为己登录状态实现方法_PHP教程
-
php+ajax实现无刷新数据分页的办法,ajax分页_PHP教程
-
PHP与js搭配的ajax中出现如下错误,不知道什么原因
-
ajax.call和jquery.ajax比较,该如何处理
-
Ajax如何实现从前端不刷新页面就可以到后端取到数据
-
关于静态注册BroadcastReceiver接收不到非系统广播的问题
-
python_爬虫_通过selenium获取人人网cookie值并模拟登陆个人界面