简体   繁体   English

Servlet Jsp阵列打印链接

[英]Servlet Jsp Array Printing Links

I have a java servlet that passes an array to a jsp page, on that jsp page it displays a bunch of results. 我有一个java servlet将一个数组传递给一个jsp页面,在那个jsp页面上它显示了一堆结果。 What I am trying to do is when it prints out it prints a link so I can use it as a parameter. 我想要做的是当它打印出来时打印一个链接,所以我可以用它作为参数。 In my case it prints out a bunch of lab classes, what i want to happen is they click the link related to that lab then i click the link and can use that lab.id in a sql statement. 在我的例子中它打印出一堆实验室类,我想要发生的是他们点击与该实验室相关的链接然后我点击链接并可以在sql语句中使用该lab.id。

here is the code for the array being printed out 这是打印出来的数组的代码

here is the servlet 这是servlet

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); 

and here is the jsp page 这是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>

so how can I get it to print the results as a link that passes a parameter 那么如何才能将结果打印为传递参数的链接

To display the results as a link that pass the parameter id , each link could look like: 要将结果显示为传递参数id链接,每个链接可能如下所示:

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

but look how clunky this looks. 但看起来这看起来多么笨重。

JSTL tags can eliminate all this scriptlet code: JSTL标记可以消除所有这些scriptlet代码:

<c:forEach items="${res}" var="id">
   <tr><td><a href="/myapp/mypage.jsp?id=${id}">Link ${id}</a></td></tr>
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM