[學習筆記] EL 使用EL表達式獲得作用域屬性

 

採用EL表達式 對於屬性的訪問的隱含訪問順序是

 pageContext,  httpServletRequest,  HttpSession,  ServletContext

 

通過以下實例 來說明

servlet

//scopeVar.java

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author he
 */
public class scopeVar extends HttpServlet {
  
    /**
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        request.setAttribute("firstVar", "tom");
   HttpSession session =request.getSession();
   session.setAttribute("secondVar","jack");
   ServletContext application=this.getServletContext();
   application.setAttribute("thirdVar","jerry");
   RequestDispatcher dis=request.getRequestDispatcher("getScopedVars.jsp");
   dis.forward(request, response);
    
       
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}

 

//getScopedVar.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h2>使用EL表達式獲得作用域屬性</h2>
        <table border="1" width="80%">
            <tr>
               
               <td width="50%">使用getAttribute 方法</td>
                   <td width="50%">使用EL表達式</td>
           </tr>
             <tr>
               
               <td width="50%"><%=request.getAttribute("firstVar")%></td>
                   <td width="50%">${firstVar}</td>
           </tr>
             <tr>
               
               <td width="50%"><%=session.getAttribute("secondVar")%></td>
                   <td width="50%">${secondVar}</td>
           </tr>
           <tr>
           <td width="50%"><%=application.getAttribute("thirdVar")%></td>
                   <td width="50%">${thirdVar}</td>
           </tr>
        </table>
    </body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章