Advertisement
Guest User

Untitled

a guest
Jul 30th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. display data using bean and servlet
  2. package form;
  3. import java.sql.*;
  4. import java.util.*;
  5. public class EmpBean {
  6.  
  7. public List dataList(){
  8. ArrayList list=new ArrayList();
  9. try{
  10. Class.forName("com.mysql.jdbc.Driver");
  11. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
  12. Statement st=con.createStatement();
  13. ResultSet rs=st.executeQuery("select * from employee");
  14. while(rs.next()){
  15. list.add(rs.getString("name"));
  16. list.add(rs.getString("address"));
  17. list.add(rs.getString("contactNo"));
  18. list.add(rs.getString("email"));
  19.  
  20. }
  21. }
  22. catch(Exception e){}
  23. return list;
  24.  
  25. }
  26. }
  27.  
  28. import java.io.*;
  29. import java.util.*;
  30. import javax.servlet.*;
  31. import javax.servlet.http.*;
  32.  
  33. public class BeanInServlet extends HttpServlet{
  34. protected void doGet(HttpServletRequest req, HttpServletResponse res)
  35. throws ServletException, IOException{
  36. form.EmpBean p = new form.EmpBean();
  37. List list=p.dataList();
  38.  
  39. req.setAttribute("data", list);
  40. RequestDispatcher rd = req.getRequestDispatcher("/jsp/beandata.jsp");
  41. rd.forward(req, res);
  42. }
  43. }
  44.  
  45. <%@page language="java" import="java.util.*" %>
  46. <html>
  47. <body>
  48. <table border="1" width="303">
  49. <tr>
  50. <td width="119"><b>Name</b></td>
  51. <td width="168"><b>Address</b></td>
  52. <td width="119"><b>Contact no</b></td>
  53. <td width="168"><b>Email</b></td>
  54. </tr>
  55. <%Iterator itr;%>
  56. <% List data= (List)request.getAttribute("data");
  57. for (itr=data.iterator(); itr.hasNext(); ){
  58. %>
  59. <tr>
  60. <td width="119"><%=itr.next()%></td>
  61. <td width="168"><%=itr.next()%></td>
  62. <td width="168"><%=itr.next()%></td>
  63. <td width="168"><%=itr.next()%></td>
  64. </tr>
  65. <%}%>
  66. </table>
  67. </body>
  68. </html>
  69.  
  70. <table>
  71. <c:foreach var="item" varStatus="status" items="${requestScope.data}">
  72. <c:choose>
  73. <c:when test="${status.index % 4 == 0}">
  74. <tr>
  75. <td width="119"><c:out value="${item}" /></td>
  76. </c:when>
  77. <c:otherwise>
  78. <td width="168"><c:out value="${item}" /></td>
  79. </c:otherwise>
  80. </c:choose>
  81. <c:if test="${status.index % 4 == 3}">
  82. </tr>
  83. </c:if>
  84. </c:foreach>
  85. </table>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement