用户综合案例
01_用户列表
02_删除用户
03_批量删除
04_添加用户
05_修改用户
06_BaseServlet的应用
07_BaseServlet的优化
01_用户列表
@Override
public List< User> selectUserList ( ) throws Exception {
return new QueryRunner ( JDBCUtil. getDataSource ( ) )
. query ( "select * from tb_user" ,
new BeanListHandler < User> ( User. class ) ) ;
}
@WebServlet ( name = "ShowIndexServlet" , urlPatterns = "/showIndex" )
public class ShowIndexServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
User existUser = ( User) request. getSession ( ) . getAttribute ( "existUser" ) ;
StringBuffer responseBody = new StringBuffer ( ) ;
if ( null != existUser) {
responseBody. append ( "欢迎回来~ " + existUser. getUsername ( ) ) ;
responseBody. append ( " 注销 " ) ;
UserDao userDao = new UserDaoImpl ( ) ;
try {
List< User> userList = userDao. selectUserList ( ) ;
System. out. println ( userList) ;
responseBody. append ( "") ;
responseBody. append ( "") ;
responseBody. append ( " ID ") ;
responseBody. append ( " 账户 ") ;
responseBody. append ( " 密码 ") ;
responseBody. append ( " " ) ;
for ( User user : userList) {
responseBody. append ( "") ;
responseBody. append ( " "+ user. getId ( ) + " ") ;
responseBody. append ( " "+ user. getUsername ( ) + " ") ;
responseBody. append ( " "+ user. getPassword ( ) + " ") ;
responseBody. append ( " " ) ;
}
responseBody. append ( "
" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
} else {
responseBody. append ( "您还没有登录,请登录 " ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
response. getWriter ( ) . write ( responseBody. toString ( ) ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost ( request, response) ;
}
}
02_删除用户
开发步骤
首页,给每一个记录后面添加一个删除按钮
用户列表不要显示当前登录用户
代码实现
@Override
public void deleteUserById ( Integer id) throws Exception {
new QueryRunner ( JDBCUtil. getDataSource ( ) )
. update ( "delete from tb_user where id = ?" ,
id) ;
}
responseBody. append ( "") ;
responseBody. append ( "") ;
responseBody. append ( " ID ") ;
responseBody. append ( " 账户 ") ;
responseBody. append ( " 密码 ") ;
responseBody. append ( " 操作 ") ;
responseBody. append ( " " ) ;
for ( User user : userList) {
if ( user. getId ( ) != existUser. getId ( ) ) {
responseBody. append ( "") ;
responseBody. append ( " "+ user. getId ( ) + " ") ;
responseBody. append ( " "+ user. getUsername ( ) + " ") ;
responseBody. append ( " "+ user. getPassword ( ) + " ") ;
responseBody. append ( " 删除 ") ;
responseBody. append ( " " ) ;
}
}
responseBody. append ( "
" ) ;
@WebServlet ( name = "DeleteUserServlet" , urlPatterns = "/deleteUser" )
public class DeleteUserServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String idStr = request. getParameter ( "id" ) ;
Integer id = Integer. parseInt ( idStr) ;
UserDao userDao = new UserDaoImpl ( ) ;
try {
userDao. deleteUserById ( id) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
response. sendRedirect ( "/day60/showIndex" ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost ( request, response) ;
}
}
03_批量删除
开发流程
在首页需要给每一个记录新加多选框
当点击批量删除按钮之后,完成批量删除操作
代码实现
public void deleteUsersByIds ( List< Integer> idList) throws Exception {
for ( Integer id : idList) {
deleteUserById ( id) ;
}
}
responseBody. append ( " ) ;
responseBody. append ( "" ) ;
responseBody. append ( "批量删除 " ) ;
responseBody. append ( "" ) ;
04_添加用户
开发步骤
首页加入一个添加用户按钮
点击添加用户按钮进入到登录页面
代码实现
public void addUser ( User inputUser) throws Exception {
new QueryRunner ( JDBCUtil. getDataSource ( ) )
. update ( "insert into tb_user(username ,password) values(?,?)" ,
inputUser. getUsername ( ) ,
inputUser. getPassword ( ) ) ;
}
responseBody. append ( "欢迎回来~ " + existUser. getUsername ( ) ) ;
responseBody. append ( " 注销 " ) ;
responseBody. append ( "添加用户 " ) ;
- AddUserServlet
@WebServlet ( name = "AddUserServlet" , urlPatterns = "/addUser" )
public class AddUserServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request. getParameter ( "username" ) ;
String password = request. getParameter ( "password" ) ;
User inputUser = new User ( ) ;
inputUser. setUsername ( username) ;
inputUser. setPassword ( password) ;
UserDao userDao = new UserDaoImpl ( ) ;
try {
userDao. addUser ( inputUser) ;
response. sendRedirect ( "/day60/showIndex" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
response. sendRedirect ( "/day60/add.html" ) ;
}
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost ( request, response) ;
}
}
05_修改用户
@Override
public void updateUser ( User user) throws Exception {
new QueryRunner ( JDBCUtil. getDataSource ( ) )
. update ( "update tb_user set username = ? , password = ? where id = ?" ,
user. getUsername ( ) ,
user. getPassword ( ) ,
user. getId ( ) ) ;
}
for ( User user : userList) {
if ( user. getId ( ) != existUser. getId ( ) ) {
responseBody. append ( "") ;
responseBody. append ( " ") ;
responseBody. append ( " "+ user. getId ( ) + " ") ;
responseBody. append ( " "+ user. getUsername ( ) + " ") ;
responseBody. append ( " "+ user. getPassword ( ) + " ") ;
responseBody. append ( " " +
"删除 " +
"修改 " +
" ") ;
responseBody. append ( " " ) ;
}
}
@WebServlet ( name = "ShowUpdateUserServlet" , urlPatterns = "/showUpdateUser" )
public class ShowUpdateUserServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer. parseInt ( request. getParameter ( "id" ) ) ;
UserDao userDao = new UserDaoImpl ( ) ;
StringBuffer responseBody = new StringBuffer ( ) ;
try {
User user = userDao. selectUserById ( id) ;
System. out. println ( user) ;
responseBody. append ( " ) ;
responseBody. append ( " " ) ;
responseBody. append ( "账户: " ) ;
responseBody. append ( "密码: " ) ;
responseBody. append ( "修改 " ) ;
responseBody. append ( "" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
response. getWriter ( ) . write ( responseBody. toString ( ) ) ;
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost ( request, response) ;
}
}
@WebServlet ( name = "UpdateUserServlet" , urlPatterns = "/updateUser" )
public class UpdateUserServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer id = Integer. parseInt ( request. getParameter ( "id" ) ) ;
String username = request. getParameter ( "username" ) ;
String password = request. getParameter ( "password" ) ;
System. out. println ( "username : " + username + ", password : " + password) ;
User user = new User ( id, username, password) ;
UserDao userDao = new UserDaoImpl ( ) ;
try {
userDao. updateUser ( user) ;
response. sendRedirect ( "/day60/showIndex" ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
response. sendRedirect ( "/day60/showUpdateUser?id=" + id) ;
}
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost ( request, response) ;
}
}
06_BaseServlet的应用
@WebServlet ( name = "UserServlet" , urlPatterns = "/user" )
public class UserServlet extends BaseServlet {
public void login ( HttpServletRequest request, HttpServletResponse response) {
String username = request. getParameter ( "username" ) ;
String password = request. getParameter ( "password" ) ;
User inputUser = new User ( ) ;
inputUser. setUsername ( username) ;
inputUser. setPassword ( password) ;
UserDao userDao = new UserDaoImpl ( ) ;
try {
User existUser = userDao. login ( inputUser) ;
if ( null == existUser) {
request. getRequestDispatcher ( "/login.html" ) . forward ( request, response) ;
} else {
request. getSession ( ) . setAttribute ( "existUser" , existUser) ;
response. sendRedirect ( "/day61/user?methodName=showIndex" ) ;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public void showIndex ( HttpServletRequest request , HttpServletResponse response) throws Exception {
System. out. println ( "showIndex" ) ;
User existUser = ( User) request. getSession ( ) . getAttribute ( "existUser" ) ;
StringBuffer responseBody = new StringBuffer ( ) ;
if ( null != existUser) {
responseBody. append ( "欢迎回来~~~" + existUser. getUsername ( ) ) ;
responseBody. append ( "注销登录 " ) ;
} else {
responseBody. append ( "您还没有登录;" ) ;
responseBody. append ( "请登录 " ) ;
}
response. setContentType ( "text/html;charset=utf-8" ) ;
response. getWriter ( ) . write ( responseBody. toString ( ) ) ;
}
public void logout ( HttpServletRequest request , HttpServletResponse response) throws Exception {
request. getSession ( ) . invalidate ( ) ;
response. sendRedirect ( "/day61/login.html" ) ;
}
}
07_BaseServlet的优化
@WebServlet ( name = "BaseServlet" , urlPatterns = "/base" )
public class BaseServlet extends HttpServlet {
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String methodName = request. getParameter ( "methodName" ) ;
try {
Method method = this . getClass ( ) . getMethod ( methodName, HttpServletRequest. class , HttpServletResponse. class ) ;
if ( null != method) {
String returnValue = ( String) method. invoke ( this , request, response) ;
if ( null != returnValue) {
int index = returnValue. lastIndexOf ( ":" ) ;
if ( - 1 == index) {
request. getRequestDispatcher ( returnValue) . forward ( request, response) ;
} else {
String path = returnValue. substring ( index+ 1 ) ;
if ( returnValue. startsWith ( "redirect" ) ) {
response. sendRedirect ( request. getContextPath ( ) + path) ;
} else if ( returnValue. startsWith ( "forward" ) ) {
request. getRequestDispatcher ( path) . forward ( request, response) ;
}
}
}
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost ( request, response) ;
}
}