<%@ page import="java.util.*" pageEncoding="UTF-8"%>
<%@ page errorPage="b.jsp" %>
<%
if(true)
throw new Exception("哈哈~");
%>
b.jsp
<%@ page pageEncoding="UTF-8"%>
出错啦!
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>
出错啦!
<%=exception.getMessage() %>
设置b.jsp页面为错误页,在错误页中就可以使用exception隐藏对象了。
注意:一旦转发到错误页,那么Tomcat会把状态码设置为500,而不在是200了。
web.xml
404
/error404.jsp
500
/error500.jsp
java.lang.RuntimeException
/error.jsp
如果没有配置这个< error-page>,在抛出RuntimeException时会转发到error500.jsp页面。即匹配响应码500
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException { //request和response两个对象是方法参数。
PageContext pageContext = null; //pageContext对象
HttpSession session = null; //session对象
ServletContext application = null;//application对象
ServletConfig config = null;//config对象
JspWriter out = null;//out对象
Object page = this;//page对象
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
// 从这里开始,才是JSP页面的内容,JSP页面的内容从这里才开始,所以可以使用上面定义的变量!这就是9大内置对象为什么可以在JSP页面中无需创建就可以使用的奥秘了!
}…
pageContext.setAttribute("x", "X"); //向pageContext中存储数据
pageContext.setAttribute("x", "XX", PageContext.REQUEST_SCOPE);//向request中存储数据
pageContext.setAttribute("x", "XXX", PageContext.SESSION_SCOPE);//向session中存储数据
pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE);//向application中存储数据
hel.jsp
lo.jsp
<%
out.println("lo.jsp
");
%>
<%
out.println("lo.jsp
");
%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
a.jsp
a.jsp
//给被包含的页面b.jsp传递参数。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
b.jsp
b.jsp
<%
String username = request.getParameter("username"); //获取参数
out.print("你好:" + username);
%>
package cn.itcast.domain;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
public class User {
private String iD; //错误的JavaBean属性
private String ID;
private String qQ;//错误的JavaBean属性
private String QQ;
…
}
JavaBean可能存在属性,但不存在这个成员变量,例如:
public class User {
public String getUsername() {
return "zhangSan";
}
}
public class User {
private String hello;
public String getUsername() {
return hello;
}
public void setUsername(String username) {
this.hello = username;
}
}
Map map = new HashMap();
map.put("username", "admin");
map.put("password", "admin123");
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
public void fun1() throws Exception {
Map map = new HashMap();
map.put("username", "admin");
map.put("password", "admin123");
BeanInfo beanInfo = Introspector.getBeanInfo(User.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();//获取User类型的所有JavaBean属性的属性描述符对象
User user = new User();
for(PropertyDescriptor pd : pds) {
String name = pd.getName();
String value = map.get(name);
if(value != null) { //如果在Map中存在这个属性的值
Method writeMethod = pd.getWriteMethod(); /获取该属性的写方法
writeMethod.invoke(user, value);//把Map中的值写入到user对象的对应属性中
}
}
System.out.println(user);
}
User user = new User();
BeanUtils.setProperty(user, "username", "admin"); //设置user对象的username属性为admin
BeanUtils.setProperty(user, "password", "admin123"); //设置user对象的password属性为admin123
System.out.println(user);
3.2 获取JavaBean属性
User user = new User("admin", "admin123");
String username = BeanUtils.getProperty(user, "username");//获取user对象的username属性值
String password = BeanUtils.getProperty(user, "password");//获取user对象的password属性值
System.out.println("username=" + username + ", password=" + password);
3.3 封装Map数据到JavaBean对象中
Map map = new HashMap();
map.put("username", "admin");
map.put("password", "admin123");
User user = new User();
BeanUtils.populate(user, map);//把map中的数据封装到user对象中,要求user的属性名要与map的key一致。
System.out.println(user);
package cn.itcast.domain;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
本节代码:
package cn.itcast.domain;
/*
* 必须要为成员提供get/set方法(两者只提供一个也是可以的!)
* 必须要有默认构造器!(没参的!)
* 一般对于具有get/set方法的成员变量称之为属性
*
* 其实就算一个属性没有对应的成员变量,只有get/set方法也是可以的!
* 属性的名称就是get/set方法去除get/set后,再把首字母小写了!
*/
public class Person {
private String name;
private int age;
private String gender;
private boolean bool;
public boolean isBool() {
return bool;
}
public void setBool(boolean bool) {
this.bool = bool;
}
public String getId() {
return "fdsafdafdas";
}
public String getName() {
return name;
}
public void setName(String username) {
this.name = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", gender=" + gender
+ "]";
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age, String gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
}
package cn.itcast.domain;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
}
package cn.itcast.utils;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.beanutils.BeanUtils;
public class CommonUtils {
/**
* 生成不重复的32位长的大写字符串
* @return
*/
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "").toUpperCase();
}
/**
* 把map转换成指定类型的javaBean对象
* @param map
* @param clazz
* @return
*/
public static T toBean(Map map, Class clazz) {
try {
/*
* 1. 创建指定类型的javabean对象
*/
T bean = clazz.newInstance();
/*
* 2. 把数据封装到javabean中
*/
BeanUtils.populate(bean, map);
/*
* 返回javabean对象
*/
return bean;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
}
package cn.itcast.domain;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
import cn.itcast.utils.CommonUtils;
public class Demo1 {
@Test
public void fun1() throws Exception {
String className = "cn.itcast.domain.Person";
Class clazz = Class.forName(className);
Object bean = clazz.newInstance();
BeanUtils.setProperty(bean, "name", "张三");
BeanUtils.setProperty(bean, "age", "23");
BeanUtils.setProperty(bean, "gender", "男");
BeanUtils.setProperty(bean, "xxx", "XXX");
String age = BeanUtils.getProperty(bean, "age");
System.out.println(age);
System.out.println(bean);
}
/*
* 把map中的属性直接封装到一个bean中
*
* Map: {"username":"zhangSan", "password","123"}
* 我们要把map的数据封装到一个javabean中!要求map的key与bean的属性名相同!
*/
@Test
public void fun2() throws Exception {
Map map = new HashMap();
map.put("username", "zhangSan");
map.put("password", "123");
User user = new User();
BeanUtils.populate(user, map);
System.out.println(user);
}
@Test
public void fun3() {
Map map = new HashMap();
map.put("username", "zhangSan");
map.put("password", "123");
/*
* request.getParameterMap();
*/
User user = CommonUtils.toBean(map, User.class);
System.out.println(user);
}
}
package cn.itcast.domain;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User() {
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
public class Person {
private String name;
private int age;
private String sex;
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'a.jsp' starting page
${cookie.JSESSIONID.value }
package cn.itcast.domain;
public class Employee {
private String name;
private double salary;
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + ", address="
+ address + "]";
}
public String getHehe() {
return "我去...";
}
}
package cn.itcast.domain;
public class Address {
private String city;
private String street;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return "Address [city=" + city + ", street=" + street + "]";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.itcast.domain.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'a.jsp' starting page
<%
Address address = new Address();
address.setCity("北京");
address.setStreet("西三旗");
Employee emp = new Employee();
emp.setName("李小四");
emp.setSalary(123456);
emp.setAddress(address);
request.setAttribute("emp", emp);
%>
使用el获取request域的emp
${requestScope.emp.address.street }
${emp.hehe }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'a.jsp' starting page
${header['User-Agent']}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'a.jsp' starting page
${initParam.xxx }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'a.jsp' starting page
${pageContext.request.contextPath }
点击这里
${pageContext.session.id }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'a.jsp' starting page
<%-- map.key这是el的语法!
map['key']也可以操作map
--%>
${param.username }
${paramValues.hobby[0] }
${paramValues.hobby[1] }
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
…
String[] strs = {"a", "b","c"};
List list = new ArrayList();
list.add("a");
pageContext.setAttribute("arr", strs);
pageContext.setAttribute("list", list);
%>
${fn:length(arr) }
${fn:length(list) }
${fn:toLowerCase("Hello") }
${fn:toUpperCase("Hello") }
${fn:contains("abc", "a")}
${fn:containsIgnoreCase("abc", "Ab")}
${fn:contains(arr, "a")}
${fn:containsIgnoreCase(list, "A")}
${fn:endsWith("Hello.java", ".java")}
${fn:startsWith("Hello.java", "Hell")}
${fn:indexOf("Hello-World", "-")}
${fn:join(arr, ";")}
${fn:replace("Hello-World", "-", "+")}
${fn:join(fn:split("a;b;c;", ";"), "-")}
${fn:substring("0123456789", 6, 9)}
${fn:substring("0123456789", 5, -1)}
${fn:substringAfter("Hello-World", "-")}
${fn:substringBefore("Hello-World", "-")}
${fn:trim(" a b c ")}
${fn:escapeXml("")}
1.0
itcast
http://www.itcast.cn/jsp/functions
test
cn.itcast.el.funcations.ItcastFuncations
String test()
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="itcast" uri="/WEB-INF/itcast.tld" %>
${itcast:test() }