JSP概述
jsp是用于开发动态资源网站的技术,相当于一个servlet
JSP的原理
当第一次访问jsp的时候。会将jsp翻译成为java代码,并且编译成为class文件,执行这个class文件,以后访问就直接执行class文件。
JSP和Servlet的区别
JSP用于进行显示数据
Servlet是控制器,用于获取表单数据,逻辑处理和分发转向。
JSP的基本语法
JSP的脚本:
小脚本<% %>
表达式<%=2+3%>等价于out.print(2+3);
${1+1}将会直接输出2
声明:<%!%>在类中1定义全局变量,静态代码块。
JSP中的3个指令
jsp指令是为jsp引擎而设置的,他们并不产生任何输出,而只告诉引擎如何处理JSP页面的其余部分。
jsp定义了三个指令:
page指令
include指令
taglib指令
语法:
<%@ 指令名称 属性1 = "属性值1" 属性2 = "属性值2"%>
例子:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
page指令:
作用:用于定义JSP页面的各种属性
属性:
> 1.1import和Java中的import的作用一致
> 1.2session:是否自动创建session对象。默认是true
> 1.3errorPage:当页面出错的时候会自动的跳转到指定的页面。
路径:"/"开头的表示当前应用,绝对路径,否则是相对路径
>1.4contextType:等同于response.setContextType("");
>1.5pageEncoding:设置使用的编码
>1.6isELIngored:是否支持EL表达式
格式:
<%@page pageEncoding=”“%>
include指令
静态包含:把其他资源包含到当前页面。
<%@include file=”/iclude/1.jsp”%>
动态包含:
两者的区别: 翻译的时间断不同
静态包含:在翻译的时候就将二者合并
后者:不会合并文件,但是当执行到include时候,才包含另一个文件的内容。
原则:能用静的就不用动的。
taglib指令
在jsp页面中导入JSTL标签库,用于替换jsp中方的Java代码。
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c” %>
JSP的6个动作
动态包含
请求转发
设置参数请求
创建一个对象
给指定的对象赋值
取出指定的对象属性值
JSP中的九个内置对象:在<%=%>和<%%>可以直接使用的对象
对象名:
application :ServletContext
request : HttpServletRequest
pageContext :PageContext
session : HttpSession
response : HttpServletResponse
out : 字符输出流
page:当前对象的this
config: ServletConfig
exception:isErrorPage=false的开关
pageContext:
- 本身是一个域对象,可以操作其他三个域对象(request,session. application)的数据
常用API
:void setAttribute(String name, Object o)
void getAttribute(String name)
void removeAttribute(String name)
操作其他域对象的方法
void setAttribute(String name, Object o, int scope)
void getAttribute(String name, int scope)
void removeAttribute(String name, int scope
scpoe的值:
PageContext.PAGE_SCOPE
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE)
**查找的方法**findAttribute(String name),按照域对象从小到大查找,找到就返回。
四大域对象
PageContext:在当前页面有效
request:HttpServletRequest:在一次请求中有效
session:HttpSession,在一次会话中有效
application:HttpServletContext:在整个app中有效
EL表达式
作用,简化jsp中的Java代码开发。是一种获取数据的规范。
${u }//输出u,使用的是PageContext.getAttribute("u");
注意:EL表达式只能取得四大域对象的值
EL对于null这样的数据表述的是空字符串
${u.name} ==>相当于u.getName()方法
点(.)相当于调用了getter方法
属性导航:${u.address.city}
[]相当于.,但是作用大于逗号
${stu.name} ==>${stu['name']}
<%
List list = new ArrayList();
list.add("aaa");
request.setAttribute("list", list);
%>
${list[1]}
<%
Map map = new HashMap();
map.put("a", "aaaa");
request.setAttribute("m", map);
%>
${m["a"]} ==${m.a}
运算:empty 是空返回true,否则返回true
JSTL的常用标签
通用标签:set out remove
属性:var表示变量名字,value表示变量内容,scope表示作用范围,test属性,用于if和choose标签中。
设置变量:<c:set var="num" value="${10+5}" scope="page"></c:set>
输出数据
移除变量
条件标签: if choose
3}”>//结果是true
<c: set var="num" value="30"></c:set>.
<c:choose>
<c:when test="${num == 1}">
第一名
</c:when>
<c:when test="${num==1}">
</c:when>
<c:when test="${num==3}">
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
迭代标签:
items:指的域的k值。 var是变量值,varStatus指向一个字符串,该字符串引用一个对象,这个对象记录着当前遍历元素的一些信息;
getIndex();返回索引,从0开始
getCount()返回计数,从1开始
isLast()是否是最后一个元素
isFirst()是否是第一个元素
代码:
public class DoLoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取表单数据
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
//处理业务逻辑
if("tom".equals(userName)&&"123".equals(pwd)){
request.getSession().setAttribute("name", userName);
request.getRequestDispatcher("/success.jsp").forward(request, response);
//response.sendRedirect(request.getContextPath()+"/success.jsp");
}else{
//response.sendRedirect(request.getContextPath()+"/login.jsp");
request.setAttribute("msg", "用户名或密码不正确!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
//分发转向
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<%@ 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>
<h1>欢迎来到本网站!</h1>
欢迎你:<%
/* String userName = request.getParameter("userName");
out.print(userName); */
String userName = (String)session.getAttribute("name");
out.print(userName);
%>
</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>Insert title here</title>
</head>
<body>
<%
String msg = (String)request.getAttribute("msg");
if(msg!=null)
out.print(msg);
%>
<form action="/day11_01_login/servlet/doLogin" method="post">
用户名:<input type="text" name="userName"/><br/>
密码:<input type="password" name="pwd"/><br/>
<input type="submit" value="登录"/><br/>
</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>Insert title here</title>
</head>
<body>
欢迎你:<%
String userName = (String)session.getAttribute("name");
out.print(userName);
%>
<a href="/day11_01_login/home.jsp">跳到主页</a>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" errorPage="error.jsp"%>
<!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>
<%--
int i = 10/0; //错误将会直接跳转到error.jsp页面
--%>
${1+1}
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
aaaaaaaaaaaaaaaaa
<%@include file="/5.jsp" %>
<%
if(5>3){
out.print(5);
}
%>
<c:if test="${5>3 }">
aaaaaaaaaaaaa
</c:if>
JSP的六个动作
<jsp:useBean id="stu1" class="Stu.Student"></jsp:useBean> <jsp:setProperty property="name" name="stu1" value="宋东"></jsp:setProperty> <jsp:getProperty property="name" name="stu1"/> <!-- http://localhost:8080/day11_02_jsp2/6.jsp?name=123 --> <jsp:forward page="/7.jsp"> <jsp:param value="123" name="name"/> <jsp:param value="333" name="pwd"/> </jsp:forward>
if/switch语句
<body> <c:if test="${3>2 }"> aaaa; </c:if> <c:set var="num" value="${8}"></c:set> <c:choose> <c:when test="${num==1 }">1111</c:when> <c:when test="${num==2 }">2222</c:when> <c:when test="${num==3 }">333</c:when> <c:otherwise> aaaa; </c:otherwise> </c:choose>
foreach语句
<body>
<c:forEach var="i" begin="1" end="10" step="2">
${i }<br/>
</c:forEach>
</body>
List list = new ArrayList();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.add("eee");
list.add("ffff");
request.setAttribute("list", list);
%>
<c:forEach items="${list}" var="l">
${l }
</c:forEach>