JSP技术

一.jsp脚本和注释

  • jsp脚本:

  1)  <%java 代码%>----------内部的java代码翻译到service 方法的内部

  2)  <%=java变量或表达式%>-------会被翻译成service方法内部的out.print()

  3)  <%java 代码%>会被翻译成servlet的成员的内容

  • jsp注释:

  1)  Html注释:  <!--注释内容-->

  2)  java注释:  //单行注释 /*多行注释*/ 

  3)  jsp注释:  <%--注释内容--%>

 

 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 2   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 3   <modelVersion>4.0.0</modelVersion>
 4   <groupId>com.werner</groupId>
 5   <artifactId>jsp2</artifactId>
 6   <packaging>war</packaging>
 7   <version>1.0-SNAPSHOT</version>
 8   <name>jsp2 Maven Webapp</name>
 9   <url>http://maven.apache.org</url>
10   <dependencies>
11     <dependency>
12       <groupId>junit</groupId>
13       <artifactId>junit</artifactId>
14       <version>3.8.1</version>
15       <scope>test</scope>
16     </dependency>
17     <dependency>
18       <groupId>javax.servlet.jsp</groupId>
19       <artifactId>javax.servlet.jsp-api</artifactId>
20       <version>2.3.1</version>
21       <scope>provided</scope>
22     </dependency>
23     <dependency>
24       <groupId>javax.servlet</groupId>
25       <artifactId>javax.servlet-api</artifactId>
26       <version>3.1.0</version>
27       <scope>provided</scope>
28     </dependency>
29     <!-- https://mvnrepository.com/artifact/javax.el/javax.el-api -->
30     <dependency>
31       <groupId>javax.el</groupId>
32       <artifactId>javax.el-api</artifactId>
33       <version>3.0.0</version>
34     </dependency>
35     <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl -->
36     <dependency>
37       <groupId>javax.servlet.jsp.jstl</groupId>
38       <artifactId>jstl</artifactId>
39       <version>1.2</version>
40     </dependency>
41     <!-- https://mvnrepository.com/artifact/taglibs/standard -->
42     <dependency>
43       <groupId>taglibs</groupId>
44       <artifactId>standard</artifactId>
45       <version>1.1.2</version>
46     </dependency>
47   </dependencies>
48   <build>
49     <finalName>jsp2</finalName>
50   </build>
51 </project>

 

<%@page language="java" contentType="text/html;UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.util.*" %>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
xxxxxxxxx
<!--html的注释-->
<%----%> jsp注释
<% int i=0;
System.out.println(i);

%>
<%=i %>
<%=1+1 %>
<%!
String str ="nihao CHIAN";
%>
<%=str%>
</body>
</html>

``````````````````````````````````````````````````````````````````````````````````````````````````````````


xxxxxxxxx jsp注释 0 2 nihao CHIAN


 

  • jsp运行原理-------jsp本质就是servlet

  JSP在第一次被访问时会被WEB容器翻译成servlet,在执行过程:

  第一次访问----->hellosServlet.java---->helloServlet_jsp.servlet----->编译运行PS

:被翻译后的servlet在Tomcat 的work目录中可以找到

  • jap指令(3个)

  jsp的指令是指导jsp翻译和运行的命令,jsp包括三大指令:

  1)page指令

  属性最多的一个指令,根据不同的属性,指导整个jsp页面

  <%@ page 属性名1="属性值1"  属性名2="属性值2">

  常见属性如下:

  language 语言:jsp嵌入的语言

  pageEncoding 表示jsp文件的本身编码

  contentType:responese.setContentType(UTF-8) 文本类型

  import:导包

  errorPage:

  

 1 <%@page language="java" contentType="text/html;UTF-8" pageEncoding="UTF-8"  errorPage="error.jsp" %>
 2 
 3 <html>
 4 <head>
 5     <meta http-equiv="content-type" content="text/html;charset=UTF-8">
 6     <title>Insert title here</title>
 7 </head>
 8 <body>
 9 <%
