//注册- [ ] 获取register的数据,从表单传过来
将(账户,密码,用户名)上面的数据写入数据库中,用jdbc(插入)加载数据库驱动,连接数据库,发送SQL
加载数据库有可能失败保险起见抛一个异常
返回判断,如果注册成功则提醒用户注册成功,并且跳转到登录页面进行登录。如果注册失败则提醒用户注册失败,并返回到注册页面。
最后关闭数据库
等等
//登录- [ ] 去数据库里面的user表中查询看能不能找到对应相应的用户,如果找到能不能匹配他相应的密码看一不一样。
接收来自login输入的值:用户名和密码。将用户名与数据库的usercode进行查找(加载数据库驱动,然后连接数据库,然后发送SQL)
将密码取出来与接收到的密码进行匹配,如果两个密码一致,则证明登录成功。如果不一致则说明密码输入错误。
如果找不到则说明用户名错误
然后加入语句来提示。
//相关代码
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.time.LocalDate;
//主界面
@WebServlet("/Main")
public class Main 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);
if(session == null || session.getAttribute("username") == null){
RequestDispatcher dispatcher = request.getRequestDispatcher("/LogOutMain.html");
dispatcher.forward(request, response);
}
else{
PrintWriter out = response.getWriter();
String username = (String)session.getAttribute("username");
boolean hasEvent = false;
rsId = stmt.executeQuery("select userid from users where username = '" + username + "'");
rsId.next();
int userid = rsId.getInt(1);
rsEvent = stmt.executeQuery("select date from e" + userid + " order by date");
if(rsEvent.next()){
Date eventDate = rsEvent.getDate(1);
Date now = Date.valueOf(LocalDate.now());
if(now.equals(eventDate)) hasEvent = true;
}
out.print("""
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>日历</title>
<style>
.right-align{
text-align: right;
}
</style>
</head>
<body>
<div class="right-align">
<strong>你好 <a href="/crm/UserInfo">""" + username + """
</a><strong>
</div>
<br><br><br><br><br><br><br><br>
<script type="text/javascript">
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var dayOfWeek = date.getDay();
var arr = ["日", "一", "二", "三", "四", "五", "六"];
document.write("<h1><center>" + year + "年" + (month + 1) + "月" + day + "日</center></h1><br><center><h2>星期" + arr[dayOfWeek] + "</h2></center>");
</script>
<br><br><br><br><br><br><br><br>
<center>
<a href="/crm/LogOut">退出登录</a>
<a href="/crm/Close">账号注销</a>
<a href="/crm/Events">查看事件""" + (hasEvent ? "(今日有待办)" : "<br>") + """
</a>
</center>
</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(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 "Main";
}
@Override
public void destroy(){
}
}
{
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 UserAct;
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.;
import java.io.;
import java.sql.;
//登出
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.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.;
import java.time.LocalDate;
//登出
@WebServlet("/UserAct.Events")
public class Events 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 username = (String)session.getAttribute("username");
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");
int eventCnt = 0;
String result = "";
Date now = Date.valueOf(LocalDate.now());
Date lastDate = null;
while(rsEvent.next()){
Date eventDate = rsEvent.getDate("date");
if(eventDate.compareTo(now) < 0){
int cnt = stmt.executeUpdate("delete from e" + userid + " where date = '" + eventDate + "'");
stmt.executeUpdate("update users set eventCnt = eventCnt - " + cnt + " where username = '" + username + "'");
}
else if(!eventDate.equals(lastDate)){
lastDate = eventDate;
if(eventDate.equals(now))
result += "<h2>今日</h2>";
else result += "<h2>" + eventDate + "</h2>";
}
eventCnt ++;
result += "<strong>" + eventCnt + "</strong> " + rsEvent.getString("info") + "<br>";
}
PrintWriter out = response.getWriter();
out.print("""
<html>
<head>
<meta charset="utf-8"/>
<title>事件簿</title>
</head>
<body>
<form action="DeleteEvent" method="post">
<label for="username">删除事件(输入事件编号)</label> <br>
<input type="text" name="delete"/> <br><br>
<input type="submit" value="删除"/> *过期事件会自动删除,添加过期事件是无效添加<hr>
</form>
<form action="AddEvent" method="post">
<label for="username">添加事件</label> <br>
<label for="username">日期</label>
<input type="text" name="year"/>年
<input type="text" name="month"/>月
<input type="text" name="date"/>日 <br><br>
<textarea rows="10" cols="40" name="event" placeholder="在此输入具体内容"></textarea> <br><br>
<input type="submit" value="添加"/><hr>
</form>
<a href="Main">返回主界面</a> <br><br>
<h1>事件簿</h1>""" + result + """
</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(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.Events";
}
@Override
public void destroy(){
}
}
package UserAct;
import jakarta.servlet.;
import jakarta.servlet.annotation.WebServlet;
import java.io.;
import jakarta.servlet.http.;
import java.sql.;
//用户信息
@WebServlet("/UserAct.UserInfo")
public class UserInfo 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);
String username = (String)session.getAttribute("username");
int userid = 0, eventCnt = 0;
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();
PrintWriter out = response.getWriter();
rs = stmt.executeQuery("select userid, eventCnt from users where username = '" + username + "'");
rs.next();
userid = rs.getInt(1);
eventCnt = rs.getInt(2);
out.print("""
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<title>用户信息</title>
</head>
<body>
<h1>""" + username + """
</h1> <hr>
<h2>用户id</h2>""" + userid + """
<br>
<h2>用户事件数量</h2>""" + eventCnt + """
<br><br>
<a href="#" onclick="window.history.back(); return false">返回</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.UserInfo";
}
@Override
public void destroy(){
}
}
标签:username,jdbc,Java,request,记事簿,stmt,SQLException,null,public From: https://www.cnblogs.com/qgmz/p/18254267