javaBean是java开发语言中一个可以重复使用的软件组件。它必须有一个公共的、无参的构造方法这个方法可以是编译器自动产生的默认构造方法;它提供公共的setter方法和getter方法,让外部程序设置和获取javaBean。
建立一个Book类:
package cn.itcast.chapter07.javabean;
public class Book{
private double price;
public double getPrice(){
return price;
}
public void setPrice(double price){
this.price = price;
}
}
建立一个Student类:
package cn.itcast.chapter07.javabean;
public class Student{
private String sid;
private String name;
private int age;
private boolean married;
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public boolean isMarried(){
return married;
}
public viid setMarried(boolean married){
this.married = married;
}
public String getSid(){
return sid;
}
public void setName(String name){
this.name = name;
}
public void getInfo(){
System.out.print("我是一个学生");
}
}
BeanUtils工具是Apache软件基金会提供的一套简单、易用的API。
在已建立的lib目录下添加下载的commons-beanutils-1.9.2.jar和Logging的JAR包commons-logging-1.2.jar,并在src目录下建立Person类:
package cn.itcast.chapter07.beanutils;
public class Person{
private String name;
private int age;
private String getName(){
return age;
}
private void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
}
建立一个BeanUtilsDemo类:
package cn.itcast.chapter07.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{
Person p = new Person();
BeanUtils.setProperty(p, "name", "Jack");
BeanUtils.setProperty(p, "age", 10);
String name = BeanUtils.getProperty(p, "name");
String age = BeanUtils.getProperty(p, "age");
System.out.println("我的名字是"+name+",我今年"+age+"岁了!");
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "张三");
map.put("age", 10);
BeanUtils.populate(p.map);
System.out.println("姓名:"+p.getName()+",年龄"+p.getAge());
}
}
EL是Expression Language的缩写,它是一种简单的数据访问语言。
创建一个MyServlet类:
package cn.itcast.chapter07.servlet;
import java.io.*;
import java.servlet.*;
import java.servlet.http.*;
public class MyServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
request.setAttribute("username", "itcast");
request.setAttribute("password", "123");
RequestDispatcher dispatcher = request.getRequestDispatcher("/myjsp.jsp");
dispatcher.forward(request, response);
}
public viod doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
doGet(request, response);
}
}
在WebContent目录下编写一个myjsp.jsp文件:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body>
用户名:<% =request.getAttribute("username") %></br>
密码:<% =request.getAttribute("password") %></br>
<hr />
使用EL表达式:<br />
用户名:${username}<br />
密码:${password}<br />
</body>
</html>
pageContext隐式对象(创建一个pageContext.jsp):
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body>
请求URI为:${pageContext.request.requestURI}<br />
Content-Type响应头:${pageContext.response.contentType}<br />
服务器信息为:${pageContext.servletContext.serverInfo}<>br />
Servlet注册名为:${pageContext.servletConfig.servletName}<br />
</body>
</html>
param和paramValues对象(创建一个param.jsp):
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body style="text-align: center;">
<from action="${pageContext.request.contextPath}/param.jsp">
num1:<input type="text" name="num1"><br />
num2:<input type="text" name="num"><br />
num3:<input type="text" name="num"><br />
<imput type="submit" value="提交" /><br />
<imput type="submit" value="重置" /><br />
num1:<param.num1><br />
num2:<paramValues.num[0]><br />
num3:<paramValues.num[1]><br />
</form>
</body>
</html>
Cookie对象(创建一个cookie.jsp):
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head></head>
<body>
Cookie对象的信息:<br />
${coolie.userName}<br />
Cookie对象的名称和值:<br />
${cookie.userName.name} = ${cookie.userName.value}
<% response.addCookie(new Cookie("userName", "itcast")); %>
</body>
</html>