InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.1: Developing servlets >
4.2.1.3: Servlet content, examples, and samples >
4.2.1.3.2: Inter-servlet communication >
4.2.1.3.2.2: Example: Servlet communication by forwarding

4.2.1.3.2.2: Example: Servlet communication by forwarding

In this example, the forward method is used to send a message to a JSP file (a servlet) that prints the message. The forwarding servlet code is:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class UpdateJSPTest extends HttpServlet
{
public void doGet (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String message = "This is a test";
req.setAttribute("message", message);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Update.jsp");
rd.forward(req, res);
}
}

The JSP file is:

<html>
<head>
</head>
<body>
<h1><servlet code=UpdateJSPTest></servlet></h1>

<%
String message = (String) request.getAttribute("message");
out.print("message: <b>" + message + "</b>");
%>

<p>
<ul>
<%
for (int i = 0; i < 5; i++)
{
out.println ("<li>" + i);
}
%>
</ul>

</body>
</html>

Go to previous article: Forwarding and including data (request and response) Go to next article: Using page lists to avoid hard coding URLs

 

 
Go to previous article: Forwarding and including data (request and response) Go to next article: Using page lists to avoid hard coding URLs