How can I do this? Suppose I have jsp page aaa.jsp. From aaa.jsp on form action I have made a call to a servlet xxx.java. In xxx.java I have written code to retrieve data from database through List and data is being retrieve.
Now I want to pass this List to another jsp page bbb.jsp from xxx.java. How can I do this? How can I get the accurate path of the bbb.jsp? To be frankly, managing path is one of the great headache to me. Please help me.
1)callServlet.jsp:
<form method="post" action="../Servlet"> <input type="submit" value="Call Servlet"> </form>
2)Put the servlet in the classes folder and do the servlet mapping in web.xml.Here is Servlet.java:
import java.io.*; import java.sql.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Servlet extends HttpServlet{ protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ ArrayList list=new ArrayList(); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from employee"); while(rs.next()){ list.add(rs.getString("name")); list.add(rs.getString("address")); list.add(rs.getString("contactNo")); list.add(rs.getString("email")); } req.setAttribute("data", list); RequestDispatcher rd = req.getRequestDispatcher("/jsp/data.jsp"); rd.forward(req, res); } catch(Exception e){ } } }
3)data.jsp:
<%@page language="java" import="java.util.*" %> <html> <body> <table border="1" width="303"> <tr> <td width="119"><b>Name</b></td> <td width="168"><b>Address</b></td> <td width="119"><b>Contact no</b></td> <td width="168"><b>Email</b></td> </tr> <% Iterator itr;%> <% List data=(List)request.getAttribute("data"); for(itr=data.iterator(); itr.hasNext(); ){ %> <tr> <td width="119"><%=itr.next()%></td> <td width="168"><%=itr.next()%></td> <td width="168"><%=itr.next()%></td> <td width="168"><%=itr.next()%></td> </tr> <%}%> </table> </body> </html>
Ads