当我们使用 jsp 和 servlet 单击分配给每一行的提交按钮时,如何检索记录的信息



我通过创建pogoclass.jsp和servlet从mysql中检索表信息,并在jsp中使用jstl打印它。我的问题是,当我单击分配给每行的提交按钮并将其发送到另一个 jsp 以更新行时,我无法检索每行信息。我正在使用eclipseIDE。这是我的代码:

索引.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="ViewTableDelete" method="post">
        <table>
            <tr>
                <td><input type="submit" value="tablevalues" /></td>
                </tr>
          </table>
    </form>
</body>
</html>

pojoclassdemo3.java:

package com.example.pojo;
public class PojoClassDemo3 {
    private String empname;
    private String password;
    public PojoClassDemo3()
    {
    }
    public String getEmpname() {
        return empname;
    }
    public void setEmpname(String empname) {
        this.empname = empname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "PojoClassDemo3 [empname=" + empname + ", password=" + password + "]";
    }
}

viewTableDelete.java:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.pojo.PojoClassDemo3;
/**
 * Servlet implementation class ViewTableDelete
 */
@WebServlet("/ViewTableDelete")
public class ViewTableDelete extends HttpServlet {
    Connection con;
    Statement st;
    private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewTableDelete() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("********** In Init method **************");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("registered");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp1", "root", "root");
            System.out.println("connection established");
            st = con.createStatement();
            System.out.println("statement created");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }
    /**
     * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
     */
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        try{
            PrintWriter out=response.getWriter();
            response.setContentType("text/html");
            ResultSet rs=st.executeQuery("select * from emp1.employee");
            System.out.println("select * from emp1.employee");
            System.out.println("creating list");
            List<PojoClassDemo3> list = new ArrayList<PojoClassDemo3>();
            while(rs.next())
            {
                PojoClassDemo3  pojoclassdemo=new  PojoClassDemo3();
                pojoclassdemo.setEmpname(rs.getString("empname"));
                pojoclassdemo.setPassword(rs.getString("password"));
                list.add(pojoclassdemo);
            }
            request.setAttribute("list",list);
            RequestDispatcher rd= request.getRequestDispatcher("tableprint3.jsp");
            rd.forward(request, response);

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }
}

表印3.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@page import="java.util.List"%>
     <%@page import="java.util.Map"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

</head>
<body >
<form method="post" action="update.jsp">
        <table border="1" cellspacing="0" cellpadding="5">
            <tr>
                <th>empname</th>
                <th>password</th>
                <th>edit</th>
            </tr>
            <c:forEach items="${requestScope.list}" var="record">
                <tr>
                    <td><input type="text" value="${record.empname}" /></td>
                    <td><input type="text" value="${record.password}" /></td>
                    <td><input type="submit" name="${record.empname}" onclick="Change(name)" value="update" /></td>
         </c:forEach>

        </table>
    </form>
</body>
</html>

您应该为每个文本框添加一个名称字段,并且需要更新记录的隐藏字段。

            <tr>
                <td><input name="empname" type="text" value="${record.empname}" /></td>
                <td><input name="password" type="text" value="${record.password}" /></td>
                <td><input type="submit" name="${record.empname}" onclick="Change(name)" value="update" /></td>
     </c:forEach>

最新更新