java代码实现Java代码实现基于AJAX技术,在用户注册时,校验用户名是否在数据已存在数据库中。 1)若存在,点击登录按钮,在用户登录页面login.html,完成登录系统操作; 2)若不存在,点击注册按钮,进入注册页面 ,并在register.html页面完成注册。设计思路: 1.视图层:register.html、login.html。 2.Servlet层: 1)LoginServlet.java //1. 获取到请求中的参数; //2. 和数据库中的内容进行比较; //3. 如果比较通过,创建会话,返回响应状态码; 2)RegisterServlet.java //1.获取请求用户名、密码等; //2.通过用户名查询对象
时间: 2024-01-12 18:02:44 浏览: 215
是否存在于数据库中; //3.如果存在,返回错误响应信息;如果不存在,将用户信息保存到数据库中,返回成功响应信息。 3.前端JS层:使用AJAX技术,向Servlet层发送请求,接收响应信息,并做相应处理。以下是实现代码:
register.html
```
<html>
<head>
<title>注册页面</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#register').click(function() {
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
url: 'RegisterServlet',
data: {'username': username, 'password': password},
type: 'POST',
dataType: 'json',
success: function(data) {
if (data.result == "success") {
alert("注册成功!");
window.location.href = "login.html";
} else {
alert("注册失败,请重试!");
}
}
});
});
});
</script>
</head>
<body>
<h3>用户注册</h3>
<form>
用户名:<input type="text" id="username" name="username" placeholder="请输入用户名"><br>
密码:<input type="password" id="password" name="password" placeholder="请输入密码"><br>
<button type="button" id="register">注册</button>
</form>
</body>
</html>
```
login.html
```
<html>
<head>
<title>登录页面</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#login').click(function() {
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
url: 'LoginServlet',
data: {'username': username, 'password': password},
type: 'POST',
dataType: 'json',
success: function(data) {
if (data.result == "success") {
alert("登录成功!");
window.location.href = "homepage.html";
} else {
alert("登录失败,请重试!");
}
}
});
});
});
</script>
</head>
<body>
<h3>用户登录</h3>
<form>
用户名:<input type="text" id="username" name="username" placeholder="请输入用户名"><br>
密码:<input type="password" id="password" name="password" placeholder="请输入密码"><br>
<button type="button" id="login">登录</button>
</form>
</body>
</html>
```
RegisterServlet.java
```
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public void init() throws ServletException {
String url = "jdbc:mysql://localhost:3306/user";
String username = "root";
String password = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
JSONObject resultJson = new JSONObject();
try {
pstmt = conn.prepareStatement("select * from user where username=?");
pstmt.setString(1, username);
rs = pstmt.executeQuery();
if (rs.next()) {
resultJson.put("result", "fail");
} else {
pstmt = conn.prepareStatement("insert into user(username, password) values(?, ?)");
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
resultJson.put("result", "success");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(resultJson);
out.flush();
out.close();
}
public void destroy() {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
LoginServlet.java
```
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 com.alibaba.fastjson.JSONObject;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
public void init() throws ServletException {
String url = "jdbc:mysql://localhost:3306/user";
String username = "root";
String password = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
JSONObject resultJson = new JSONObject();
try {
pstmt = conn.prepareStatement("select * from user where username=? and password=?");
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if (rs.next()) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
resultJson.put("result", "success");
} else {
resultJson.put("result", "fail");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
response.setContentType("application/json;charset=UTF-8");
PrintWriter out = response.getWriter();
out.print(resultJson);
out.flush();
out.close();
}
public void destroy() {
try {
if (rs != null) rs.close();
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
阅读全文