EL表达式的内置对象param参数隐藏对象

----------------------- EL表达式的内置对象param参数隐藏对象-------------------------------

EL内置对象

在EL表达式中,无需创建就可以使用的对象称之为EL隐藏(隐含、内置)对象。在EL中一共有11个隐藏对象,它们都与Map相似。其中10是Map,一个是 PageContext

 

1 参数隐藏对象

这些隐藏对象都是Map类型!

  1. param:param是Map<String,String>类型!param对象可以用来获取参数,与request.getParameter()方法相同。

 

注意,在使用EL获取参数时,如果参数不存在,返回的是空字符串,而不是null。这一点与使用request.getParameter()方法是不同的。

 

 

  1. paramValues:paramValues是Map<String, String[]>类型,当一个参数名,对应多个参数值时可以使用它。

  1. header:header是Map<String,String>类型,用来获取请求头。

  1. headerValues:headerValues是Map<String,String[]>类型。当一个请求头名称,对应多个值时,使用该对象,这里就不在赘述。
  2. initParam:initParam是Map<String,String>类型。它对应web.xml文件中的<context-param>参数。

  1. cookie:cookie是Map<String,Cookie>类型,其中key是Cookie的名字,而值是Cookie对象本身。

 

Login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"

    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>Insert title here</title>

</head>

<body>

       <form action="/EL_demo/login.jsp" method="post">

              账号:<input type="text" name="username"><br>

              密码:<input type="password" name="password"><br>

              <input type="submit" value="登录">

       </form>

       <hr>

       <%

              String username = request.getParameter("username");

              String password = request.getParameter("password");

             

              //添加cookie对象

              response.addCookie(new Cookie("personName","likunpeng"));

        %>

        <h1><%=username %></h1>

        <h1><%=password %></h1>

        <hr>

        <%-- 当值为空时EL表达式输出的是空字符串而不是null --%>

        <%-- 单值接收 --%>

        <h1>${param.username} </h1>

        <h1>${param.password} </h1>

        <hr>

        <%--多值接收 --%>

        <h1>${paramValues.favor[0] },${paramValues.favor[1] }</h1>

        <hr>

        <h1>头信息的接收</h1>

        <h2>${header.Host }</h2>

        <hr>

        <h1>servletContext级别的参数获取</h1>

        <h2>${initParam.person_name }</h2>

        <hr>

        <h1>cookie取值</h1>

        <h2>${cookie.personName.name }:${cookie.personName.value }</h2>

</body>

</html>

 

猜你喜欢

转载自blog.csdn.net/weixin_41547486/article/details/81476297
今日推荐