实现功能 1.用户信息注册 2.用户信息新增 3.用户信息列表 4.用户信息删除 5.用户信息修改 6.用户登录 项目包结构:
sql sever数据库结构
代码语言:javascript复制USE [MyDB]
GO
/****** Object: Table [dbo].[users] Script Date: 2020/11/11 9:26:07 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[users](
[id] [int] IDENTITY(1,1) NOT NULL,
[user_name] [varchar](64) NULL,
[pwd] [varchar](64) NULL,
[age] [int] NULL,
[sex] [varchar](64) NULL,
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
工具类:
代码语言:javascript复制package org.zhangsan.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=MyDB", "sa",
"1");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (conn != null)
conn.close();
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static int update(String sql, Object[] objs) {
Connection conn = DBUtil.getConn();
PreparedStatement ps = null;
int count = 0;
try {
ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i ) {
ps.setObject(i 1, objs[i]);
}
count = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
close(conn, ps, null);
}
return count;
}
}
dao层:
代码语言:javascript复制package org.zhangsan.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.zhangsan.bean.User;
import org.zhangsan.util.DBUtil;
public class UserDao {
public static ArrayList<User> getUsers() {
Connection conn = DBUtil.getConn();
String sql = "select *from users";
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<User> users = new ArrayList<User>();
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("user_name"));
user.setPwd(rs.getString("pwd"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
users.add(user);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return users;
}
public static User getUserByID(Integer id) {
Connection conn = DBUtil.getConn();
String sql = "select * from users where id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("user_name"));
user.setPwd(rs.getString("pwd"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return user;
}
public static User login(String userName, String pwd) {
Connection conn = DBUtil.getConn();
String sql = "select *from users where user_name= ? and pwd = ?";
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
rs = ps.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getString("user_name"));
user.setPwd(rs.getString("pwd"));
user.setAge(rs.getInt("age"));
user.setSex(rs.getString("sex"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return user;
}
}
bean层
代码语言:javascript复制package org.zhangsan.bean;
public class User {
private Integer id;
private String userName;
private String pwd;
private Integer age;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User(Integer id, String userName, String pwd, Integer age, String sex) {
super();
this.id = id;
this.userName = userName;
this.pwd = pwd;
this.age = age;
this.sex = sex;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
}
注册Servlet
代码语言:javascript复制package org.zhangsan.action;
import java.io.IOException;
import java.io.PrintWriter;
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 org.zhangsan.bean.User;
import org.zhangsan.dao.UserDao;
import org.zhangsan.util.DBUtil;
public class RegServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");
String ageStr = request.getParameter("age");
Integer age = Integer.parseInt(ageStr);
String sql = "insert into users (user_name, pwd ,age,sex) values(?,?,?,?)";
Object[] objs = {
userName, pwd, age, sex };
int count = DBUtil.update(sql, objs);
if (count > 0) {
//注册成功
response.sendRedirect("userList");
} else {
//注册失败
response.sendRedirect("reg.jsp");
}
}
}
新增Servlet
代码语言:javascript复制package org.zhangsan.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.zhangsan.util.DBUtil;
public class UserDeleteServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
String sql = "delete from users where id=?";
Object[] objs = {
id };
DBUtil.update(sql, objs);
response.sendRedirect("userList");
}
}
删除Servlet
代码语言:javascript复制在这里插入代码片
package org.zhangsan.action;
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.zhangsan.bean.User; import org.zhangsan.dao.UserDao; import org.zhangsan.util.DBUtil;
public class UserToUpdateServlet extends HttpServlet {
代码语言:javascript复制public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
User user = UserDao.getUserByID(id);
request.setAttribute("user", user);
request.getRequestDispatcher("userUpdate.jsp").forward(request,
response);
}
}
代码语言:javascript复制2.
```java
package org.zhangsan.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.zhangsan.util.DBUtil;
public class UserUpdateServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String sex = request.getParameter("sex");
String ageStr = request.getParameter("age");
Integer age = Integer.parseInt(ageStr);
String sql = "update users set user_name= ? ,pwd= ?,sex= ? ,age= ? where id= ?";
Object[] objs = { userName, pwd, sex, age, id };
DBUtil.update(sql, objs);
response.sendRedirect("userList");
}
}
登录Servlet
代码语言:javascript复制package org.zhangsan.action;
import java.io.IOException;
import java.io.PrintWriter;
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 org.zhangsan.bean.User;
import org.zhangsan.dao.UserDao;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
User user = UserDao.login(userName, pwd);
if (user != null) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("main.jsp");
} else {
request.setAttribute("msg", "登录失败");
request.getRequestDispatcher("login.jsp")
.forward(request, response);
}
}
}
列表Servlet 1.
代码语言:javascript复制package org.zhangsan.action;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.zhangsan.bean.User;
import org.zhangsan.dao.UserDao;
import org.zhangsan.util.DBUtil;
public class UserInfoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String idStr = request.getParameter("id");
Integer id = Integer.parseInt(idStr);
User user = UserDao.getUserByID(id);
request.setAttribute("user", user);
request.getRequestDispatcher("userInfo.jsp").forward(request, response);
}
}
package org.zhangsan.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.zhangsan.bean.User;
import org.zhangsan.dao.UserDao;
public class UserListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList<User> users = UserDao.getUsers();
request.setAttribute("users", users);
request.getRequestDispatcher("userList.jsp").forward(request, response);
}
}
webXML配置:
代码语言:javascript复制<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>RegServlet</servlet-name>
<servlet-class>org.zhangsan.action.RegServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>org.zhangsan.action.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UserListServlet</servlet-name>
<servlet-class>org.zhangsan.action.UserListServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UserDeleteServlet</servlet-name>
<servlet-class>org.zhangsan.action.UserDeleteServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UserToUpdateServlet</servlet-name>
<servlet-class>org.zhangsan.action.UserToUpdateServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UserUpdateServlet</servlet-name>
<servlet-class>org.zhangsan.action.UserUpdateServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>UserInfoServlet</servlet-name>
<servlet-class>org.zhangsan.action.UserInfoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RegServlet</servlet-name>
<url-pattern>/regServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserListServlet</servlet-name>
<url-pattern>/userList</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserDeleteServlet</servlet-name>
<url-pattern>/userDel</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserToUpdateServlet</servlet-name>
<url-pattern>/userToUpdate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserUpdateServlet</servlet-name>
<url-pattern>/userUpdate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserInfoServlet</servlet-name>
<url-pattern>/userInfo</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
view层: 登录界面
代码语言:javascript复制<%@ 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 'index.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>
<form action="loginServlet" method="post">
用户名
<input type="text" name="userName" />
<br />
密码
<input type="text" name="pwd" />${msg }
<br />
<input type="submit" name="登录" />
</form>
</body>
</html>
session界面
代码语言:javascript复制在这里插入代码片
main界面
代码语言:javascript复制在这里插入代码片<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@include file="session.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.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>
登录后的用户可以查看
</body>
</html>
注册界面
代码语言:javascript复制<%@ 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 'index.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>
<form action="regServlet" method="post">
用户名
<input type="text" name="userName" />
<br />
密码
<input type="text" name="pwd" />
<br />
年龄
<input type="text" name="age" />
<br />
性别
<input type="text" name="sex" />
<br />
<input type="submit" name="注册" />
</form>
</body>
</html>
用户信息界面
代码语言:javascript复制<%@ 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 'index.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>
<div>
用户信息详情页面
</div>
<body>
编号
<span>${user.id }</span>
<br />
用户名
<span>${user.userName }</span>
<br />
密码
<span>${user.pwd }</span>
<br />
年龄
<span>${user.age }</span>
<br />
性别
<span>${user.sex }</span>
<br />
<a href="userList">返回列表</a>
</form>
</body>
</html>
用户列表界面
代码语言:javascript复制<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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 'index.jsp' starting page</title>
</head>
<body>
<div>
<a href="reg.jsp">新增用户</a>
</div>
<table border="1px" cellspacing="0">
<thead>
<tr>
<th>
编号
</th>
<th>
用户名
</th>
<th>
密码
</th>
<th>
年龄
</th>
<th>
性别
</th>
<th>
操作
</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>
${user.id }
</td>
<td>
<a href="userInfo?id=${user.id }">${user.userName }</a>
</td>
<td>
${user.pwd }
</td>
<td>
${user.age }
</td>
<td>
${user.sex }
</td>
<td>
<a href="userDel?id=${user.id }"> 删除</a>
<a href="userToUpdate?id=${user.id }"> 修改</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
用户信息修改界面
代码语言:javascript复制<%@ 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 'index.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>
<div>
用户信息修改页面
</div>
<body>
<form action="userUpdate" method="post">
编号
<input type="text" name="id" value="${user.id }" readonly="readonly" />
<br />
用户名
<input type="text" name="userName" value="${user.userName }" />
<br />
密码
<input type="text" name="pwd" value="${user.pwd }" />
<br />
年龄
<input type="text" name="age" value="${user.age }" />
<br />
性别
<input type="text" name="sex" value="${user.sex }" />
<br />
<input type="submit" value="更新" />
</form>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/181122.html原文链接:https://javaforall.cn