当我在更新两个或更多行列后尝试获取值并尝试进入servlet时,我只在servlet中获取一个值,并且我必须发送两行或更多行,或者我们可以说更新的项目列表到我想写的另一个servlet我写了她的PLZ帮助我在servlet和jsp
的新功能 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeProjectBO"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@page import="com.nousinfo.tutorial.employee.service.model.bo.EmployeeBO"%>
<!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>Update Project</title>
<link rel="stylesheet" href="css/style.css" type="text/css"></link>
</head>
<body >
<form action="ProjectUpdateServlet">
<table border="1" width="500" id="tbl">
<tr>
<td width="234"><b>click</b></td>
<td width="119"><b>Employee Number</b></td>
<td width="234"><b>Project Code</b></td>
<td width="234"><b>Start Date</b></td>
<td width="234"><b>End Date</b></td>
<td width="234"><b>Role</b></td>
</tr>
<c:forEach var="employeeProjectBO" items="${sessionScope.projectList}">
<tr>
<td><input type="checkbox" value='${employeeProjectBO.projectCode}' name="projectCode"></td>
<td><input type="text" value='${employeeProjectBO.employeeNumber}' name="employeeNumber" readonly="readonly"/></td>
<td><input type="text" value='${employeeProjectBO.projectCode}' name="projectCode" readonly="readonly"/></td>
<td><input type="text" value='${employeeProjectBO.startDate}'name="startDate"/></td>
<td><input type="text" value='${employeeProjectBO.endDate}' name="endDate"/></td>
<td><input type="text" value='${employeeProjectBO.role}' name="role"/></td>
<td><input type="hidden" name="updateStatusProject" value="M" /></td>
</tr>
</c:forEach>
</table>
<p> </p>
<br />
<table width="200" border="0" align="center">
<tr>
<td><center>
<input type="submit" name="submit" value="update" onclick="self.close()">
</center></td>
<td><center>
<input type="button" name="cancle" value="cancle"
onclick="self.close()"/>
</center></td>
</tr>
</table>
<hr size="1" width="786">
<p> </p>
</form>
</body>
</html>
这是我的servlet
package com.nousinfo.tutorial;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nousinfo.tutorial.employee.service.model.bo.EmployeeProjectBO;
/**
* Servlet implementation class ProjectUpdateServlet
*/
public class ProjectUpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProjectUpdateServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String[] projectCode= request.getParameterValues("checkedProject");
for(int i=0; i<projectCode.length; i++) {
System.out.println(projectCode[i]);}
System.out.println(request.getSession().getAttribute("projectList"));
if (request.getParameter("submit") != null) {
request.getSession().setAttribute("employeeProjectBO",
setEmployeeProject(request));
}
}
public EmployeeProjectBO setEmployeeProject(HttpServletRequest request) {
EmployeeProjectBO employeeProjectBO = new EmployeeProjectBO();
String[] projectCode= request.getParameterValues("checkedProject");
for(int i=0; i<projectCode.length; i++) {
employeeProjectBO.setUpdateStatus(request.getParameter("updateStatusProject").charAt(0));
employeeProjectBO.setEmployeeNumber(Long.parseLong(request
.getParameter("employeeNumber")));
employeeProjectBO.setProjectCode(request.getParameter("projectCode"));
employeeProjectBO.setStartDate(new java.sql.Date(getDate(
request.getParameter("startDate")).getTime()));
employeeProjectBO.setEndDate(new java.sql.Date(getDate(
request.getParameter("endDate")).getTime()));
employeeProjectBO.setRole(request.getParameter("role"));
System.out.println(request.getParameter("role"));}
return employeeProjectBO;
}
private static Date getDate(String source) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
return format.parse(source);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return new Date();
}
}
}
答案 0 :(得分:0)
1) 您应该为复选框创建不同的名称 命名=&#34; projectCode&#34;例如,将employeeProjectBO.projectCode添加到此输入名称
2)在servlet中
for(int i=0; i<projectCode.length; i++) {
request.getParameterValues("checkedProject"+projectCode.getProjectCode);
…
}