美文网首页
EL中访问隐式对象

EL中访问隐式对象

作者: 开心的小哈 | 来源:发表于2020-04-01 17:30 被阅读0次

    pageContext

    通过pageContext获取servletContext,request,response,session等对象
    格式${pageContext.request.locale}
    获取request中用户请求的值${param.name}
    获取请求头信息${headers['Content-Type']}(如果定义的字段含有-等关键词也可以采用这样)
    获取cookie值${cookie.cookeName.value}
    获取context-param${initParam.name}

    获取request中用户请求的值:

    userName:<%=request.getParameter("name") %>
    password:<%=request.getParameter("pwd") %>
    <hr>
    userName:${param.name}
    password:${param["user-pwd"]}
    

    获取头

    user-agent:<%=request.getHeader("user-agent") %><br>
    user-agent:${header["user-agent"] }
    

    获取web的context-param配置信息

    web.xml代码

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>el</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <context-param>
      <param-name>test</param-name>
      <param-value>123123</param-value>
      </context-param>
    </web-app>
    

    index页面获取方式

    Context-Param:${initParam.test}<br>
    Context-Para“”m:<%= application.getInitParameter("test")%>
    

    获取Cookie

    cookie:${cookie.test.value}<br>
    <% Cookie cookies[]=request.getCookies();
                String s=cookies[1].getValue();
                %>
    cookie:<%= s%>
    

    获取隐式作用域对象中保存的属性

    ${user.name}按照pageScope,requestScope,sessionScope,applicationScope的顺序查找
    ${pageScope.user.name}只在pageContext中查找(指定范围)
    流程介绍

    1.
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>ELbiaodahshi</title>
    </head>
    <body>
    <form action="control/doForm.jsp" method="post">
    <p>login</p>
    <p>name:<input type="text" name="name"></p>
    <p>password:<input type="password" name="pwd"></p>
    <p><button type="reset">cancel</button>
    <button type="submit">submit</button></p>
    </form>
    <hr>
    
    </body>
    </html>
    2.
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <jsp:useBean id="user" class="el.User" scope="request"></jsp:useBean>
    <jsp:setProperty property="*" name="user"/>
    <jsp:forward page="../show.jsp"/>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    userName1:<%=request.getParameter("name") %>
    password1:<%=request.getParameter("pwd") %>
    <hr>
    userName2:${param.name}
    password2:${param["pwd"]}
    <br>
    
    </body>
    </html>
    3.
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"
        import="el.User"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    userName:${user.name}
    <%User user=(User)pageContext.findAttribute("user"); %>
    password:${requestScope.user.pwd}
    <%=user.getPwd() %>
    </body>
    </html>
    3
    package el;
    
    public class User {
     private String name;
     private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    }
    
    

    相关文章

      网友评论

          本文标题:EL中访问隐式对象

          本文链接:https://www.haomeiwen.com/subject/wjugehtx.html