Servlet+Jsp—查、删
从一开始的网络编程(TCP编程对Socket进行监听,到HTTP编程),现在终于可以使用强大的API进行JavaEE编程了,Servlet底层封装了各种网络协议的处理,使我们不再深陷于对协议的编程,我们只需要处理请求和返回响应就行了。这次是个简单的用Servlet类来处理对用户的简单的增删改查,我们通过对实体类与数据库的映射来将数据传送到JSP页面上进行展示。 模式如下,controller层、dao层、pojo层(用户实体类)、utils层(封装jdbc)1.登录页面(在写代码之前导入jdbc的jar包)
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>login</title> </head> <body> <form action="/ServletJSPLX/LoginCheck"> username:<input type="text" name="username"><br> password:<input type="text" name="password"><br> <input type="submit" value="登录"> </form> </body> </html>
2.实体用户类
public class User { public User() {} public User(String username, Integer userno, String job, String password, BigDecimal sal) { super(); this.username = username; this.userno = userno; this.job = job; this.password = password; this.sal = sal; } public String username; public Integer userno; public String job; public String password; public BigDecimal sal; public String getUserame() { return username; } public void setUsername(String username) { this.username = username; } public Integer getUserno() { return userno; } public void setUserno(Integer userno) { this.userno = userno; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public BigDecimal getSal() { return sal; } public void setSal(BigDecimal sal) { this.sal = sal; } }
3.dao层的接口和实现类
//接口 //查询全部员工信息 public List<User> getAllUser(); //删除信息 public void deleteUser(int userno); //实现类,分别是查询所有用户和根据userno删除 @Override public List<User> getAllUser() { // TODO Auto-generated method stub Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; try { conn = JdbcUtils.getConnection(); ps = conn.prepareStatement("select * from user"); rs = ps.executeQuery(); List list = new ArrayList<User>(); while(rs.next()) { String username = rs.getString("username"); int userno = rs.getInt("userno"); String job = rs.getString("job"); String password = rs.getString("password"); BigDecimal sal = rs.getBigDecimal("sal"); User user = new User(username,userno,job,password,sal); list.add(user); } return list; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new RuntimeException(e); }finally { JdbcUtils.close(conn, ps, rs); } } @Override public void deleteUser(int userno) { // TODO Auto-generated method stub Connection conn=null; PreparedStatement ps=null; try { conn = JdbcUtils.getConnection(); ps = conn.prepareStatement("delete from user where userno=?"); ps.setInt(1,userno); int count=ps.executeUpdate(); if(count==0) { throw new RuntimeException("666"); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); throw new RuntimeException(e); }finally { JdbcUtils.close(conn, ps, null); } }
4.login登录页面会跳转到检查页面,检查是否有该用户,如果有则继续,如果没有则跳转到error.jsp
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); String username=request.getParameter("username"); String password=request.getParameter("password"); Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; boolean loginsuccess=false; try { conn = JdbcUtils.getConnection(); ps = conn.prepareStatement("select * from user where username=? and password=?"); ps.setString(1,username); ps.setString(2,password); rs = ps.executeQuery(); if(rs.next()) { loginsuccess=true; } conn.commit(); } catch (Exception e) { // TODO: handle exception if(conn!=null) { try { conn.rollback(); } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } e.printStackTrace(); }finally { JdbcUtils.close(conn, ps, rs); } if(loginsuccess) { response.sendRedirect(request.getContextPath()+"/ListUser"); }else { request.getRequestDispatcher("/error.jsp"); } }
5.这时候成功之后跳转到该页面,回调用之前接口写的方法
//查询 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub UserDaoImpl userDaoImpl = new UserDaoImpl(); List<User> allUser = userDaoImpl.getAllUser(); request.setAttribute("allUser",allUser); request.getRequestDispatcher("/user/list.jsp").forward(request, response); } //删除 @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String userno=request.getParameter("userno"); UserDaoImpl userDaoImpl = new UserDaoImpl(); userDaoImpl.deleteUser(new Integer(userno)); response.sendRedirect(request.getContextPath()+"/ListUser"); return; }
6.然后就可以在jsp页面显示出来,在这里又加了一个文件夹user,安全
<body> <% List<User> users=(List<User>)request.getAttribute("allUser"); %> <table> <td> <th>username</th> <th>userno</th> <th>job</th> <th>password</th> <th>sal</th> </td> <% for(int i=0;i<users.size();i++){ User user=users.get(i); %> <tr> <td><%=user.getUserame() %></td> <td><%=user.getUserno()%></td> <td><%=user.getJob() %></td> <td><%=user.getPassword() %></td> <td><%=user.getSal() %></td> <td> <a href="<%=request.getContextPath()%>/DeleteUser?userno=<%=user.getUserno()%>">del</a> </td> </tr> <% } %> </table> </body>
数据库表如下
如下图就是成功登录显示的页面,按del可以进行删除
标签:username,ps,String,Jsp,password,Servlet,public,userno From: https://www.cnblogs.com/YHSDDJM/p/16886957.html