本文介绍了选中复选框后获取servlet中jsp行的更新值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在更新两个或更多行列后尝试获取值并尝试获取servlet时,即仅在servlet中获取一个值,而我必须发送两行或更多行,或者我们可以说要更新的项目列表另一个servlet,我正在尝试写她的plz,这可以帮助我在servlet和jsp上获得新的体验

when i try to get value after update the two or more row columns and try to get in servlet ,i m getting only one value in servlet and i have to send two or more row or we can say a updated list of project to another servlet what i m trying to do i have write her plz help me out i m new at servlet and 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>&nbsp;</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>&nbsp;</p>
        </form>
        </body>
        </html>

这是我的servlet

and this is my 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();
        }
    }
}

推荐答案

1)您应该为复选框创建其他名称name ="projectCode",例如,将employeeProjectBO.projectCode添加到此输入名称

1) You should create different names to checkboxesname="projectCode" add for example employeeProjectBO.projectCode to this input name

2)在servlet中

2) in servlet

for(int i=0; i<projectCode.length; i++) {
request.getParameterValues("checkedProject"+projectCode.getProjectCode);
…
}

这篇关于选中复选框后获取servlet中jsp行的更新值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:03