[学习笔记] 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>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值