Oracle JavaServer Pages Developer's Guide and Reference Release 8.1.7 Part Number A83726-01 |
|
Although coding JSP pages is convenient in many ways, some situations call for servlets. One example is when you are outputting binary data, as discussed in "Reasons to Avoid Binary Data in JSP Pages".
Therefore, it is sometimes necessary to go back and forth between servlets and JSP pages in an application. This section discusses how to accomplish this, covering the following topics:
As when invoking one JSP page from another, you can invoke a servlet from a JSP page through the jsp:include
and jsp:forward
action tags. (See "JSP Actions and the <jsp: > Tag Set".) Following is an example:
<jsp:include page="/servlet/MyServlet" flush="true" />
When this statement is encountered during page execution, the page buffer is output to the browser and the servlet is executed. When the servlet has finished executing, control is transferred back to the JSP page and the page continues executing. This is the same functionality as for jsp:include
actions from one JSP page to another.
And as with jsp:forward
actions from one JSP page to another, the following statement would clear the page buffer, terminate the execution of the JSP page, and execute the servlet:
<jsp:forward page="/servlet/MyServlet" />
Important: You cannot include or forward to a servlet in Apache/JServ or other servlet 2.0 environments; you would have to write a JSP wrapper page instead. For information, see "Dynamic Includes and Forwards in Apache/JServ". |
When dynamically including or forwarding to a servlet from a JSP page, you can use a jsp:param
tag to pass data to the servlet (the same as when including or forwarding to another JSP page).
A jsp:param
tag is used within a jsp:include
or jsp:forward
tag. Consider the following example:
<jsp:include page="/servlet/MyServlet" flush="true" > <jsp:param name="username" value="Smith" /> <jsp:param name="userempno" value="9876" /> </jsp:include>
For more information about the jsp:param
tag, see "JSP Actions and the <jsp: > Tag Set".
Alternatively, you can pass data between a JSP page and a servlet through an appropriately scoped JavaBean or through attributes of the HTTP request object. Using attributes of the request object is discussed later, in "Passing Data Between a JSP Page and a Servlet".
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:
ServletContext sc = this.getServletContext();
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. See the next section, "Passing Data Between a JSP Page and a Servlet", for information.
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);
The functionality of these methods is similar to that of jsp:include
and jsp:forward
actions. The include()
method only temporarily transfers control; execution returns to the invoking servlet afterward.
Note that the forward()
method clears the output buffer.
The preceding section, "Invoking a JSP Page from a Servlet", notes that when you invoke a JSP page from a servlet through the request dispatcher, you can optionally pass data through the HTTP request object.
You can accomplish this using either of the following approaches:
name
=
value
pairs.
Here is an example:
RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp?username=Smith");
In the target JSP page (or servlet), you can use the getParameter()
method of the implicit request
object to obtain the value of a parameter set in this way.
setAttribute()
method of the HTTP request object.
Here is an example:
request.setAttribute("username", "Smith"); RequestDispatcher rd = sc.getRequestDispatcher("/jsp/mypage.jsp");
In the target JSP page (or servlet), you can use the getAttribute()
method of the implicit request
object to obtain the value of a parameter set in this way.
This section provides a JSP page and a servlet that use functionality described in the preceding sections. The JSP page Jsp2Servlet.jsp
includes the servlet MyServlet
, which includes another JSP page, welcome.jsp
.
<HTML> <HEAD> <TITLE> JSP Calling Servlet Demo </TITLE> </HEAD> <BODY> <!-- Forward processing to a servlet --> <% request.setAttribute("empid", "1234"); %> <jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/> </BODY> </HTML>
import javax.servlet.*; import javax.servlet.http.*; import java.io.PrintWriter; import java.io.IOException; public class MyServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { PrintWriter out= response.getWriter(); out.println("<B><BR>User:" + request.getParameter("user")); out.println (", Employee number:" + request.getAttribute("empid") + "</B>"); this.getServletContext().getRequestDispatcher("/jsp/welcome.jsp"). include(request, response); } }
<%----------------------------------------------------------- Copyright © 1999, Oracle Corporation. All rights reserved. ------------------------------------------------------------%> <HTML> <HEAD> <TITLE> The Welcome JSP </TITLE> </HEAD> <BODY> <H3> Welcome! </H3> <P><B> Today is <%= new java.util.Date() %>. Have a nice day! </B></P> </BODY> </HTML>
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|