首页 > 其他分享 >Servlet+Jsp—查、删

Servlet+Jsp—查、删

时间:2022-11-13 21:14:43浏览次数:54  
标签:username ps String Jsp password Servlet public userno

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

相关文章

  • day17-Servlet06
    Servlet0615.HttpServletResponse15.1HttpServletResponse介绍每次HTTP请求,Tomcat都会创建一个HttpServletResponse对象传递给Servlet程序使用HttpServletRequest表示......
  • servlet的使用
    JavaServlet是运行在Web服务器或应用服务器上的程序,它是作为来自Web浏览器或其他HTTP客户端的请求和HTTP服务器上的数据库或应用程序之间的中间层。使用Servle......
  • jsp 学习
    <%@pageimport="java.util.ArrayList"%><%@pageimport="java.util.HashMap"%><%@pagecontentType="text/html;charset=UTF-8"language="java"%><%@taglibpr......
  • servlet 案例
    importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServl......
  • Servlet
    概念servlet?whatisservlet,相比单调的前端,是一种前后端一体化的动态web资源开发,,是服务器的Java程序,工作原理doGet和doPost方法是居基于http的post和get方法doGet()......
  • SAP 电商云 Spartacus UI Angular UI 和 Accelerator JSP UI 的混合使用
    Spartacus在技术和架构(library发布方式vs模板发布方式,headlessvsembedded,AngularvsJSP技术栈)等各方面,都是一种全面的从Accelerator的完整范式转变。因此,对于C......
  • jsp::过滤器和监听器
    Filter过滤器用于拦截请求和响应,并对响应进行检查和修改,只是起过滤作用,本身没有请求和响应对象,建立过滤器five步骤建立了filter接口类doFilter方法中实现过滤FilterC......
  • Servlet3.0新特性:异步处理,太好用了!!!
    以下转载于https://blog.csdn.net/weixin_44742132/article/details/117137408Servlet3.0新特性:异步处理,太好用了!!!互联网全栈架构于 2021-05-2109:52:41 发......
  • day16-Servlet05
    Servlet0514.HttpServletRequestHttpServletRequest对象代表客户端的请求当客户端/浏览器通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中通过......
  • JSP—El表达式,java脚本,java表达式,jstl标签库
    jsp简介JSP(全称JavaServerPages)是由SunMicrosystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成HTML、XML或其他格式文档的We......