Advertisement
Guest User

Untitled

a guest
Jan 14th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. Servlet Jsp Array Printing Links
  2. private void sendBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  3. HttpSession session = request.getSession(true);
  4.  
  5. //Set data you want to send back to the request (will be forwarded to the page)
  6. //Can set string, int, list, array etc.
  7. //Set data you want to send back to the request (will be forwarded to the page)
  8. //Can set string, int, list, array etc.
  9. String sql = "SELECT s.name, l.time, l.day, l.room" +
  10. " FROM lab l, subject s, user_lab ul" +
  11. " WHERE ul.user_id=" + (Integer)session.getAttribute("id") +" AND ul.lab_id ="+ "l.id"+" AND l.subject_id ="+"s.id";
  12.  
  13. try{
  14. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/wae","root","");
  15. System.out.println("got boobs");
  16. System.out.println(session.getAttribute("id"));
  17.  
  18. Statement stmt = con.createStatement();
  19. ResultSet res = stmt.executeQuery(sql);
  20. System.out.println(res);
  21. ArrayList<String> list1 = new ArrayList<String>();
  22. if (res.next()){
  23. do{
  24. list1.add(res.getString(1) + " " + res.getString(2) +" "+ res.getString(3) + " " + res.getString(4));
  25. System.out.print(res.getString(1) + res.getString(2));
  26. }while(res.next());
  27. System.out.println("Outside");
  28. String[] arr = list1.toArray(new String[list1.size()]);
  29. request.setAttribute("res", arr);
  30. }
  31.  
  32.  
  33.  
  34.  
  35. }catch (SQLException e) {
  36. }
  37. catch (Exception e) {
  38. }
  39.  
  40.  
  41. //Decides what page to send the request data to
  42. RequestDispatcher view = request.getRequestDispatcher("Lecturer_labs.jsp");
  43. //Forward to the page and pass the request and response information
  44. view.forward(request, response);
  45.  
  46. <h3>Manage Labs</h3>
  47. <table>
  48. <% String[] list1 = (String[])request.getAttribute("res");
  49. if(null == list1){%>
  50.  
  51. <%
  52. }else{
  53. for(int i=0; i<list1.length; i++)
  54. { %>
  55. <tr><%out.println(list1[i] + "<br/>");%></tr>
  56. <% }
  57. }
  58. %>
  59. </table>
  60.  
  61. <a href="/myapp/mypage.jsp?id="<%out.println(list1[i]);%>">Link <%out.println(list1[i]);%></a>
  62.  
  63. <c:forEach items="${res}" var="id">
  64. <tr><td><a href="/myapp/mypage.jsp?id=${id}">Link ${id}</a></td></tr>
  65. </c:forEach>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement