我必须从不同的不同表中获取值,并且值不止一个,并且我必须在box的基础上显示不同的不同下拉框中的所有值,但我不确定我的方法是否正确,但是m获得价值,但最终当我尝试选择价值时会发生什么,但是它像[saurabh,prakash,karan]并在带有数组符号的一行中,但是我想要saurabh,在下一行中使用prakash,所以这是我的问题,这我正在其中调用empDetail方法的servlet类

 @WebServlet(name = "EmployeeMapping", urlPatterns = {"/EmployeeMapping"})
 public class EmployeeMapping extends HttpServlet {
 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
  HttpSession session = request.getSession(true);
  Employee emp1 = (Employee)session.getAttribute("emp1");
  EmployeeBean eb = new EmployeeBean();
  List<Employee> leave1 = eb.empDetail(emp1);
  if(emp1.isValid())
  {
    request.setAttribute("leave1",leave1);
    response.sendRedirect("AssignProject.jsp");
  }}
   @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  processRequest(request, response);
   }
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  processRequest(request, response);
     }
  @Override
  public String getServletInfo() {
  return "Short description";}}


这是我的方法

  public List<Employee> empDetail(Employee emp1) {
  List<Employee> leave1 = new ArrayList<Employee>();
  List<String> myList1 = new ArrayList<String>();
  List<String> myList2 = new ArrayList<String>();
  List<String> myList3 = new ArrayList<String>();
  List<String> myList4 = new ArrayList<String>();
  try{
  con = ConnectionManager.getConnection();
  stmt1 = con.createStatement();
  String Query11 = "SELECT projectname FROM addproject";
  System.out.println("Query1 is" +Query11);
  rs1 = stmt1.executeQuery(Query11);
  while(rs1.next())
  {
  String z =(rs1.getString("projectname"));
  System.out.println("...11." +z);
  myList1.add(z);
  }
   emp1.setPrjname(myList1);
   stmt2 = con.createStatement();
  String Query12 = "select empName from empinfo where access_type ='teamleader'";
  System.out.println("Query1 is" +Query12);
  rs2 = stmt2.executeQuery(Query12);
  while(rs2.next())
  {
String p = rs2.getString("empName");
System.out.println("...11." +p);
myList2.add(p);
  }
  emp1.setTlname(myList2);
  stmt3 = con.createStatement();
  String Query13 = "select empName from empinfo where access_type ='manager'";
  System.out.println("Query1 is" +Query13);
  rs3 = stmt3.executeQuery(Query13);
  while(rs3.next())
  {
  String q = rs3.getString("empName");
  System.out.println("...11." +q);
  myList3.add(q);
  }
  emp1.setManname(myList3);
  stmt4 = con.createStatement();
   String Query14 = "select empName from empinfo where access_type ='employee'";
   System.out.println("Query1 is" +Query14);
   rs4 = stmt4.executeQuery(Query14);
   while(rs4.next())
  {
  String r = rs4.getString("empName");
  System.out.println("...11." +r);
  myList4.add(r);
  }
   emp1.setEmpname(myList4);
   leave1.add(emp1);
   emp1.setValid(true);
  }
  catch (SQLException  ex) {
  } finally {
      try {
          if (pstmt != null) {
              pstmt.close();
          }
          if (con != null) {
              con.close();
          }
      } catch (SQLException ex) {
          Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    return leave1;
    };


这是我的jsp页面,我在其中获得类似[saurabh,prakash,karan]的值,但是如果我要给$ {emp1.prjname}插入$ {emp1.prjname [0]},那么我将得到saurabh就像明智的选择在这里,我面临着与此难以相提并论的困难,所以请任何人帮助我..

 <%if(session.getAttribute("emp1")==null)
 {
 String message ="please login first";
 request.setAttribute("message", message);
  request.getRequestDispatcher("index.jsp").forward(request, response);
 }
 %>
   <title>RESOURCE MANAGEMENT SYSTEM</title>
 <head
 </head><body>
 <%
 Employee emp1 = (Employee)session.getAttribute("emp1");
 session.setAttribute("emp1",emp1);
 %>
 <tr>
        <td >select project name </td>
         <td>   <select name="pname">
      <option value="${emp1.prjnamename}">${emp1.prjnamename}</option></select></td>
      </tr>
         <tr>
      <td >manager </td>
         <td>   <select name="manager">
      <option value="${emp1.manname}">${emp1.manname}</option></select></td>
            </tr>
         <tr>
               <td >team leader</td>
         <td>   <select name="teamleader">
      <option value="emp1.Tlname()">emp1.Tlname()</option></select></td>
            </tr>
    <tr>
                <td> <section class="container">
    <div>
    <select id="leftValues" size="5" multiple>
   <option value="${emp1.empname}">${emp1.empname}</option>
  </select>
  </div>
  </td>
  <td>
  <div>
    <input type="button" id="btnLeft" value="&lt;&lt;" />
    <input type="button" id="btnRight" value="&gt;&gt;" />
  </div>
 </td>
  <td>
 <div>
    <select name="nameofemps" id="rightValues" size="4" multiple="multiple">
        <option>
        </option>
    </select>
    <div>
        <input type="text" id="txtRight" />
    </div>
  </div>
 </section>
 </td>
   </tr>
        </table><BR>
        </fieldset>
  </body>
   </html>

最佳答案

您需要使用forEach属性迭代request循环,

  <select id="leftValues" size="5" multiple>
  <c:forEach var='temp' items='${leave1}' >
    <option value="${temp.empname}">${temp.empname}</option>
  </c:forEach>
  </select>


另外,您正在此处创建新请求,

  if(emp1.isValid())
  {
    request.setAttribute("leave1",leave1);
    response.sendRedirect("AssignProject.jsp");
  }}


相同的请求应该是对jsp的forwarded。使用RequestDispatcher代替

我假设您正在尝试从请求中打印leave1,如果您的看法有所不同,请在我的答案下方发表评论

07-27 13:38