InfoCenter Home > 4.2.1.3.2.2: Example: Servlet communication by forwardingIn 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>
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||
|