10     int y =1/0;
11 %>
12 </body>
13 </html>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/9/9
  Time: 21:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>error_page!!!!,你的代码有问题</h1>
</body>
</html>
---------------------------------------------------------------
浏览器返回:

error_page!!!!,你的代码有问题

 

  session:是否jsp在翻译时自动创建session,默认为true.

  2)include 指令

  页面包含(静态包含)指令,可以将一个jsp页面包含到另一个jsp页面中

  格式:   <%@ include file="被包含的文件地址">

  例如:<%@ include file="test.jsp">

  3)taglib指令

  格式:  <%@ taglib uri="标签库地址" pretix ="前缀">

  在jsp页面中引入标签库

  • jsp的九大隐式对象

  out:

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2017/9/9
 5   Time: 21:40
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
 9 <html>
10 <head>
11     <meta http-equiv="content-type" content="text/html;charset=UTF-8">
12     <title>Title</title>
13 </head>
14 <body>
15     ddddddddddd
16 <% out.write("aaaaaaaaaaaaaaaa");
17 response.getWriter().write("ccccccccccccc");
18 %>
19 <%= "ffffffffffffffff" %>
20 
21 
22 </body>
23 </html>
-----------------------------------------------------------------------------------------------------
浏览器运行结果:
http://localhost:8080/out.jsp
ccccccccccccc ddddddddddd aaaaaaaaaaaaaaaa ffffffffffffffff
  • pageContext域

   

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/9/10
  Time: 10:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

</head>
<body>
<%
//使用pageContext向request域存数据
    request.setAttribute("name","zhangsan");
    pageContext.setAttribute("name","maliu");
    pageContext.setAttribute("name","lisi",PageContext.REQUEST_SCOPE);
    pageContext.setAttribute("name","wangwu",PageContext.SESSION_SCOPE);
    pageContext.setAttribute("name","zhaosan",PageContext.APPLICATION_SCOPE);
%>
<%= request.getAttribute("name")%>
<%= pageContext.getAttribute("name",PageContext.REQUEST_SCOPE)%>
<%--findAttribute会从小到大搜索域的范围中的name--%>
<%--page域<request域<session域<application域--%>
<%=pageContext.findAttribute("name")%>
</body>
</html>

 

 四大作用域的总结:

    page域:  当前jsp页面范围

    request域:  一次请求

    session域:  一次会话

    application域:  整个web应用

 

  • include
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2017/9/10
 5   Time: 11:06
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <meta http-equiv="content-type" content="text/html" charset="UTF-8">
12     <title>Title</title>
13 </head>
14 <body>
15     <h1>This is include1 page</h1>
16 <%--包含include2--%>
17 <jsp:include page="/include2.jsp"></jsp:include>
18 
19 </body>
20 </html>
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2017/9/10
 5   Time: 11:26
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <meta http-equiv="content-type" content="text/html" charset="UTF-8">
12     <title>Title</title>
13 </head>
14 <body>
15 <h1>This is include2 page</h1>
16 </body>
17 </html>

--------------------------------------------------------------------------------
浏览器执行结果:http://localhost:8080/include.jsp

This is include1 page

This is include2 page

   转发forward

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2017/9/10
 5   Time: 11:47
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <meta http-equiv="content-type" content="text/html" charset="UTF-8">
12     <title>forward转发</title>
13 </head>
14 <body>
15 <jsp:forward page="forward2.jsp"></jsp:forward>
16 </body>
17 </html>
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Administrator
 4   Date: 2017/9/10
 5   Time: 11:48
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <meta http-equiv="content-type" content="text/html" charset="UTF-8">
12     <title>Title2</title>
13 </head>
14 <body>
15     <h1>good good studey ,day day up!</h1>
16 </body>
17 </html>
------------------------------------------------------------------------------------------
浏览器运行结果:http://localhost:8080/forward1.jsp

good good studey ,day day up!

 

 

 

 

  

posted @ 2017-09-09 20:25  友情天  阅读(141)  评论(0编辑  收藏  举报