- User实体类:
简单的定义属性,然后生成getter/setter方法
public class User {
private int id;
private String name;
private int age;
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
private String pwd;
public User() {
}
public User(int id, String name, int age, String pwd) {
this.id = id;
this.name = name;
this.age = age;
this.pwd = pwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 登录界面 index.jsp
简单的登录界面 登录由ajax发起
<%--
Created by IntelliJ IDEA.
User: Rtwo
Date: 2020/3/23
Time: 20:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function () {
$("#login").click(function () {
var username = $("#username").val();//取值
var password = $("#password").val();
if (!username) {
alert("用户名必填!");
$("#username").focus();//获取焦点
return;
}
if (!password) {
alert("密码必填!");
$("#password").focus();//获取焦点
return;
}
$.ajax({
url: "login",
type: "post",
data: {
username: username,
password: password
},
dataType: "text",
success: function (data) {
if (data === '1') {
window.location.href = "userlist?name="+username+"&pwd="+password;
} else {
alert("用户名或密码错误!请重新输入!");
}
},
error: function () {
alert("错误");
}
});
});
});
</script>
<title>登录</title>
</head>
<body>
<center>
<h1>登录</h1>
<table border="0">
<tr>
<td>账号:</td>
<td><input type="text" class="loginform_input" id="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" class="loginform_input" id="password">
</td>
</tr>
</table>
<br>
<input id="login" type="submit" class="loginform_submit" value="登录">
</center>
</body>
</html>
- UserListServlet.class
登录成功后转到这里,从数据库查询用户表,再将查到的数据传给main.jsp
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.bosssoft.hr.train.web.entity.User;
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @author Rtwo
*/
@Slf4j
@WebServlet("/UserListServlet")
public class UserListServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
Connection conn = DbUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from t_user";
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
List<User> users = new ArrayList<>();
// int index = -1;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
User user = new User(rs.getInt(1), rs.getString(2), rs.getInt(3), rs.getString(4));
users.add(user);
}
// for (int i = 0; i < users.size(); i++) {
// if (name.equals(users.get(i).getName()) && pwd.equals(users.get(i).getPwd())) {
// index = i;
// }
// }
} catch (Exception e) {
log.info(e.getMessage());
} finally {
DbUtil.closeConn(conn, ps, rs);
}
HttpSession session = request.getSession();
session.setAttribute("userList", users);
// session.setAttribute("current", index);
response.sendRedirect("main.jsp");
}
}
- 主界面 main.jsp
在此界面可以操作增删查改
<%@ page import="com.bosssoft.hr.train.web.entity.User" %>
<%@ page import="java.util.List" %>
<%--
Created by IntelliJ IDEA.
User: 13701
Date: 2020/3/23
Time: 19:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作用户</title>
</head>
<body>
<%
// int index = (int) session.getAttribute("current");
List<User> users = (List) session.getAttribute("userList");
%>
<%--<center>--%>
<%-- <a>当前用户:</a>--%>
<%-- <a id="current"><%=users.get(index).getName()%>--%>
<%-- </a>--%>
<%--</center>--%>
<table border="1" cellspacing="0" align="center" width="1000dp"
height="500dp">
<thead>
<tr>
<td>
编号
</td>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
操作
</td>
</tr>
</thead>
<tbody>
<tr>
<%
for (User user : users) {
%>
<td><%=user.getId()%>
</td>
<td><%=user.getName()%>
</td>
<td><%=user.getAge()%>
</td>
<td>
<a href="delete?id=<%=user.getId() %>">删除</a>
<a href="updateshow?id=<%=user.getId() %>">更新</a>
</td>
</tr>
<%
}
%>
</tbody>
</table>
<center>
<a href="insert.jsp">新增</a>
<a href="search.jsp">查询</a>
</center>
</body>
</html>
- 删除用户 DeleteServlet.class
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Rtwo
*/
@Slf4j
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
//接受到将要删除的id
String id = request.getParameter("id");
Connection conn = DbUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "delete from t_user where id = ?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, id);
response.sendRedirect("UserListServlet");
} catch (SQLException e) {
log.info(e.getMessage());
} finally {
DbUtil.closeConn(conn, ps, rs);
}
}
}
- 更新用户界面逻辑 UpdateShowServlet.class
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Rtwo
*/
@Slf4j
@WebServlet("/UpdateShowServlet")
public class UpdateShowServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String id = request.getParameter("id");
//获取要删除数据的id
Connection conn = DbUtil.getConn();
//查询出该id的所有字段内容
String sql = "select * from t_user where id=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
if (rs.next()) {
request.setAttribute("id", rs.getInt(1));
request.setAttribute("name", rs.getString(2));
request.setAttribute("age", rs.getInt(3));
/*
*将数据发送到一个jsp页面,在该页面进行人工的数据的修改
*/
request.getRequestDispatcher("update.jsp").forward(request, response);
}
} catch (SQLException e) {
log.info(e.getMessage());
} finally {
DbUtil.closeConn(conn, ps, rs);
}
}
}
- 更新用户界面 update.jsp
<%@ page import="com.bosssoft.hr.train.web.entity.User" %>
<%@ page import="java.util.List" %>
<%--
Created by IntelliJ IDEA.
User: 13701
Date: 2020/3/23
Time: 19:02
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>更新用户</title>
</head>
<style type="text/css">
.input1 {
border: 1px solid #CDC28D; /* 输入框表框颜色*/
background-color: #F2F9FD; /* 输入框背景颜色*/
height: 40px; /*输入框高度*/
width: 200px; /*输入框宽度*/
padding-top: 4px;
/*输入框里的文字和输入框上边之间的距离
font-family: Arial, Helvetica, sans-serif; /*输入框文字类型*/
font-size: 12px; /*输入框内文字大小*/
padding-left: 10px; /*输入框里的文字和输入框左边之间的距离*/
}
.input2 {
border: 1px solid #1E1E1E; /* 输入框表框颜色*/
background-color: #F2F9FD; /* 输入框背景颜色*/
height: 30px; /*输入框高度*/
width: 80px; /*输入框宽度*/
}
</style>
<body>
<form action="UpdateServlet" method="post">
<table align="center">
<tr height="150dp">
<td></td>
</tr>
<tr>
<td colspan="2"><h1>用户更新</h1></td>
</tr>
<tr>
<td>ID:</td>
<td><input class="input1" type="text" name="id" readonly="readonly"
value="<%=request.getAttribute("id") %>"/></td>
</tr>
<tr>
<td>姓名:</td>
<td><input class="input1" type="text" name="name" value="<%=request.getAttribute("name") %>"/></td>
</tr>
<tr>
<td>年龄:</td>
<td><input class="input1" type="text" name="age" value="<%=request.getAttribute("age") %>"/></td>
</tr>
<tr align="center">
<td colspan="2"><input class="input2" type="submit" value="更新"/></td>
</tr>
</table>
</form>
</body>
</html>
- 更新用户信息 UpdateServlet.class
将更新后的数据写到数据库,然后返回主界面
import com.bosssoft.hr.train.web.util.DbUtil;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author Rtwo
*/
@Slf4j
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
//获取到要更新的数据
Connection conn = DbUtil.getConn();
PreparedStatement ps = null;
ResultSet rs = null;
//更新语句
String sql = "update t_user set name=?, age=? where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setInt(2, Integer.parseInt(age));
ps.setInt(3, Integer.parseInt(id));
response.sendRedirect("UserListServlet");
} catch (SQLException e) {
log.info(e.getMessage());
} finally {
DbUtil.closeConn(conn, ps, rs);
}
}
}
标签:总结,每日,request,public,2023.3,sql,import,servlet,id
From: https://www.cnblogs.com/fuchuchu/p/17173637.html