Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
59 views

Assignment 4

A servlet can call a JSP page by using the RequestDispatcher interface. The servlet first gets a ServletContext instance and uses it to get a RequestDispatcher for the target JSP. It can then pass request attributes to the JSP and invoke the RequestDispatcher's include() or forward() method, passing the request and response. For example, a servlet retrieves data from a database, stores it in the request, and forwards to a JSP to display the data in a table.

Uploaded by

namrata vyas
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Assignment 4

A servlet can call a JSP page by using the RequestDispatcher interface. The servlet first gets a ServletContext instance and uses it to get a RequestDispatcher for the target JSP. It can then pass request attributes to the JSP and invoke the RequestDispatcher's include() or forward() method, passing the request and response. For example, a servlet retrieves data from a database, stores it in the request, and forwards to a JSP to display the data in a table.

Uploaded by

namrata vyas
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT-4

Q.7 How to call JSP from Servlet? Explain using


example.

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:

1) Get a servlet context instance from the servlet instance:


ServletContext sc = this.getServletContext();

2) Get a request dispatcher from the servlet context instance, specifying


the page-relative or application-relative path of the target JSP page as
input to the getRequestDispatcher() method:

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.*;

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>

You might also like