我通过创建pogoclass.jsp
和servlet从mysql中检索了表信息,并使用jsp中的jstl打印出来。我的问题是当我点击分配给每一行的提交按钮并将其发送到另一个jsp以更新该行时,我无法检索每一行信息。我正在使用eclipseIDE。这是我的代码:
的index.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();
}
}
}
tableprint3.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>
答案 0 :(得分:0)
您应该为每个文本框添加名称字段,并且需要更新记录的隐藏字段以进行更新。
<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>