JSP:从用户角度来看是一个网页,从开发人员来看就是一个java类,继承了servlet,所以实际上就是一个servlet。
1.jsp的三大指令
指令的写法
<%@ 指令名字 %>
1.page指令
- language:表面jsp页面可以写java代码
- contentType:告诉浏览器这个文件是什么内容类型以及使用什么编码
contentType="text/html; charset=UTF-8"
text/html MIMEType 这是一个文本,html网页
- pageEncoding:jsp的内容编码
- extends:用于指定jsp翻译成java文件后继承的父类,一般不用修改
- import:导包,一般不手写,用快捷键alt+/
- session:值这能选true或false,用于控制在这个jsp页面中是否能够直接使用session对象
- errorPage:指的是错误的页面,值需要给错误的页面路径 ,用于指定错误的时候跑到哪一个页面去
- isErrorPage:用于声明某个页面到底是不是错误的页面
2.include指令:包含另外一个jsp的内容进来
- 静态包含:把另外一个页面的所有内容拿过来一起输出,所有的标签元素都包含进来
- 动态包含:使用jsp的动态标签(jsp:include), 只把它的运行结果拿过来
3.taglib指令:
- <%@ taglib prefix="" url="" %>
- uri:标签库路径
- prefix:标签库的别名
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="index2.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
hello index<br>
收到的参数为:<br>
<%=
request.getParameter("age")
%>
</body>
</html>
2.JSP的动作标签:
- include:能包含第二个jsp文件
- forward:请求转发,前往到指定页面
- param:在包含或者跳转到某个页面的时候,加入这个参数
action.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>
<%--
<jsp:include page=""></jsp:include>
<jsp:param value="" name=""/>
<jsp:forward page=""></jsp:forward>
--%>
<%--
能包含第二个jsp文件<br>
<jsp:include page="index2.jsp"></jsp:include>
hello action!<br>
请求转发,前往到指定页面<br>
<jsp:forward page="index.jsp"></jsp:forward>
等同于
<%
request.getRequestDispatcher("index.jsp").forward(request,response);
%>
--%>
在index.jsp中获取参数<br>
<jsp:forward page="index.jsp">
<jsp:param value="22" name="age"/>
</jsp:forward>
</body>
</html>
3.jsp内置对象:我们可以直接在jsp页面中使用这些对象,不需要创建
- 作用域对象:作用域指的是这些对象可以存值,它们的取值范围有限定。通过setAttribute和getAttribute方法进行值的操作
- pageContext(PageContext):作用域仅限于当前页面
- request(HttpServletRequest):作用域仅限于一次请求,只要服务器对该请求做出了响应,这个域中的值就没有了
- session(HttpSession):作用域限于一次会话(多次请求与响应)
- application(ServletContext):整个工程都可以访问,当服务器关闭后就不能访问了
- response(HttpResponse):与out同时出现时,先输出的是response的内容
- out(JspWriter):out对象输出的内容放到response的缓冲区
- config(ServletConfig):处理异常
- page(Object):这个jsp翻译成的java类的实例对象
- exception(Throwable):获取初始化的值
object.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>
使用作用域来存储数据<br>
<%
pageContext.setAttribute("name","page");
request.setAttribute("name","request");
session.setAttribute("name","session");
application.setAttribute("name","application");
%>
取出四个作用域中的值<br>
<%= pageContext.getAttribute("name")%>
<%= request.getAttribute("name")%>
<%= session.getAttribute("name")%>
<%= application.getAttribute("name")%>
跳转到下一个页面<br>
<%
//请求转发,一次请求
//request.getRequestDispatcher("object2.jsp").forward(request,response);
//重定向,两次请求
response.sendRedirect("object2.jsp");
%>
</body>
</html>
object2.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>
hello object2<br>
<%= pageContext.getAttribute("name")%>
<%= request.getAttribute("name")%>
<%= session.getAttribute("name")%>
<%= application.getAttribute("name")%>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>
<!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>
hello error<br>
<%
config.getInitParameter("name");
%>
<%
out.write("hello error1!");
%>
<br>
<%
response.getWriter().write("hello error2!");
%>
</body>
</html>
EL表达式:为了简化jsp代码,实际上就是简化在jsp里面写的java代码。
写法:${表达式}
EL表达式:为了简化jsp代码,实际上就是简化在jsp里面写的java代码
写法:${表达式}
1).普通情况下取值取出四个作用域中存放的值
2).如果域中所存的值是数组时的EL取值
3).如果域中所存的值是集合时的EL取值
4).如果域中所存的值是map时的EL取值
取值方式:如果有下标,那么直接用[],没有下标则用.的方式去取
EL表达式一般用在从一个对象中取出它的属性值
el.jsp
<%@page import="jsp.domain.User"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ 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>
<%
pageContext.setAttribute("name","page");
request.setAttribute("name","request");
session.setAttribute("name","session");
application.setAttribute("name","application");
%>
普通情况下取值<br>
<%= pageContext.getAttribute("name")%>
<%= request.getAttribute("name")%>
<%= session.getAttribute("name")%>
<%= application.getAttribute("name")%>
<br>
使用EL表达式取出作用域中的值<br>
${ pageScope.name }------直接指定到这个作用域里面去找这个name
${ requestScope.name }
${ sessionScope.name }
${ applicationScope.name }
<br>
如果域中所存的值是数组时的EL取值<br>
<%
String[] array = {"aa","bb","cc","dd"};
pageContext.setAttribute("array",array);
%>
${array[0] },${array[1] },${array[2] },${array[3] }
<br>
如果域中所存的值是集合时的EL取值<br>
<%
List list=new ArrayList();
list.add("11");
list.add("22");
list.add("33");
pageContext.setAttribute("li",list);
%>
${li[0] },${li[1] },${li[2] }
<br>
如果域中所存的值是map时的EL取值<br>
<%
Map map=new HashMap();
map.put("a","1");
map.put("b","2");
map.put("c","3");
map.put("c.qq","4");
pageContext.setAttribute("map",map);
%>
${map.a },${map.b },${map.c },${map["c.qq"] }
<br>
如果域中所存的值是自定义User对象时的EL取值<br>
<%
User user=new User("aaaa",28);
session.setAttribute("u",user);
%>
${ u.name },${ u.age } <br>
---判断user对象是否为空<br>
${ empty u }
</body>
</html>
EL表达式的11个内置对象:${ 对象名.成员 }
作用域相关:pageScope,requestScope,sessionScope,applicationScope
请求头相关:header,headerValues
请求参数相关:param,paramValues
全局初始化参数:initParam
其他:cookie,pageContext
JSTL:jsp的标签库,用来简化jsp的代码编写,替换<%%>写法,一般与EL表达式配合。
使用步骤:
1.导入JSTL支持jar文件jstl.jar和standard.jar(使用1.1的jstl,1.0不支持el表达式)
2.在jsp页面上使用taglib来引入标签库
jstl.jsp
<%@page import="jsp.domain.User"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!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>
声明一个对象,对象的值,进行存储 <br>
<c:set var="name" value="qq"></c:set>//会默认存到page域里<br>
<c:set var="name2" value="qq2" scope="session"></c:set>
${ name }<br>
${ sessionScope.name2 }<br>
<c:set var="age" value="12"></c:set>
//判断test里面的EL表达式是否满足,如果满足,就执行c:if标签中输出,可以定义一个变量名flag,去接收前面表达式的值,然后存在session域中<br>
<c:if test="${age>10 }" var="flag" scope="session">age大于10岁</c:if> <br>
${flag }<br>
//用于作遍历,从1开始到10,得到的结果赋值给i,并且会存储到page域中, step代表的是增幅为2<br>
<c:forEach begin="1" end="10" var="i" step="2">
${i }
</c:forEach>
<br>
<%
List list=new ArrayList();
list.add(new User("aa",11));
list.add(new User("bb",22));
list.add(new User("cc",33));
list.add(new User("dd",44));
pageContext.setAttribute("list",list);
%>
<br>items表示要遍历哪一个对象,这里必须写EL表达式,遍历出来的每一个元素用user取接收<br>
<c:forEach var="user" items="${list }">
${user.name },${user.age }....
</c:forEach>
</body>
</html>
在测试的过程中用到自己定义的User类:
user.java
package jsp.domain;
public class User {
private String name;
private int age;
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
private User() {
super();
}
}
感谢每一位阅读的人~