栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

EL表达式javaweb

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

EL表达式javaweb

一:JavaBean
JavaBean是Java开发语言中一个可以重复使用的软件。它本质上就是一个Java类。
为了规范 JavaBean 的开发, Sun 公司发布了 JavaBean 的规范,它要求一个标准的 JavaBean 组件需要道循一定的编码规范,具体如下
(1)们它必须貝有一个公共的、无参的构造方法,这个方法可以是编译器自动产生的默认构造方法。
(2)它提供公共的 setter 方法和 getter 方法,让外部程序设置和获取 JavaBean 的属性。

在包下创建 Person 类, Person 类中定义了 name 和 age 两个属性,并提供了对应的 getter 方法 setter 方法供外界访问这两个属性,具体代码如下:

package cn.itcast.capter07.beanutils;

public class Person {
private String name;
private int age;
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

再创一个类,该类中使用BeabUtils类的常用方法,如下:

package cn.itcast.capter07.beanutils;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
public class BeanutilsDemo {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
Person p=new Person();
BeanUtils.setProperty(p, "name", "莫");
BeanUtils.setProperty(p, "age", 19);
String name=BeanUtils.getProperty(p, "name");
String age=BeanUtils.getProperty(p, "age");
System.out.print("我的名字是:"+name+",今年"+age+"岁!n");
Map map=new HashMap();
map.put("name","huihui");
map.put("age",20);
BeanUtils.populate(p, map);
System.out.print("姓名:"+p.getName()+",年龄"+p.getAge());
	}

}

实现了使用 BeanUtils 为 JavaBean 的属性先赋值,然后再取值的功能,第9~10行代码使用 seiProperty ()方法分别为 name 和 age 属性赋值为 Jack 和10,第12~13行代码使用 getProperty ()方法分别获取 name 和 age 属性的值,第14行代码打印出这些值。第16-18行代码创建了一个 map 集合,并将属性 name 和 age 及其对应的值以键值对的形式存放到 map 中,第20行代码使用 BeanUtils 类中的 populate ()方法一次性为多个属性赋值,第22行代码打印赋值后对象的信息。

二:EL
由于 EL 可以简化 JSP 页面的书写,因此,在 JSP 的学习中,掌握 EL 是相当重要的。要使用 EL 表达式,首先要学习它的语法。 格式如下
${表达式}

首先,在项目的 src 目录下创建包 在包中创建一个用于存储用户名和密码的类 MySorvlet ,代码 :

package cn.itcast.chapter07.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/myservlet")
public class myservlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public myservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setAttribute("username", "itcast");
		request.setAttribute("password", "123");
		RequestDispatcher dispatcher=request.getRequestDispatcher("/myjsp.jsp");
		dispatcher.forward(request, response);
		}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

在webcontent编写一个myjsp的jsp文件,用来输出MySorvlet的信息,如下:

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




Insert title here


${person.name}
用户名:<%=request.getAttribute("username") %>
密码:<%=request.getAttribute("password") %>

使用el表达式:
用户名:${username}
密码:${password}

结果:

三:EL隐式对象
1.pageContext对象

为了获取 JSP 页面的隐式对象,可以使用 EL 表达式中的 pageContext 隐式对象。 pageContexi 隐式对象的示例代码如下。
$ {pageContext . response . characterEncoding }
在上述示例中, pageContext 对象用于获取 response 对象中的 characterEncoding 属性
在项目的 WebContent 目录下创建一个名为 pageContext . jsp 的文件,

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




pagecontext


获取项目路径:${pageContext.request.contextPath}
content-type响应头:${pageContext.response.contentType }
服务器信息:${pageContext.servletContext.serverInfo}
servlet注册名:${pageContext.servletConfig.servletName }

结果:

2.web域相关对象

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




Insert title here


<%
pageContext.setAttribute("page", "PAGE");
request.setAttribute("request", "REQUEST");
session.setAttribute("session", "SESSION");
application.setAttribute("application", "APPLICATION");
%>
${pageScope.page}===${page }
${requestScope.request}===${request }
<% pageContext.setAttribute("aa1", "PAGE"); request.setAttribute("aa2", "REQUEST"); session.setAttribute("aa", "SESSION"); application.setAttribute("aa", "APPLICATION"); %> =============================================
${pageScope.aa }===${aa }
${requestScope.aa }===${aa }
=============================================
${aa }

3.param和paramValues对象

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




 


num1:
num2:
num3:

  
num1:${param.num1 }
num2:${paramValues.num[0]}
num3:${paramValues.num[1]}


4.cookie对象

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




huihui


<%
response.addcookie(new cookie("username","莫"));
%>
获取cookie对象信息:${cookie.username }
cookie对象的名称:${cookie.username.name }
cookie对象的值:${cookie.username.value }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/602381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号