0

我有一个将数组传递给jsp页面的java servlet,在那个jsp页面上它显示了一堆结果。我想要做的是当它打印出来时它会打印一个链接,这样我就可以将它用作参数。就我而言,它会打印出一堆实验室课程,我想要发生的是他们单击与该实验室相关的链接,然后我单击该链接并可以在 sql 语句中使用该 lab.id。

这是正在打印的数组的代码

这是小服务程序

private void sendBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpSession session = request.getSession(true);

        //Set data you want to send back to the request (will be forwarded to the page)
    //Can set string, int, list, array etc.
    //Set data you want to send back to the request (will be forwarded to the page)
    //Can set string, int, list, array etc.
    String sql = "SELECT s.name, l.time, l.day, l.room" +
              " FROM lab l, subject s, user_lab ul" +
            " WHERE ul.user_id=" + (Integer)session.getAttribute("id") +" AND ul.lab_id ="+ "l.id"+" AND l.subject_id ="+"s.id";

  try{
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
    System.out.println("got boobs");
    System.out.println(session.getAttribute("id"));

      Statement stmt = con.createStatement();
      ResultSet res = stmt.executeQuery(sql);
      System.out.println(res);
      ArrayList<String> list1 = new ArrayList<String>();
      if (res.next()){
          do{
               list1.add(res.getString(1) + " " + res.getString(2) +" "+ res.getString(3) + " " + res.getString(4));
               System.out.print(res.getString(1) +  res.getString(2));
          }while(res.next());
      System.out.println("Outside");
      String[] arr = list1.toArray(new String[list1.size()]);
      request.setAttribute("res", arr);
      }




  }catch (SQLException e) {
    } 
    catch (Exception e) {
    } 


    //Decides what page to send the request data to
    RequestDispatcher view = request.getRequestDispatcher("Lecturer_labs.jsp");
    //Forward to the page and pass the request and response information
    view.forward(request, response); 

这是jsp页面

<h3>Manage Labs</h3>
<table>
<% String[] list1 = (String[])request.getAttribute("res");
         if(null == list1){%>

<% 
         }else{
        for(int i=0; i<list1.length; i++)
        {   %>
        <tr><%out.println(list1[i] + "<br/>");%></tr>        
    <%  }
        }
        %>
        </table>

那么我怎样才能将结果打印为传递参数的链接

4

1 回答 1

2

要将结果显示为传递参数的链接id,每个链接可能如下所示:

<a href="/myapp/mypage.jsp?id="<%out.println(list1[i]);%>">Link <%out.println(list1[i]);%></a>

但看看这看起来多么笨重。

JSTL 标记可以消除所有这些 scriptlet 代码:

<c:forEach items="${res}" var="id">
   <tr><td><a href="/myapp/mypage.jsp?id=${id}">Link ${id}</a></td></tr>
</c:forEach>
于 2012-10-05T23:57:20.970 回答