java基础80 JSP基础知识点(网页知识)

1、jsp的引入

    Servlet的作用:用java语言开发动态资源技术!!!
    Jsp的作用:用java语言(+html语言)开发的动态资源技术!!!     Jsp就是servlet

问题:为什么jsp就是servlet?
因为:Jsp翻译成java文件
public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent
即:
org.apache.jasper.runtime.HttpJspBase类:extends org.apache.jasper.runtime.HttpServlet implemets javax.servlet.jsp.HttpjspPage

结论:jsp就是一个servlet程序,Servlet的技术可以用在jsp中,Jsp技术并不是全部使用于servlet

2、jsp的特点

    1)jsp的运行必须交给tomcat服务器。  Tomcat的work目录:tomcat服务器存放jsp运行时的临时文件
    2)jsp页面既可以写html代码,也可以java代码。   (html页面不能写java代码,而jsp页面是可以写java代码)

3、jsp的执行过程

    1、访问hello.jsp页面,tomcat扫描jsp文件,apache-tomcat-7.0.73\work把jsp文件翻译成java源文件:hello.jsp ----> hello_jsp.java(翻译)
    2、tomcat服务器把java源文件编译成class字节码文件:hello_jsp.java ---> hello_jsp.class(编译)
    3、tomcat服务器构造hello_jsp类的对象
    4、tomcat服务器调用hello_jsp类里面的方法,返回内容显示到浏览器

 1 <%@ page language="java" import="java.util.*,java.text.*" pageEncoding="utf-8" %>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'hello.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <% 
27         //这里写的都是java代码
28         //获取当前时间
29         SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
30         String curTime=format.format(new Date());
31         //输出内容到浏览器(下面两个效果是一样的)
32         response.getWriter().write(curTime);
33         //out.write(curTime);
34     %>
35   </body>
36 </html>

注意:jsp文件被修改或者jsp临时文件被删除了,重新走编译1和编译2的过程

4、Jsp的生命周期

Jsp的生命周期 Servlet的生命周期
1、翻译:jsp-java文件
2、编译:java文件-class文件(servlet程序
3、构造方法(第一次方法)
4、Init方法(第一次访问):jspinit()
5、Service方法:jspService()
6、Destroy方法:jspDestory();
1、构造方法(第一次访问)
2、Init方法(第一次访问)
3、Service方法
4、Destroy方法

5、jsp的语法

5.1、jsp的表达式

    语法:<%=变量或表达式%>
    作用:向浏览器输出变量值或者表达式计算的结果
    注意:
        1)表达式的原理就是翻译成了out.print(“变量”);通过该方法向浏览器写出内容
        2)表达式后面不需要带分号结束

5.2、jsp的脚本

    语法:<%java代码%>
    作用:执行java代码
    注意:原理把脚本中的java代码原封不动拷贝到jspService方法中执行

5.3、jsp的声明

    语法:<%!变量或方法%>
    作用:声明jsp的变量或方法
    注意:变量翻译成成员变量,方法翻译成成员方法.

5.4、jsp的注释

    语法:<%-- jsp注释--%>
    注意:html注释会被翻译和执行,而jsp的注释不能被翻译和执行

6、jsp中的三大指令

6.1、include指令

    作用:在当前页面用于包含其他页面
    语法:%@include file=”common/header.jsp”%
    注意:
        1、原理是把被包含的页面(header.jsp)的内容翻译到包含页面(index.jsp),合并翻译成一个java源文件,再编译运行,这种包含叫静态包含(源码包含)
        2、如果使用静态包含,被包含的页面不需要出现全局的html标签(源码包含)

6.2、page指令

