//相关代码
package UserAct;
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.;
import java.io.;
import java.sql.;
//登出
@WebServlet("/UserAct.DeleteEvent")
public class DeleteEvent implements Servlet{
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException{
this.config = config;
}
@Override
public ServletConfig getServletConfig(){
return config;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;UTF-8");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null, rsId = null, rsEvent = null;;
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HTTPServer", "root", "lcy333668");
stmt = conn.createStatement();
HttpSession session = ((HttpServletRequest)request).getSession(false);
String username = (String)session.getAttribute("username");
String req = request.getParameter("delete");
int line = 0;
if(req == null || req.isEmpty()){
RequestDispatcher dispatcher = request.getRequestDispatcher("/UserAct/NullDelete.html");
dispatcher.forward(request, response);
}
else{
line = Integer.parseInt(req);
rs = stmt.executeQuery("select eventCnt from users where username = '" + username + "'");
rs.next();
int eventCnt = rs.getInt(1);
if(line > eventCnt){
RequestDispatcher dispatcher = request.getRequestDispatcher("/UserAct/SegmentationFault.html");
dispatcher.forward(request, response);
}
else{
rsId = stmt.executeQuery("select userid from users where username = '" + username + "'");
rsId.next();
int userid = rsId.getInt(1);
rsEvent = stmt.executeQuery("select * from e" + userid + " order by date");
while(line -- > 0) rsEvent.next();
String event = rsEvent.getString("info");
stmt.executeUpdate("delete from e" + userid + " where info = '" + event + "'");
stmt.executeUpdate("update users set eventCnt = eventCnt - 1 where username = '" + username + "'");
((HttpServletResponse)response).sendRedirect("/crm/Events");
}
}
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn != null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
if(stmt != null)
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rs != null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rsEvent != null)
try{
rsEvent.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rsId != null)
try{
rsId.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
@Override
public String getServletInfo(){
return "UserAct.DeleteEvent";
}
@Override
public void destroy(){
}
}
package UserAct;
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.;
import java.io.;
import java.sql.;
//登出
@WebServlet("/UserAct.Close")
public class Close implements Servlet{
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException{
this.config = config;
}
@Override
public ServletConfig getServletConfig(){
return config;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;UTF-8");
HttpSession session = ((HttpServletRequest)request).getSession(false);
if(session == null || session.getAttribute("username") == null){
System.out.println("NullSession");
return;
}
else System.out.println("YES");
String username = (String)session.getAttribute("username");
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HTTPServer", "root", "lcy333668");
stmt = conn.createStatement();
rs = stmt.executeQuery("select userid from users where username = '" + username + "'");
rs.next();
int userid = rs.getInt(1);
stmt.executeUpdate("delete from users where username = '" + username + "'");
stmt.execute("drop table e" + userid);
session.invalidate();
PrintWriter out = response.getWriter();
out.print("""
<html>
<head>
<meta charset="utf-8"/>
<title>注销成功</title>
</head>
<body>
<h1>账号已注销!</h1>
<a href="/crm/Main">回到主页面</a>
</body>
</html>
""");
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn != null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
if(stmt != null)
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rs != null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
@Override
public String getServletInfo(){
return "UserAct.Close";
}
@Override
public void destroy(){
}
}
package UserAct;
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.;
import java.io.;
import java.sql.;
//登出
@WebServlet("/UserAct.AddEvent")
public class AddEvent implements Servlet{
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException{
this.config = config;
}
@Override
public ServletConfig getServletConfig(){
return config;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;UTF-8");
Connection conn = null;
Statement stmt = null;
ResultSet rsId = null, rsEvent = null;
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HTTPServer", "root", "lcy333668");
stmt = conn.createStatement();
HttpSession session = ((HttpServletRequest)request).getSession(false);
String year = request.getParameter("year");
String month = request.getParameter("month");
String date = request.getParameter("date");
String event = request.getParameter("event");
String username = (String)session.getAttribute("username");
rsId = stmt.executeQuery("select userid from users where username = '" + username + "'");
rsId.next();
int userid = rsId.getInt(1);
if(year == null || year.isEmpty() || month == null || month.isEmpty() || date == null || date.isEmpty()
|| event == null || event.isEmpty()){
RequestDispatcher dispatcher = request.getRequestDispatcher("/UserAct/NullEvent.html");
dispatcher.forward(request, response);
}
else{
rsEvent = stmt.executeQuery("select * from e" + userid + " where date = '" + year + "-" + month + "-" + date + "' and info = '" + event + "'");
if(rsEvent.next()){
RequestDispatcher dispatcher = request.getRequestDispatcher("/UserAct/SameEvent.html");
dispatcher.forward(request, response);
}
else{
stmt.executeUpdate("insert into e" + userid + " values('" + year + "-" + month + "-" + date + "', '" + event + "')");
stmt.executeUpdate("update users set eventCnt = eventCnt + 1 where username = '" + username + "'");
}
((HttpServletResponse)response).sendRedirect("/crm/Events");
}
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn != null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
if(stmt != null)
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rsId != null)
try{
rsId.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rsEvent != null)
try{
rsEvent.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
@Override
public String getServletInfo(){
return "UserAct.AddEvent";
}
@Override
public void destroy(){
}
}
package Login;
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.;
import java.io.;
import java.sql.;
//登录处理
@WebServlet("/Login.Login")
public class Login implements Servlet{
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException{
this.config = config;
}
@Override
public ServletConfig getServletConfig(){
return config;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HTTPServer", "root", "lcy333668");
stmt = conn.createStatement();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
RequestDispatcher dispatcher = null;
if(username == null || username.isEmpty())
dispatcher = request.getRequestDispatcher("/Login/NullUsername.html");
else if(password == null || password.isEmpty())
dispatcher = request.getRequestDispatcher("/Login/NullPassword.html");
else{
rs = stmt.executeQuery("select password from users where username = '" + username + "' and password = '" + password + "'");
if(rs.next()){
HttpSession session = ((HttpServletRequest)request).getSession();
session.setAttribute("username", username);
((HttpServletResponse)response).sendRedirect("/crm/Main");
}
else{
dispatcher = request.getRequestDispatcher("/Login/Incorrect.html");
dispatcher.forward(request, response);
}
}
if(dispatcher != null) dispatcher.forward(request, response);
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn != null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
if(stmt != null)
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rs != null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
@Override
public String getServletInfo(){
return "Login.Login handling form submissions";
}
@Override
public void destroy(){
}
}
package SignUp;
import jaka rta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import java.io.;
import java.sql.*;
//注册处理
@WebServlet("/SignUp.SignUp")
public class SignUp implements Servlet{
private ServletConfig config;
@Override
public void init(ServletConfig config) throws ServletException{
this.config = config;
}
@Override
public ServletConfig getServletConfig(){
return config;
}
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/HTTPServer", "root", "lcy333668");
stmt = conn.createStatement();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirmPassword = request.getParameter("confirmPassword");
System.out.println(password + " " + confirmPassword);
RequestDispatcher dispatcher = null;
if(username == null || username.isEmpty())
dispatcher = request.getRequestDispatcher("/SignUp/NullUsername.html");
else if(!nameIsValid(username, stmt))
dispatcher = request.getRequestDispatcher("/SignUp/InvalidUsername.html");
else if(password == null || password.isEmpty())
dispatcher = request.getRequestDispatcher("/SignUp/NullPassword.html");
else if(!passwordIsValid(password))
dispatcher = request.getRequestDispatcher("/SignUp/InvalidPassword.html");
else if(confirmPassword == null || confirmPassword.isEmpty())
dispatcher = request.getRequestDispatcher("/SignUp/NullConfirmPassword.html");
else if(!confirmPassword.equals(password))
dispatcher = request.getRequestDispatcher("/SignUp/UnequalPassword.html");
else{
boolean flag = true;
rs = stmt.executeQuery("select username from users where username = '" + username + "'");
while(rs.next())
if(username.equals(rs.getString(1))){
dispatcher = request.getRequestDispatcher("/SignUp/UserNameExists.html");
flag = false;
break;
}
if(flag){
//添加注册成功的用户,并添加一张表,用来记录该用户的事件
stmt.executeUpdate("insert into users(username, password, eventCnt) values('" + username + "', '" + password + "', 0)");
rs = stmt.executeQuery("select userid from users where username = '" + username + "' order by userid desc");
rs.next();
int userid = rs.getInt(1);
stmt.execute("drop table if exists e" + userid);
stmt.execute(
"create table e" + userid + "(" +
"date date not null," +
"info varchar(1000))"
);
dispatcher = request.getRequestDispatcher("/SignUp/Successfully.html");
}
}
if(dispatcher != null) dispatcher.forward(request, response);
}catch(SQLException e){
throw new RuntimeException(e);
}finally{
if(conn != null)
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
if(stmt != null)
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
if(rs != null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
@Override
public String getServletInfo(){
return "SignUp.SignUp handling form submissions";
}
@Override
public void destroy(){
}
//检查用户名的合法性
public static boolean nameIsValid(String name, Statement stmt){
for(char i : name.toCharArray())
if(i == '\\') return false;
ResultSet rs = null;
try{
rs = stmt.executeQuery("select length('" + name + "')");
rs.next();
if(rs.getInt(1) > 30) return false;
}catch(SQLException e){
e.printStackTrace();
}finally{
if(rs != null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
return true;
}
//检查密码的合法性
public static boolean passwordIsValid(String password){
if(password.length() < 7 || password.length() > 16) return false;
for(int i : password.toCharArray())
if(i == '\\' || (i < 33 || i > 126)) return false;
return true;
}
}
标签:username,jdbc,Java,rs,request,记事簿,stmt,null,public From: https://www.cnblogs.com/lhyy/p/18252580