zoukankan      html  css  js  c++  java
  • jsp 实现修改和删除功能

    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 

  • 相关阅读:
    大端模式与小端模式
    通过tcp socket实现Linux与windows之间的文件传输
    关于递归的几个小例子
    关于线性表的一些简单应用
    数据结构(c语言实现)--线性表
    简单实现getpwnam()
    chapter 7 内存分配函数
    chapter6 非局部跳转函数 setjmp()与longjmp()
    Xcode 出现Permission denied 解决方法
    关于ARfoundation ILRuntime热更新项目的坑
  • 原文地址:https://www.cnblogs.com/zhenqk/p/11055735.html
Copyright © 2011-2022 走看看