0313 jsp

在jsp中可以写java代码

三种格式

(1)<%java代码%>

(2)<%=java变量或表达式%>

(3)<%!java代码%>

代码展示

新建demo01.jsp

<body>
	<%
	int y=0;
	System.out.println(y);
	session.setAttribute("name", "张三");
	
	%>
	<%=y %>
	<%! String str="hello"; %>
	<%=str %>
	<%=session.getAttribute("name") %>
</body>

  

 jsp本身就是一个servlet,jsp在第一次被访问时会被Web容器翻译成servlet

流程:第一次访问---->helloServlet.jsp---->helloServlet_jsp.java---->编译运行,被翻译后的servlet在Tomcat的work目录中可以找到

jsp指令

(1)page指令 --- 属性最多的指令

常用属性

language:jsp脚本中可以嵌套进来的语言

pageEncoding:当前jsp文件的本身编码---内部可以包含contentType

contentType:例:response.setContentType(text/html;charset=UTF-8) 解决响应乱码

session:是否jsp在翻译时自动创建session(默认是true,自动创建session对象,如果不想创建可以写 session=“false”)

import:导入java的包

errorPage:当当前页面出错后跳转到哪个页面

isErrorPage:当前页面是一个处理错误的页面

代码展示

demo01.jsp

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" errorPage="error.jsp" session="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>
	<%
	ArrayList<String> list=new ArrayList<String>();
	int y=1/0;
	System.out.println(y);
	session.setAttribute("name", "张三");
	
	%>
	<%=y %>
	<%! String str="hello"; %>
	<%=str %>
	<%=session.getAttribute("name") %>
</body>
</html>

  上述代码中故意写了一个错误1/0,因为设置了报错会调到页面error.jsp,那么在error.jsp中要声明isErrorPage

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>
这是处理错误页面
</body>
</html>

  访问demo01.jsp

 那我们经常见到404和500错误页面,我们也可以设置一个跳转到自己的错误信息页面,就在xml文件中加标签

例:

  <error-page>
  <error-code>404</error-code>
  <location>/error.jsp</location>
  </error-page>
  <error-page>
  <error-code>500</error-code>
  <location>/error.jsp</location>
  </error-page>

(2)include指令

 就上图这样的结构,建demo02.jsp,通过引入头部jsp和尾部jsp引入

header.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>
头部
</body>
</html>

  

footer.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>
尾部
</body>
</html>

  demo02.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>
<%@include file="/header.jsp" %>
正文
<%@include file="/footer.jsp" %>
</body>
</html>

  

 (3)taglib指令(后续将学习)

jsp内置/隐式对象(9个)jsp被翻译成servlet之后,service方法中有9个对象定义并初始化完毕,我们在jsp脚本中可以直接使用这9个对象

(1)out          javax.servlet.jsp.JspWriter 用于页面输出

(2)request    javax.servlet.http.HttpServletRequest 得到用户请求信息

(3)response  javax.servlet.http.HttpServletResponse 服务器向客户端的回应信息

(4)config      javax.servlet.ServletConfig 服务器配置,可以取得初始化参数

(5)session    javax.servlet.http.HttpSession 用来保存用户的信息

(6)application javax.servlet.ServletContext 所有用户的共享信息

(7)page       java.lang.Object 指当前页面转换后的Servlet类的实例(在普通类中的this)

(8)pageContext javax.servlet.jsp.PageContext JSP的页面容器

(9)exception java.lang.Throwable 表示JSP页面所发生的异常,在错误页中才起作用

正常jsp有前八个内置对象,只有像上述中的error.jsp中会有第九个异常对象

重点分析 out对象和 pageContext对象

1、out对象

out对象的类型是JspWriter

out作用就是向客户端输出内容----out.write()

out.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>
aaaaaaaa
<%="bbbbbbbb" %>
<%
response.getWriter().write("cccccccc");
out.write("dddddddd");
%>
</body>
</html>

  

 输出的内容顺序是跟我们预想的顺序不一样

是因为 aaaaa,bbbbb,ddddd 都进到了out缓冲区。而ccccc进到了response缓冲区,进到out缓冲区的内容要再进到response缓冲区然后再输出。

图解

 如果不想这样,那可以将out缓冲区关闭 out缓冲区默认8kb 可以设置成0 代表关闭out缓冲区 内容直接写到respons缓冲 器

代码展示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" buffer="0kb"%>
<!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>
aaaaaaaa
<%="bbbbbbbb" %>
<%
response.getWriter().write("cccccccc");
out.write("dddddddd");
%>
</body>
</html>

  

2、pageContext对象(域对象)

作用域只在本jsp中,所以不能实现数据传递,不能在这个jsp中存入数据,在另一个jsp中取出

但是 该对象可以往别的域中存储数据

pageContext可以向指定的其他域中存取数据

setAttribute(String name,Object obj,int scope)

getAttribute(String name,int scope)

removeAttrbute(String name,int scope)

findAttribute(String name)

---依次从pageContext域,request域,session域,application域中获   取属性,在某个域中获取后将不在向后寻找

代码展示

<%@ 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>
<%
	//reguest域存值
	pageContext.setAttribute("name", "李四", pageContext.REQUEST_SCOPE);
	//session域中存值
	pageContext.setAttribute("name", "王五", pageContext.SESSION_SCOPE);
	//aplisation
	pageContext.setAttribute("name", "赵六", pageContext.APPLICATION_SCOPE);
%>
<%=pageContext.getAttribute("name",pageContext.REQUEST_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.SESSION_SCOPE) %>
<%=pageContext.getAttribute("name",pageContext.APPLICATION_SCOPE) %>
<%=pageContext.findAttribute("name") %>
</body>
</html>

  

 可以通过pageContex获得八大隐式对象 例 pageContex.getRequest

3、jsp标签动作

上述中我们可以通过include指令将其他的jsp引入到我们应用的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="/header.jsp" ></jsp:include>
正文
<jsp:include page="/footer.jsp" ></jsp:include>
</body>
</html>

  动态包含 在我们访问的时候 将header.jsp和footer.jsp也都编译成了servlet文件

静态包含 当我们访问的时候并没有将header.jsp和footer.jsp编译,而是将这两个文件的代码直接类似粘贴到了主jsp文件中

原文地址:https://www.cnblogs.com/-gongxue/p/14529329.html