非常感谢孙浩老师。
servlet:
package cn.lichen.servlet; import java.io.IOException; public class UserServlet extends HttpServlet { private static final long serialVersionUID = 1L; private UserService us; private IUserService service; @Override protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws servletexception,IOException { req.setCharacterEncoding("UTF-8"); us = new UserService(); service = us.getUserServicePort(); // 标识不同的方法 String method = req.getParameter("method"); if (method == null || "".equals(method)) { list(req,resp); } else if (method.equals("add")) { add(req,resp); } else if (method.equals("login")) { login(req,resp); } else if (method.equals("delete")) { delete(req,resp); } } private void delete(HttpServletRequest req,HttpServletResponse resp) { String username = req.getParameter("username"); service.delete(username); list(req,resp); } private void login(HttpServletRequest req,HttpServletResponse resp) { String username = req.getParameter("username"); String password = req.getParameter("password"); try { req.setAttribute("user",service.login(username,password)); } catch (UserException_Exception e) { e.printstacktrace(); } list(req,resp); } private void add(HttpServletRequest req,HttpServletResponse resp) { User user = new User(); user.setUsername(req.getParameter("username")); user.setNickname(req.getParameter("nickname")); user.setPassword(req.getParameter("password")); try { service.add(user); } catch (UserException_Exception e) { e.printstacktrace(); } list(req,resp); } @Override protected void doPost(HttpServletRequest req,IOException { doGet(req,resp); } private void list(HttpServletRequest request,HttpServletResponse response) { try { request.setAttribute("users",service.list()); Requestdispatcher dis = request.getRequestdispatcher("list.jsp"); dis.forward(request,response); } catch (servletexception e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } }
list.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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"> </head> <style type="text/css"> table { width: 50%; border-top: 1px solid black; border-left: 1px solid black; text-align: center; letter-spacing: 3px; } th { background-color: #00ff90; } th,td { border-bottom: 1px solid black; border-right: 1px solid black; } </style> <body> 当前用户:${requestScope.user.nickname} <br> <table id="tt" class="usertable" cellspacing="0" cellpadding="0"> <tr> <th>用户名</th> <th>呢称</th> <th>密码</th> <th>操作</th> </tr> <c:forEach var="user" items="${requestScope.users}"> <tr> <td>${user.username }</td> <td>${user.nickname }</td> <td>${user.password }</td> <td><input type='button' value='删除' onclick="javascript:location.href='user.do?username=${user.username }&method=delete'"/></td> </tr> </c:forEach> </table> </body> </html>
add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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,keyword3"> <Meta http-equiv="description" content="This is my page"> </head> <body> 当前用户:${requestScope.user.nickname} <br> <form action="user.do" method="post"> <input type="hidden" name="method" value="add"> UserName:<input type="text" name="username"/><br/> Password:<input type="text" name="password"/><br/> Nickname:<input type="text" name="nickname"/><br/> <input type="submit"> </form> </body> </html>
login:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <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,keyword3"> <Meta http-equiv="description" content="This is my page"> </head> <body> 当前用户:${requestScope.user.nickname} <br> <form action="user.do" method="post"> <input type="hidden" name="method" value="login"> UserName:<input type="text" name="username"/><br/> Password:<input type="text" name="password"/><br/> <input type="submit"> </form> </body> </html>
得到所有user信息:
添加user:
登入:
删除:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。