Assignment 4
Assignment 4
You can invoke a JSP page from a servlet through functionality of the
standard javax.servlet.RequestDispatcher interface. Complete the
following steps in your code to use this mechanism:
RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");
Prior to or during this step, you can optionally make data available to the
JSP page through attributes of the HTTP request object
3) Invoke the include() or forward() method of the request dispatcher,
specifying the HTTP request and response objects as arguments. For
example:
rd.include(request, response);
or:
rd.forward(request, response);
Example:
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.*;
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>