1 <%@ page language="java" import="java.util.*,java.text.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8" %>

   解析:

        language="java"  //告诉我们的服务使用什么语言来翻译jsp文件
        import="java.util.*,java.text.*"  //告诉服务器java文件使用什么包,导入包,多个包之间用逗号分割
        pageEncoding="utf-8"  //告诉服务器使用什么编码翻译jsp文件(成java文件)
        contentType="text/html; charset=utf-8"  //服务器发送浏览器的数据类型和内容编码

        Errorpage=”error.page” //错误页面
        IsErrorpage=”fasle”;  //判断是不是错误页面
        Buffer=”8kb”  //页面缓冲区的大小
        Session=”true”  //是否能够使用session
        IsElIgnored=”false”   //是否忽略EL表达式

6.3、taglib指令

 

 

附录1

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'script.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25            <!--jsp表达式-->
26            <%
27                //变量
28                String name="jack";
29                int a=10;
30                int b=20;
31                //System.out.println(name);
32             %>
33             <%=name %>
34             <br/>
35             <%=(a-b) %>
36             <br/>
37          
38             <!--jsp脚本-->
39             <%
40             //生成随机数
41             Random ran=new Random();
42             float num=ran.nextFloat();
43             %>
44             <%=num %>
45         <hr/> 
46              
47         <!--练习:使用脚本和html代码显示99乘法表-->
48          <% 
49              String[] color={"red","green","blue"};
50                  for(int i=1;i<=9;i++){
51                      for(int j=1;j<=i;j++){
52                          out.print("<span style='color:"+color[i%3]+"'>"+j+"*"+i+"="+(i*j)+"&nbsp</span>");                 
53         %>
54                         <%=i%> x <%=j%>=<%=(i*j)%>&nbsp; <!-- 这行代码和上面的out.print();是一样的,都是向页面输出 -->    
55                     <% 
56                      }
57                      %>
58                      <br/>
59                  <% 
60                  }
61                 %> 
62                 
63          <!--html的注释方式-->
64          <%--jsp的注释--%>
65          <%!
66              //变量
67              String name="rose";
68              
69              /*
70              jsp声明中不能重复定义翻译一些方法
71               public void _jspInit() {}
72              */
73          %>
74   </body>
75 </html>

附录2

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" buffer="1kb" %>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'MyJsp.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   <body>
24        <%
25           //方法1:顶部设置缓冲区的大小为1Kb(buffer="1kb"26            for(int i=1;i<=500;i++){
27            out.write("123");
28            }
29            //查看缓冲区大小
30            System.out.println("当前缓冲区的大小:"+out.getBufferSize());
31            //查看缓冲区剩余大小
32            System.out.println("剩余缓冲区大小:"+out.getRemaining());
33            //方法2:刷新缓存
34            //out.flush();
35         
36            response.getWriter().write("a"); //解析:方法1或2,都能让123先输出来,最后输出a;如果两者都去掉,那么先输出a,后输出123.
37         %>
38   </body>
39 </html>

附录3

web.xml 配置文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7     <error-page>
 8         <error-code>500</error-code>
 9         <location>/common/500.jsp</location>
10     </error-page>
11       <error-page>
12           <error-code>404</error-code>
13           <location>/common/404.html</location>
14       </error-page>
15 </web-app>

用户当前访问的页面

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'page.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22   </head>
23   
24   <body>
25         <% 
26         String name=null;
27         name.charAt(1); //这里是一个错误; charAt:这是一个字符串,括号里填1(整形)的话 就错了
28         %>     
29   </body>
30 </html>

用户访问后,处理错误信息的页面(显示给用户看的页面)

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
 2 isErrorPage="true"  <!-- 这里要声明为错误页面 -->
 3 %>
 4 
 5 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 6 <html>
 7   <head>
 8     <title>错误页面500.jsp</title>
 9   </head>
10   
11   <body>
12       亲:系统发送小小的错误,请耐心等待,管理员正在拼命维修中....
13     错误的原理:<%=exception.getMessage() %>
14   </body>
15 </html>

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/9623946.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2018-09-10 23:10  DSHORE  阅读(457)  评论(0编辑  收藏  举报