JSP

JSP(全称Java Server Pages)是由Sun Microsystem公司倡导和许多公司创建的一种使软件开发者可以相应客户端请求。

而动态生成H TML、XML或其他格式文档的Web网页的技术指标。

JSP技术是以Java语言作为脚本语言。JSP网页为整个服务器端的Java库单元提供一个借口来服务于HTTP的应用程序。

JSP是一个可视化的Servlet。

 

1、JSP的三大标签

    <%@    指令

    <%        java代码

    <%!         声明方法以及属性

    <%=      相当于out.println()

2、JSP的指令元素 共有三种:@page    @include  @taglib

    @page       用于设置JSP页面属性,这些属性用于和JSP容器。控制所生成的Srevlet结构

    @include    将外部内容包含到JSP文档中

    @taglib       声明用户使用自定义的标签,将标签库描述的文件导入jsp页面

3、JSP的九大隐式对象

1、request     封装用户请求

2、response     向用户做出响应

3、page       JSP页面本身

4、session     客户和服务器间的会话

5、config        包括Servlet初始化要用的参数

6、application      于服务器启动时开始运行,用来存放全局变量,在用户间共享

7、out                向客户端输出

8、pageContext   用于访问page的各种对象

9、exception   异常

1 <body>
2         你好
3         <%
4                 out.write("Hello");
5                 response.getWriter().write("Hiiiiiiii");
6         %>
7 </body>

 

从浏览器键入http://localhost:8080/20180912/index.jsp

执行结果:

response比ut.write()运行的快

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8" %>
 3     <%@ page import="java.util.Date" %>  导入包
 4     <%@page import="java.text.SimpleDateFormat"%>  
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type"  content="text/html;charset=UTF-8"">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12         你好
13         <%
14                 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
15                 out.write(sdf.format(new Date()));
16         %>
17 </body>
18 </html>

从浏览器键入http://localhost:8080/20180912/index.jsp

执行结果:

方法的声明和调用

1 <!-- 声明方法和属性 -->
2        <%! int a=1;%>
3        <%! public String abc(){
4            return "111";
5        }
6        %>
7 <!-- 调用  采用%= -->
8        <%=a%>
9        <%=abc() %>

4、 静态包含和动态包含

  静态包含:<%@ include file="被包含页面"%>

  动态包含:<jsp:include page="被包含页面" flush="true"></jsp:include>

 静态包含以及动态包含的区别:

  被包含的页面 不能添加base和响应头

  静态包含发生在:jsp----->java文件阶段    动态包含发生在:执行class文件阶段动态加入。

  静态包含:只生成一个java文件  动态包含:生成多个class文件

  无论是动态包含还是静态包含,其request对象都是相同的,也就是同一个request。

  静态包含速度优于动态包含

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8>
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10             <!-- 静态嵌入页面 -->
11             <%@ include file="static.jsp" %>
12             <!-- 动态嵌入页面 -->
13             <jsp:include page="abc.jsp"></jsp:include>
14 </body>
15 </html>

 5、报错页面处理

局部页面处理:

      errorPage   制定当前jsp页面错误的处理

      isErrorPage  如果是true,就处理页面的错误信息

处理404错误,404报错会出现提示,为了提高用户体验,将404页面进行视觉化处理

-----------------------------------error.jsp-------------------------------------

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"  %>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10        <img alt=""   src="img/123.png" >
11 </body>
12 </html>

-----------------------------------web.xml(公共)-------------------------------------

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <error-page>
 4   <error-code>404</error-code>
 5   <location>/error.jsp</location>  跳转到error.jsp页面
 6   </error-page>
 7   
 8  <error-page>
 9   <error-code>500</error-code>
10   <location>/error.jsp</location>
11   </error-page> 
12   
13   <display-name>20180913</display-name>
14   <welcome-file-list>
15     <welcome-file>index.html</welcome-file>
16     <welcome-file>index.htm</welcome-file>
17     <welcome-file>index.jsp</welcome-file>
18     <welcome-file>default.html</welcome-file>
19     <welcome-file>default.htm</welcome-file>
20     <welcome-file>default.jsp</welcome-file>
21   </welcome-file-list>
22 </web-app>

从浏览器键入URL:http://localhost:8080/20180913/12

执行结果:

 

500错误也可以跳转到error页面(验证)

-----------------------------------exception.jsp-------------------------------------

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8" errorPage="error.jsp" %>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10         <%
11         int i=1/0;  ---->此处会报500错误
12         %>
13 </body>
14 </html>

-----------------------------------error.jsp-------------------------------------

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"  isErrorPage="true" %>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10        <img alt=""   src="img/123.png" >
11     <%
12     if(exception!=null){
13         String msg=exception.getMessage();
14         if(msg!=null){
15             out.print(msg);  --打错错误信息
16         }
17     }
18     %>
19 </body>
20 </html>

从浏览器键入URL:http://localhost:8080/20180913/exception.jsp

执行结果:

原文地址:https://www.cnblogs.com/echola/p/9647252.html