Java Web EL表达式

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" isELIgnored="false" import="com.xzit.domain.Student"%>
<!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>首页</title>
    </head>
    <%
        String name = "Marke";
        pageContext.setAttribute("user_name",name);
        Student stu = new Student("岳不群",23);
        request.setAttribute("stuObj", stu);
    %>
    <body bgcolor="#ffeeee">
        ${'张无忌'}<br>
        ${56*2}<br>
        从作用域中取出对象:${pageScope.user_name}<br>
        <%--
        <jsp:forward page="el.jsp"></jsp:forward>
        --%>
        <a href="el.jsp?stuname=Zhangsan&age=21">请求EL.jsp</a><br>
        <a href="ELServlet">请求ELServlet</a><br>
        <form action="el.jsp">
            <input type="checkbox" name="likes" value="lanqiu">篮球
            <input type="checkbox" name="likes" value="zuqiu">足球
            <input type="checkbox" name="likes" value="paiqiu">排球<br>
            <input type="submit" value="提交">
        </form>
    </body>
</html>
<%@ 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>EL 表达式</title>
    </head>
    <body bgcolor="#ffeeaa">
        从作用域中访问复合类型对象:${requestScope.stuObj.stuName}<br>
        获取请求查询参数:${param.stuname}&nbsp;${param.age}<br>
        获取Cookie:${cookie['username'].name}&nbsp;${cookie['username'].value}<br>
        获取一个请求参数对应多个值的方式:<br>
        第一个:${paramValues.likes[0]}<br>
        第二个:${paramValues.likes[1]}<br>
        第三个:${paramValues.likes[2]}<br>
    </body>
</html>
package com.xzit.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ELServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        
        Cookie cookies[] = request.getCookies();
        
        if(cookies!=null && cookies.length!=0){
            int count = 0;
            for(Cookie c:cookies){
                
                if(c.getName().equals("username")){
                    count++;
                    break;
                }
            }
            if(count==0){
                Cookie cookie = new Cookie("username","Zidane");
                cookie.setMaxAge(3600*24);
                response.addCookie(cookie);
            }
        }
    }

}
<?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>ELapp2</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
      <display-name>ELServlet</display-name>
      <servlet-name>ELServlet</servlet-name>
      <servlet-class>com.xzit.servlet.ELServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>ELServlet</servlet-name>
      <url-pattern>/ELServlet</url-pattern>
  </servlet-mapping>
  
</web-app>
package com.xzit.domain;

public class Student {
    private String stuName;
    private int age;
    public Student() {

    }
    

    public String getStuName() {
        return stuName;
    }


    public void setStuName(String stuName) {
        this.stuName = stuName;
    }


    public int getAge() {
        return age;
    }


    public void setAge(int age) {
        this.age = age;
    }


    public Student(String stuName, int age) {
        super();
        this.stuName = stuName;
        this.age = age;
    }
    
}

 

posted @ 2022-02-20 21:03  伊万  阅读(39)  评论(0编辑  收藏  举报