Java Servlets

JSP Expressions Example

package coreservlets;
/** Servlet that creates some scoped variables (objects stored
 * as attributes in one of the standard locations). Forwards
 * to a JSP page that uses the expression language to
 * display the values.

import javax.servlet.*;
import javax.servlet.http.*;
public class ScopedVars extends HttpServlet {

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
    throws ServletException, IOException {
    request.setAttribute("attribute1", "First Value");
    HttpSession session = request.getSession();
    session.setAttribute("attribute2", "Second Value");

    ServletContext application = getServletContext();
    application.setAttribute("attribute3", new java.util.Date());
    request.setAttribute("repeated", "Request");
    session.setAttribute("repeated", "Session");
    application.setAttribute("repeated", "ServletContext");
    RequestDispatcher dispatcher =request.getRequestDispatcher("/el/scoped-vars.jsp");
    dispatcher.forward(request, response);
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  <HEAD><TITLE>Accessing Scoped Variables</TITLE>
    <H1>Accessing Scoped Variables</H1>
      <LI><B>attribute1:</B> ${attribute1} <!--First value-->
      <LI><B>attribute2:</B> ${attribute2} <!--Second value-->
      <LI><B>attribute3:</B> ${attribute3} <!-- the date-->
      <LI><B>Source of "repeated" attribute:</B> ${repeated} <!-- Request -->


José M. Vidal .

86 of 89