精华内容
下载资源
问答
  • JSP实现选中删除功能

    千次阅读 2018-11-17 08:48:59
    选中删除  1.实现所使用到的技术:  JavaScript做的复选框的“全选和全不选功能”;  form表单提交;  使用Bootstrap框架,在超链接按钮做提交按钮时,使用JavaScri...

    选中删除


              1.实现所使用到的技术:


                                   JavaScript做的复选框的“全选和全不选功能”;

                                   form表单提交;

                                   使用Bootstrap框架,在超链接按钮做提交按钮时,使用JavaScript:void(0)使href属性失效;

                                   复选框的默认值value的设置;

                                   确认框(提示框)的使用confirm("您确定抛弃我们吗?")。

               2.核心代码:  

      
                        
               全选/全不选        

      function checkAll(obj) {
            var flag = obj.checked;
            //alert(flag);
            var items = document.getElementsByClassName("item");
            for(var i in items){
                items[i].checked=flag;
            }
       }
                                   web层

    String[] ids = request.getParameterValues("ids");
    ContactService contactService = new ContactService();
    contactService.delAll(ids);
    //删除成功后,重定向 查询所有联系人信息
    response.sendRedirect(request.getContextPath()+"/findAll");


                                  service层

    public void delAll(String[] ids) throws Exception{
        //1.获取连接池对象
        DataSource dataSource = JDBCUtils.getDataSource();
        //2.创建jdbcTemplate实例 // template从连接池中获取一个连接 conn1
        JdbcTemplate template = new JdbcTemplate(dataSource);

        //3.启动事务管理器(将conn和当前线程做绑定)
        TransactionSynchronizationManager.initSynchronization();
        //4.获取连接 : 获取JdbcTemplate所使用的连接对象
        Connection conn = DataSourceUtils.getConnection(dataSource);  // conn1
        try {
            //5.将连接的事务,设置为手动事务提交
            conn.setAutoCommit(false);

            //===== 业务处理
            for(String id:ids){ // [1,2,....]
                contactDao.delById(id,template);
            }

            // 提交事务
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            // 事务回顾
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            // 将异常抛给调用者,告知执行失败
            throw e;
        } finally {
            // 将conn对象和当前线程解除绑定
            TransactionSynchronizationManager.clearSynchronization();
            // 修改为自动事务提交
            try {
                conn.setAutoCommit(true);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
                                 Dao层

    String sql="delete from contact where id=? ";
    template.update(sql,id);

                         

               

    展开全文
  • jsp 实现修改和删除功能

    千次阅读 2019-06-19 23:29:00
    main.jsp 实现查询 在此界面快捷方式到修改界面 点击修改 会把数据传递到exit.jsp 修改 edit.jsp 前面数据: 数据库: 1 /* 2 Navicat Premium Data Transfer 3 4 Source Server : c10....

    main.jsp   实现查询 在此界面快捷方式到修改界面   

    点击修改  会把数据传递到exit.jsp

    修改   edit.jsp

     

     

     

    前面数据:

    数据库:

     1 /*
     2  Navicat Premium Data Transfer
     3 
     4  Source Server         : c10.87.12.251
     5  Source Server Type    : SQL Server
     6  Source Server Version : 11002100
     7  Source Host           : 10.87.12.251:1433
     8  Source Catalog        : userdb
     9  Source Schema         : dbo
    10 
    11  Target Server Type    : SQL Server
    12  Target Server Version : 11002100
    13  File Encoding         : 65001
    14 
    15  Date: 19/06/2019 22:19:40
    16 */
    17 
    18 
    19 -- ----------------------------
    20 -- Table structure for userinfo
    21 -- ----------------------------
    22 IF EXISTS (SELECT * FROM sys.all_objects WHERE object_id = OBJECT_ID(N'[dbo].[userinfo]') AND type IN ('U'))
    23     DROP TABLE [dbo].[userinfo]
    24 GO
    25 
    26 CREATE TABLE [dbo].[userinfo] (
    27   [id] int IDENTITY(1,1) NOT NULL,
    28   [username] varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
    29   [userpwd] varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
    30   [sex] varchar(50) COLLATE Chinese_PRC_CI_AS NULL,
    31   [age] int NULL,
    32   [address] varchar(50) COLLATE Chinese_PRC_CI_AS NULL
    33 )
    34 GO
    35 
    36 ALTER TABLE [dbo].[userinfo] SET (LOCK_ESCALATION = TABLE)
    37 GO
    38 
    39 
    40 -- ----------------------------
    41 -- Records of [userinfo]
    42 -- ----------------------------
    43 SET IDENTITY_INSERT [dbo].[userinfo] ON
    44 GO
    45 
    46 INSERT INTO [dbo].[userinfo] ([id], [username], [userpwd], [sex], [age], [address]) VALUES (N'1', N'root', N'123', N'1', N'1', N'1')
    47 GO
    48 
    49 SET IDENTITY_INSERT [dbo].[userinfo] OFF
    50 GO
    用户信息数据库代码

    (1)db/DbConn.java

     1 package db;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.ResultSet;
     6 import java.sql.SQLException;
     7 import java.sql.Statement;
     8 
     9 public class DbConn {
    10 
    11     public  static Connection  getConn()
    12     {
    13         Connection con =null;
    14         try {
    15             //    Class.forName("com.mysql.jdbc.Driver"); // 加载驱动程序
    16                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    17 
    18             } catch (ClassNotFoundException e) {
    19                 System.out.println("加载驱动程序错误" + e.getMessage());
    20             }
    21             
    22             try {
    23                 // 创建连接 testdb是数据库名称
    24                  con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=userdb", "sa", "123456");
    25 
    26             } catch (SQLException e) {
    27 
    28                 System.out.println("数据库连接操作出错" + e.getMessage());
    29             }
    30         return con;
    31     }
    32 }
    l连接数据库代码

    (2)  po/User.java

     1 package po;
     2 
     3 public class User {
     4     int id;
     5     String username;
     6     String password;
     7     int age;
     8     String sex;
     9     String address;
    10     
    11     public int getId() {
    12         return id;
    13     }
    14     public void setId(int id) {
    15         this.id = id;
    16     }
    17     public String getUsername() {
    18         return username;
    19     }
    20     public void setUsername(String username) {
    21         this.username = username;
    22     }
    23     public String getPassword() {
    24         return password;
    25     }
    26     public void setPassword(String password) {
    27         this.password = password;
    28     }
    29     public int getAge() {
    30         return age;
    31     }
    32     public void setAge(int age) {
    33         this.age = age;
    34     }
    35     public String getSex() {
    36         return sex;
    37     }
    38     public void setSex(String sex) {
    39         this.sex = sex;
    40     }
    41     public String getAddress() {
    42         return address;
    43     }
    44     public void setAddress(String address) {
    45         this.address = address;
    46     }
    47 
    48 }
    交互相关字段

    (3)  dao/UserDAO.java

     1     //根据id 查找字段 返回给edit表
     2     public  User  getUser(int id)
     3     {
     4         User u=new User();
     5         try {
     6             // 创建连接 testdb是数据库名称
     7             Connection con =DbConn.getConn();
     8                 // 创建声明SQL对象
     9             Statement stm = con.createStatement();
    10             // 执行SQL语句,得到结果集,结果集放到ResultSet对象中
    11             ResultSet rs = stm.executeQuery("select * from userinfo where id="+id);
    12             // 通过循环,从结果集对象中取得数据
    13             while (rs.next()) {
    14                 
    15                 String username = rs.getString("username"); // 取得字符类型的字段username的值,
    16                 String userpwd = rs.getString("userpwd");
    17                 String sex=rs.getString("sex");
    18                 int age=rs.getInt("age");
    19                 String address=rs.getString("address");
    20                 
    21                   u.setId(id);
    22                   u.setUsername(username);
    23                   u.setPassword(userpwd);
    24                   u.setSex(sex);
    25                   u.setAge(age);
    26                   u.setAddress(address);
    27                  
    28             }
    29         } catch (SQLException e) {
    30 
    31             System.out.println("数据库操作出错" + e.getMessage());
    32         }
    33         return u;
    34     }
    35 //插入新数据  根据id
    36     public int edit(User u)
    37     {
    38         int n=0;
    39         try {
    40             // 创建连接 testdb是数据库名称
    41           Connection con = DbConn.getConn();
    42             
    43             // 创建声明SQL对象
    44             Statement stm = con.createStatement();
    45             // 执行SQL语句,得到结果集,结果集放到ResultSet对象中
    46             String sql="update userinfo set username='"+u.getUsername()+"',userpwd='"+u.getPassword()+"',sex='"+u.getSex()+"',age="+u.getAge()+",address='"+u.getAddress()+"' " +
    47                     "where id="+u.getId()+"";
    48             n=stm.executeUpdate(sql);
    49         } catch (SQLException e) {
    50 
    51             System.out.println("数据库操作出错" + e.getMessage());
    52         }
    53         return n;
    54     }
    根据id 查找字段返回给edit表和插入数据
     1 public  List<User> getUserList()
     2     {
     3         List<User> ls=new ArrayList<User>();
     4         try {
     5             // 创建连接 testdb是数据库名称
     6             Connection con =DbConn.getConn();
     7                 // 创建声明SQL对象
     8             Statement stm = con.createStatement();
     9             // 执行SQL语句,得到结果集,结果集放到ResultSet对象中
    10             ResultSet rs = stm.executeQuery("select * from userinfo");
    11             // 通过循环,从结果集对象中取得数据
    12             while (rs.next()) {
    13                 int id = rs.getInt("id"); // 取得int类型的字段id的值,
    14                 String username = rs.getString("username"); // 取得字符类型的字段username的值,
    15                 String userpwd = rs.getString("userpwd");
    16                 String sex=rs.getString("sex");
    17                 int age=rs.getInt("age");
    18                 String address=rs.getString("address");
    19                 User u=new User();
    20                   u.setId(id);
    21                   u.setUsername(username);
    22                   u.setPassword(userpwd);
    23                   u.setSex(sex);
    24                   u.setAge(age);
    25                   u.setAddress(address);
    26                   ls.add(u);
    27             }
    28         } catch (SQLException e) {
    29 
    30             System.out.println("数据库操作出错" + e.getMessage());
    31         }
    32         return ls;
    33     }
    查询所有信息
     1     public boolean login(String username,String userpwd)
     2     {
     3          boolean flag=false;
     4         try {
     5             // 创建连接 testdb是数据库名称
     6             Connection con = DbConn.getConn();
     7             // 创建声明SQL对象
     8             Statement stm = con.createStatement();
     9             // 执行SQL语句,得到结果集,结果集放到ResultSet对象中
    10             ResultSet rs = stm.executeQuery("select * from userinfo where username='"+username+"' and userpwd='"+userpwd+"' ");
    11             // 通过循环,从结果集对象中取得数据
    12             if(rs.next()) {
    13                 flag=true;
    14             }
    15             else
    16             {
    17                flag=false;
    18             }
    19 
    20         } catch (SQLException e) {
    21 
    22             System.out.println("数据库操作出错" + e.getMessage());
    23         }
    24         return flag;
    25     }
    login 验证

     

     

    (4)servlet

    main里面的 <td><a href="servlet/GetUserServlet?userid=<%=u.getId()%>">修改</a></td>  调用  GetUserServlet   调用UserDAO. getUser(int id)  方法

     

    GetUserServlet.java

     1 package servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 import java.util.List;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 import javax.servlet.http.HttpSession;
    12 
    13 import po.User;
    14 import dao.UserDAO;
    15 
    16 public class GetUserServlet extends HttpServlet {
    17 
    18     /**
    19      * Constructor of the object.
    20      */
    21     public GetUserServlet() {
    22         super();
    23     }
    24 
    25     /**
    26      * Destruction of the servlet. <br>
    27      */
    28     public void destroy() {
    29         super.destroy(); // Just puts "destroy" string in log
    30         // Put your code here
    31     }
    32 
    33     /**
    34      * The doGet method of the servlet. <br>
    35      *
    36      * This method is called when a form has its tag value method equals to get.
    37      * 
    38      * @param request the request send by the client to the server
    39      * @param response the response send by the server to the client
    40      * @throws ServletException if an error occurred
    41      * @throws IOException if an error occurred
    42      */
    43     public void doGet(HttpServletRequest request, HttpServletResponse response)
    44             throws ServletException, IOException {
    45         doPost(request,response);
    46     }
    47 
    48     /**
    49      * The doPost method of the servlet. <br>
    50      *
    51      * This method is called when a form has its tag value method equals to post.
    52      * 
    53      * @param request the request send by the client to the server
    54      * @param response the response send by the server to the client
    55      * @throws ServletException if an error occurred
    56      * @throws IOException if an error occurred
    57      */
    58     public void doPost(HttpServletRequest request, HttpServletResponse response)
    59             throws ServletException, IOException {
    60 
    61         response.setContentType("text/html");
    62         PrintWriter out = response.getWriter();
    63         out
    64                 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    65         out.println("<HTML>");
    66         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    67         out.println("  <BODY>");
    68     
    69 //根据id 查找到所有字段返回给exit.jsp
    70 
    71         HttpSession  session=request.getSession(true);
    72          String sid=request.getParameter("userid");
    73          int id=Integer.parseInt(sid);
    74          UserDAO udao=new UserDAO();
    75             User u= udao.getUser(id);
    76                  //传递给session 对象
    77              session.setAttribute("user", u);
    78             response.sendRedirect("../edit.jsp");
    79         
    80         out.println("  </BODY>");
    81         out.println("</HTML>");
    82         out.flush();
    83         out.close();
    84     }
    85 
    86     /**
    87      * Initialization of the servlet. <br>
    88      *
    89      * @throws ServletException if an error occurs
    90      */
    91     public void init() throws ServletException {
    92         // Put your code here
    93     }
    94 
    95 }
    96                        
    根据id 查找到所有字段返回给exit.jsp

    edit.jsp 修改好后的传递给

    EditServlet.java

      1 package servlet;
      2 
      3 import java.io.IOException;
      4 import java.io.PrintWriter;
      5 
      6 import javax.servlet.ServletException;
      7 import javax.servlet.http.HttpServlet;
      8 import javax.servlet.http.HttpServletRequest;
      9 import javax.servlet.http.HttpServletResponse;
     10 
     11 import po.User;
     12 import dao.UserDAO;
     13 
     14 public class EditServlet extends HttpServlet {
     15 
     16     /**
     17      * Constructor of the object.
     18      */
     19     public EditServlet() {
     20         super();
     21     }
     22 
     23     /**
     24      * Destruction of the servlet. <br>
     25      */
     26     public void destroy() {
     27         super.destroy(); // Just puts "destroy" string in log
     28         // Put your code here
     29     }
     30 
     31     /**
     32      * The doGet method of the servlet. <br>
     33      *
     34      * This method is called when a form has its tag value method equals to get.
     35      * 
     36      * @param request the request send by the client to the server
     37      * @param response the response send by the server to the client
     38      * @throws ServletException if an error occurred
     39      * @throws IOException if an error occurred
     40      */
     41     public void doGet(HttpServletRequest request, HttpServletResponse response)
     42             throws ServletException, IOException {
     43 
     44         doPost(request,response);
     45     }
     46 
     47     /**
     48      * The doPost method of the servlet. <br>
     49      *
     50      * This method is called when a form has its tag value method equals to post.
     51      * 
     52      * @param request the request send by the client to the server
     53      * @param response the response send by the server to the client
     54      * @throws ServletException if an error occurred
     55      * @throws IOException if an error occurred
     56      */
     57     public void doPost(HttpServletRequest request, HttpServletResponse response)
     58             throws ServletException, IOException {
     59 
     60         response.setContentType("text/html");
     61         PrintWriter out = response.getWriter();
     62         out
     63                 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
     64         out.println("<HTML>");
     65         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
     66         out.println("  <BODY>");
     67         //获取edit 传递的字段
     68 
     69            String userid=request.getParameter("userid");
     70            String username=request.getParameter("username");
     71             String userpwd=request.getParameter("userpwd");
     72             String sex=request.getParameter("sex");
     73             String age=request.getParameter("age");
     74             String address=request.getParameter("address");   
     75 
     76           User u=new User();
     77               u.setId(Integer.parseInt(userid));
     78               u.setUsername(username);
     79               u.setPassword(userpwd);
     80               u.setSex(sex);
     81               u.setAge(Integer.parseInt(age));
     82               u.setAddress(address);
     83               
     84           //调用方法
     85             UserDAO udao=new UserDAO();
     86                
     87               int n=udao.edit(u);
     88                 if(n>0) {
     89 //成功就调用查询方法  看修改完后的
     90                     response.sendRedirect("../servlet/GetUsersServlet");
     91                 }
     92                 else
     93                 {
     94                  out.println("修改失败");
     95                 }
     96         
     97         
     98         
     99         out.println("  </BODY>");
    100         out.println("</HTML>");
    101         out.flush();
    102         out.close();
    103     }
    104 
    105     /**
    106      * Initialization of the servlet. <br>
    107      *
    108      * @throws ServletException if an error occurs
    109      */
    110     public void init() throws ServletException {
    111         // Put your code here
    112     }
    113 
    114 }
    从edit.jsp获取数据在调用修改函数 给数据库

     

    login.jsp 调用

     1 package servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 import java.sql.*;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 
    12 import dao.UserDAO;
    13 import db.DbConn;
    14 
    15 public class LoginServlet extends HttpServlet {
    16 
    17     /**
    18      * Constructor of the object.
    19      */
    20     public LoginServlet() {
    21         super();
    22     }
    23 
    24     /**
    25      * Destruction of the servlet. <br>
    26      */
    27     public void destroy() {
    28         super.destroy(); // Just puts "destroy" string in log
    29         // Put your code here
    30     }
    31 
    32     /**
    33      * The doGet method of the servlet. <br>
    34      *
    35      * This method is called when a form has its tag value method equals to get.
    36      * 
    37      * @param request the request send by the client to the server
    38      * @param response the response send by the server to the client
    39      * @throws ServletException if an error occurred
    40      * @throws IOException if an error occurred
    41      */
    42     public void doGet(HttpServletRequest request, HttpServletResponse response)
    43             throws ServletException, IOException {
    44           doPost(request,response);
    45     }
    46 
    47     /**
    48      * The doPost method of the servlet. <br>
    49      *
    50      * This method is called when a form has its tag value method equals to post.
    51      * 
    52      * @param request the request send by the client to the server
    53      * @param response the response send by the server to the client
    54      * @throws ServletException if an error occurred
    55      * @throws IOException if an error occurred
    56      */
    57     public void doPost(HttpServletRequest request, HttpServletResponse response)
    58             throws ServletException, IOException {
    59 
    60         response.setContentType("text/html");
    61         PrintWriter out = response.getWriter();
    62         out
    63                 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    64         out.println("<HTML>");
    65         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    66         out.println("  <BODY>");
    67         
    68         String username=(String)request.getParameter("username");
    69         String userpwd=(String)request.getParameter("password");
    70   
    71               UserDAO udao=new UserDAO();
    72            boolean flag= udao.login(username, userpwd);
    73     
    74         if(flag==true){
    75             out.print("success");
    76         
    77             response.sendRedirect("../servlet/GetUsersServlet");
    78             
    79         }
    80         else
    81             out.print("false");
    82         
    83         
    84         out.println("  </BODY>");
    85         out.println("</HTML>");
    86         out.flush();
    87         out.close();
    88     }
    89 
    90     /**
    91      * Initialization of the servlet. <br>
    92      *
    93      * @throws ServletException if an error occurs
    94      */
    95     public void init() throws ServletException {
    96         // Put your code here
    97     }
    98 
    99 }
    LoginServlet.java

    查询所有GetUsersServlet.java

     1 package servlet;
     2 
     3 import java.io.IOException;
     4 import java.io.PrintWriter;
     5 import java.util.List;
     6 
     7 import javax.servlet.ServletException;
     8 import javax.servlet.http.HttpServlet;
     9 import javax.servlet.http.HttpServletRequest;
    10 import javax.servlet.http.HttpServletResponse;
    11 import javax.servlet.http.HttpSession;
    12 
    13 import po.User;
    14 
    15 import dao.UserDAO;
    16 
    17 public class GetUsersServlet extends HttpServlet {
    18 
    19     /**
    20      * Constructor of the object.
    21      */
    22     public GetUsersServlet() {
    23         super();
    24     }
    25 
    26     /**
    27      * Destruction of the servlet. <br>
    28      */
    29     public void destroy() {
    30         super.destroy(); // Just puts "destroy" string in log
    31         // Put your code here
    32     }
    33 
    34     /**
    35      * The doGet method of the servlet. <br>
    36      *
    37      * This method is called when a form has its tag value method equals to get.
    38      * 
    39      * @param request the request send by the client to the server
    40      * @param response the response send by the server to the client
    41      * @throws ServletException if an error occurred
    42      * @throws IOException if an error occurred
    43      */
    44     public void doGet(HttpServletRequest request, HttpServletResponse response)
    45             throws ServletException, IOException {
    46         doPost(request,response);
    47     }
    48 
    49     /**
    50      * The doPost method of the servlet. <br>
    51      *
    52      * This method is called when a form has its tag value method equals to post.
    53      * 
    54      * @param request the request send by the client to the server
    55      * @param response the response send by the server to the client
    56      * @throws ServletException if an error occurred
    57      * @throws IOException if an error occurred
    58      */
    59     public void doPost(HttpServletRequest request, HttpServletResponse response)
    60             throws ServletException, IOException {
    61 
    62         response.setContentType("text/html");
    63         PrintWriter out = response.getWriter();
    64         out
    65                 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    66         out.println("<HTML>");
    67         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    68         out.println("  <BODY>");
    69           HttpSession  session=request.getSession(true);
    70          
    71          UserDAO udao=new UserDAO();
    72           List<User> ls= udao.getUserList();
    73              session.setAttribute("userlist", ls);
    74             response.sendRedirect("../main.jsp");
    75             
    76         out.println("  </BODY>");
    77         out.println("</HTML>");
    78         out.flush();
    79         out.close();
    80     }
    81 
    82     /**
    83      * Initialization of the servlet. <br>
    84      *
    85      * @throws ServletException if an error occurs
    86      */
    87     public void init() throws ServletException {
    88         // Put your code here
    89     }
    90 
    91 }
    获取所有数据

    DelServlet.java

      1 package servlet;
      2 
      3 import java.io.IOException;
      4 import java.io.PrintWriter;
      5 import java.sql.*;
      6 
      7 import javax.servlet.ServletException;
      8 import javax.servlet.http.HttpServlet;
      9 import javax.servlet.http.HttpServletRequest;
     10 import javax.servlet.http.HttpServletResponse;
     11 
     12 import db.DbConn;
     13 
     14 public class DelServlet extends HttpServlet {
     15 
     16     /**
     17      * Constructor of the object.
     18      */
     19     public DelServlet() {
     20         super();
     21     }
     22 
     23     /**
     24      * Destruction of the servlet. <br>
     25      */
     26     public void destroy() {
     27         super.destroy(); // Just puts "destroy" string in log
     28         // Put your code here
     29     }
     30 
     31     /**
     32      * The doGet method of the servlet. <br>
     33      *
     34      * This method is called when a form has its tag value method equals to get.
     35      * 
     36      * @param request the request send by the client to the server
     37      * @param response the response send by the server to the client
     38      * @throws ServletException if an error occurred
     39      * @throws IOException if an error occurred
     40      */
     41     public void doGet(HttpServletRequest request, HttpServletResponse response)
     42             throws ServletException, IOException {
     43         doPost(request,response);
     44     }
     45 
     46     /**
     47      * The doPost method of the servlet. <br>
     48      *
     49      * This method is called when a form has its tag value method equals to post.
     50      * 
     51      * @param request the request send by the client to the server
     52      * @param response the response send by the server to the client
     53      * @throws ServletException if an error occurred
     54      * @throws IOException if an error occurred
     55      */
     56     public void doPost(HttpServletRequest request, HttpServletResponse response)
     57             throws ServletException, IOException {
     58 
     59         response.setContentType("text/html");
     60         PrintWriter out = response.getWriter();
     61         out
     62                 .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
     63         out.println("<HTML>");
     64         out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
     65         out.println("  <BODY>");
     66     
     67          String userid=request.getParameter("userid");
     68             out.println(userid);
     69 
     70 
     71             try {
     72                 // 创建连接 testdb是数据库名称
     73                 Connection con = DbConn.getConn();                
     74                 // 创建声明SQL对象
     75                 Statement stm = con.createStatement();
     76                 // 执行SQL语句,得到结果集,结果集放到ResultSet对象中
     77                 
     78                 int n=stm.executeUpdate(sql);
     79                 if(n>0) {
     80                     response.sendRedirect("../servlet/GetUsersServlet");    
     81                     
     82                 }
     83                 else
     84                 {
     85                  out.println("删除失败");
     86                 }
     87 
     88             } catch (SQLException e) {
     89 
     90                 System.out.println("数据库操作出错" + e.getMessage());
     91             }
     92 
     93         
     94         
     95         
     96         out.println("  </BODY>");
     97         out.println("</HTML>");
     98         out.flush();
     99         out.close();
    100     }
    101 
    102     /**
    103      * Initialization of the servlet. <br>
    104      *
    105      * @throws ServletException if an error occurs
    106      */
    107     public void init() throws ServletException {
    108         // Put your code here
    109     }
    110 
    111 }
    删除

     

     

     

    1.main.jsp 

     1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
     2 <%@ page import="po.*" %>
     3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     4 <html>
     5   <head>
     6     <title>My JSP 'main.jsp' starting page</title>
     7   </head>
     8   
     9   <body>
    10  <table width="502" border="1">
    11   <tr>
    12     <td width="37">序号</td>
    13     <td width="63">姓名</td>
    14     <td width="52">密码</td>
    15     <td width="52">年龄</td>
    16     <td width="61">性别</td>
    17     <td width="197">地址</td>
    18     <td width="80">删除</td>
    19     <td width="80">修改</td>
    20   </tr>
    21   <%
    22   
    23         List<User> ls=(List<User>)session.getAttribute("userlist");
    24         for(int i=0;i<ls.size();i++)
    25         {
    26             User u=ls.get(i);
    27             %>
    28          <tr>
    29             <td><%=u.getId()%></td>
    30             <td><%=u.getUsername()%></td>
    31             <td><%=u.getPassword()%></td>
    32             <td><%=u.getAge()%></td>
    33             <td><%=u.getSex()%></td>
    34             <td><%=u.getAddress()%></td>
    35             <td><a href="servlet/DelServlet?userid=<%=u.getId()%>">删除</a></td>
    36                <td><a href="servlet/GetUserServlet?userid=<%=u.getId()%>">修改</a></td>
    37         </tr>
    38        <% 
    39      }
    40    %>
    41    </table>
    42    <a href="add.jsp">添加</a>
    43   </body>
    44 </html>
    查询界面和修改界面

    2.修改  edit.jsp

     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2     pageEncoding="utf-8"%>
     3  <%@ page import="po.*" %>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 
    12 
    13 <% 
    14   User u=(User)session.getAttribute("user");
    15 %>
    16 <p>用户添加</p>
    17 <form  name="frm1" action="servlet/EditServlet">
    18 <p>姓名:
    19   <label for="textfield"></label>
    20   <input type="text" name="username" id="textfield" value="<%=u.getUsername() %>"/>
    21   <input type="hidden" name="userid" id="textfield" value="<%=u.getId() %>"/>
    22 </p>
    23 <p>密码:
    24   <input type="text" name="userpwd" id="textfield2"   value="<%= u.getPassword()%>"/>
    25 </p>
    26 <p>年龄:
    27   <input type="text" name="age" id="textfield3"   value="<%=u.getAge() %>"/>
    28 </p>
    29 <p>性别:
    30   <input type="text" name="sex" id="textfield4"  value="<%=u.getSex() %>"/>
    31 </p>
    32 <p>地址:
    33   <input type="text" name="address" id="textfield5"   value="<%=u.getAddress() %>"/>
    34 </p>
    35 <p>&nbsp;</p>
    36 <input type="submit" value="确定"/>
    37 </form>
    38 </body>
    39 </html>
    edit.jsp

     

    3.Login

     1 <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     
    12     <title>My JSP 'Login.jsp' starting page</title>
    13     
    14     <meta http-equiv="pragma" content="no-cache">
    15     <meta http-equiv="cache-control" content="no-cache">
    16     <meta http-equiv="expires" content="0">    
    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18     <meta http-equiv="description" content="This is my page">
    19     <!--
    20     <link rel="stylesheet" type="text/css" href="styles.css">
    21     -->
    22 
    23   </head>
    24   
    25   <body>
    26     <div align="center" >
    27        <form method="get" action="servlet/LoginServlet">
    28                用户名:<input type="text" name="username"><br>
    29             密码:<input type="password" name="password"><br>
    30             <input type="submit" name="button1" value="登录">
    31         </form>
    32     </div>
    33 </body>
    34 </html>
    Login.jsp 它调用LoginServle至main.jspt

     

     

     

    运行过程

    修改的运行过程: Login.jsp--->LoginServlet-->main.jsp-->GetUserServlet->exit.jsp->EditServlet ->main.jsp 

     

    转载于:https://www.cnblogs.com/zhenqk/p/11055735.html

    展开全文
  • 删除员工信息页面delete.jsp String id=request.getParameter("id"); try{sql.executeUpdate("delete from employee_info where employee_num="+id); con.close(); } catch(SQLException e) { out....

    删除员工信息页面delete.jsp

    <%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%>
    <%@ include file="conn.jsp" %>
    <%request.setCharacterEncoding("gb2312");
    String id=request.getParameter("id");
    try{sql.executeUpdate("delete from employee_info where employee_num="+id);
    con.close();
    }
    catch(SQLException e)
    { out.print(e);
    }
    %>
    <script language="javascript">
    alert("删除成功!");
    window.location='admin.jsp';
    </script>

    <%@ include file="conn.jsp" %>
    其中conn.jsp页面实现数据库连接功能,代码如下:

    <%@ page language="java" import="java.sql.*" pageEncoding="gb2312"%>
    <% Connection con;
    Statement sql;
    try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(ClassNotFoundException e)
    { out.print(e);
    }
    con=DriverManager.getConnection("jdbc:odbc:employee_dsn","sa","2005");
    sql=con.createStatement();

    %>
    展开全文
  • 列表页面添加批量删除按钮 效果 列表页面引入独立的js文件 ajax批量删除的后端处理规划 Servlet类 ManagerDeleteAjaxServlet url manager_delete_ajax.do ajax批量删除的后端处理...js中实现批量删除函数 ...

    列表页面添加批量删除按钮
    在这里插入图片描述
    效果
    在这里插入图片描述

    列表页面引入独立的js文件

    在这里插入图片描述

    ajax批量删除的后端处理规划
    Servlet类 ManagerDeleteAjaxServlet
    url manager_delete_ajax.do
    ajax批量删除的后端处理Servlet类

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    js中实现批量删除函数
    在这里插入图片描述

    展开全文
  • jsp:选中复选框实现删除功能

    千次阅读 2014-06-05 15:39:07
    前段时间,接到一个开发jsp前台页面的项目,好久没接触JSP了,个人感觉亚历山大。公司实在没人愿意上,只能硬着头皮接了。 在开发过程中也碰到了不少问题,乘现在项目暂告一段落,在此记录下,以后再碰到类似问题就...
  • servlet+jsp实现小小购物车功能

    万次阅读 多人点赞 2016-08-28 22:56:29
    使用jsp以及servlet实现购物车功能
  • 使用jsp实现留言板功能

    千次阅读 2020-11-18 21:50:49
    使用jsp实现留言板功能 一.开发环境 本项目使用eclipse+MySQL8.0进行开发 在开发的过程中使用了一个jar包mysql-connector-java-8.0.16.jar点我下载 二.项目结构 1.index.jsp <%@ page content...
  • jsp批量删除实现

    千次阅读 2015-05-27 23:55:10
    最近在做一个小型的项目,刚好遇到了这个批量删除的操作,因为用的是ssh框架,所以感觉实现起来也不难,分享出来给大家看看,大家有什么宝贵的意见可以回复我。 在做这个功能之前,我觉得要好好想想怎么去实现,因为...
  • JSP页面实现批量删除数据

    万次阅读 2016-10-25 09:51:25
    危楼高百尺,手可摘星辰。 不敢高声语,恐惊天上人。 —李白《夜宿山寺》 首次:FindServlet.java-->... 实现批量删除,首先获取要删除的行的id String ID[]=request.getParameterValues("delid");
  • jsp删除页面

    2019-08-07 21:21:10
    1.ajax实现删除功能 <a href="dodelete.jsp?empno=8044"> <script> result =true; 每一行给一个id $('#tr_8044').remove; doDetele.jsp String empno=request.getParameter("empno"); Emplo...
  • jsp中表单页面 /DelectServlet" method=post> 全选/反选 编号 用户名 密码 姓名 邮箱 性别 生日 爱好 ...
  • JSP+javabean实现购物车功能

    万次阅读 2016-12-05 20:24:34
    简单的小程序,java后台 + Web前端,可以实现购物车的添加,删除功能,并没有用到数据库。而是用到的session存取功能。   Product.java package shopping.cart; import java.io.
  • 复选框实现批量删除 ; charset=UTF-8"  pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> ; charset=UTF-8"> ...
  • update.jsp代码 &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt; &lt;title&gt;学员信息修改&lt;/title&gt; &...
  • ajax实现删除功能

    万次阅读 2018-09-03 16:56:16
    JSP页面: 用来显示数据: &lt;c:forEach var="news" items="${newsList}" varStatus="status"&gt; &lt;tr &lt;c:if test="${status.count%2==0}"&...
  • 购物车的基本功能实现了,比如在商品显示在页面上,对了,我的图像,是放路径的,放图片的同学可以把自己的图片换上,加入购物车的时候,判断该商品是否在购物车内,是的话直接加1 ,不是直接插入,用到了虚拟账户...
  • 前一篇文章讲解了通过Servlet获取所提交的数据,这篇文章将详细讲解MyEclipse+Servlet+JSP实现火车票管理系统的查询页面、模糊查询、修改车票信息、删除车票等操作。基础性文章,希望对读者有所帮助 ,尤其是我的...
  • JSP实现网页,分页功能

    千次阅读 2012-06-07 15:05:44
    提交到本页面“dividepage.jsp” 分页显示 学号 姓名 性别 年龄 籍贯 院系 更改 删除 /** 下面这个数据库连接,的主要目的是求出总的记录数rowCount */ try...
  • JSP实现添加图书、修改图书以及删除图书https://blog.csdn.net/qq_15267341/article/details/71995449
  • 删除功能实现

    千次阅读 2020-03-27 19:55:57
    文章目录一、目标需求二、list.jsp三、delUserServlet 一、目标需求 点击按钮可以删除此条记录,这就需要点击删除按钮时传递该记录的 id 同样依照界面层、业务逻辑层、数据访问层结构完成 二、list.jsp 这个页面...
  • 商品管理功能一、功能介绍1.1显示1.2商品详情1.3新增1.4修改1.5删除二、数据库语句三、项目结构四、java代码4.1Good类4.2 Charge类4.3GoodDao接口4.4GoodDaoImpl(GoodDao接口实现类)4.5GoodService接口4.6...
  • jsp分页功能实现

    千次阅读 2014-02-19 10:22:15
    分页查询功能一直是web编程中常用的技术,如何实现可重复使用而又简单的分页技术呢,下面的代码可以提供一些参考,实现用户列表的分页显示,当其它数据需分页显示时,可以复用其中的分页对象 (SplitPage.java),然后提供...
  • JSP+SQL实现留言功能(含留言回复功能),附源码

    万次阅读 多人点赞 2018-09-27 17:47:19
    注:记得删除注释,不然jsp编译会出错 --> 留言"/> 注:表带中的uid是当前登录用户的ID信息( 记得删除注释,不然编译会出错 ) (2)SelectMessageServlet代码介绍 public void doPost...
  • JavaWEB实现批量删除功能

    千次阅读 2020-09-27 23:12:44
    JavaWEB实现批量删除功能 先根大家说说需要实现的功能把! 说来也简单,就是将平时的单个删除记录改成批量删除,在页面中是利用复选框进行批量删除的! 思路: 首先利用javaScript获取到复选框中被选中的记录名,将...
  • JDBC连接数据库实现删除功能

    千次阅读 2015-11-27 08:49:26
    这次同样运用之前写过的部分代码进行改变,实现一个删除功能,这个功能很好添加,表单中只要form一个参数到DeleteServlet中,所以重点的代码还是在DeleteServlet中实现业务逻辑。   我们运用到的SQL语句是.  ...
  • 首先要实现功能,先找到前端页面的删除按钮 <a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/delUserServlet?id=${user.id}">删除</a></td> 然后编写...
  • 它的基础上进一步实现删除上传到ftp服务器的文件 1、在uploadify.jsp页面添加: 2、在后台 UploadifyController添加: 3、实现效果如图: 点击之后1.png (26KB)文件的叉叉之后(1.png文件不见了):
  • JSP实现留言板

    千次阅读 2019-12-10 20:34:08
    JSP实现留言板 submitMsg.jsp <%@page import="java.util.ArrayList"%> <%@page import="bean.Message"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/...
  • jsp-servlet商品删除实现

    千次阅读 2018-07-10 19:27:19
    1首先在前台jsp页面设置好删除按钮:为按钮绑定一个事件delProduct(pid),参数为商品的pid,处理页面交由pattern为adminDelProduct的servlet处理。list.jspfunction delProduct(pid) { //alert(); var isDel = ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 86,945
精华内容 34,778
关键字:

jsp实现删除功能