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类里面的方法,返回内容显示到浏览器

 <%@ page language="java" import="java.util.*,java.text.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'hello.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%
//这里写的都是java代码
//获取当前时间
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String curTime=format.format(new Date());
//输出内容到浏览器(下面两个效果是一样的)
response.getWriter().write(curTime);
//out.write(curTime);
%>
</body>
</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指令

 <%@ 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

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'script.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<!--jsp表达式-->
<%
//变量
String name="jack";
int a=10;
int b=20;
//System.out.println(name);
%>
<%=name %>
<br/>
<%=(a-b) %>
<br/> <!--jsp脚本-->
<%
//生成随机数
Random ran=new Random();
float num=ran.nextFloat();
%>
<%=num %>
<hr/> <!--练习:使用脚本和html代码显示99乘法表-->
<%
String[] color={"red","green","blue"};
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
out.print("<span style='color:"+color[i%3]+"'>"+j+"*"+i+"="+(i*j)+"&nbsp</span>");
%>
<%=i%> x <%=j%>=<%=(i*j)%>&nbsp; <!-- 这行代码和上面的out.print();是一样的,都是向页面输出 -->
<%
}
%>
<br/>
<%
}
%> <!--html的注释方式-->
<%--jsp的注释--%>
<%!
//变量
String name="rose"; /*
jsp声明中不能重复定义翻译一些方法
public void _jspInit() {}
*/
%>
</body>
</html>

附录2

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" buffer="1kb" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
//方法1:顶部设置缓冲区的大小为1Kb(buffer="1kb")
for(int i=1;i<=500;i++){
out.write("123");
}
//查看缓冲区大小
System.out.println("当前缓冲区的大小:"+out.getBufferSize());
//查看缓冲区剩余大小
System.out.println("剩余缓冲区大小:"+out.getRemaining());
//方法2:刷新缓存
//out.flush(); response.getWriter().write("a"); //解析:方法1或2,都能让123先输出来,最后输出a;如果两者都去掉,那么先输出a,后输出123.
%>
</body>
</html>

附录3

web.xml 配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<error-page>
<error-code>500</error-code>
<location>/common/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/common/404.html</location>
</error-page>
</web-app>

用户当前访问的页面

 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'page.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<%
String name=null;
name.charAt(1); //这里是一个错误; charAt:这是一个字符串,括号里填1(整形)的话 就错了
%>
</body>
</html>

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

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

原创作者:DSHORE

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

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

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

最新文章

  1. 关系数据库—SQL学习笔记
  2. 一个Woker类,当id和name相同时,系统判断两个工人是相等的,打印工人对象时显示“工人:id和name”。
  3. (原创)在service中定时执行网络操作的几点说明
  4. POJ3177 &amp; 求边双联通分量
  5. jquery操作select(增加,删除,清空)
  6. WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码
  7. 设计模式:策略模式(Strategy)
  8. 从Unity学UE(一)之蓝图类的使用----制作一个可控灯光
  9. 《VIM-Adventures攻略》前言
  10. 冯如杯day1
  11. 自动化运维工具——ansible详解(一)
  12. MongoDb安装和快速入门
  13. 2019年3月2日-小雨.md
  14. OpenSSL 提取 pfx 数字证书公钥与私钥
  15. LeetCode解题录-1~50
  16. 【原创】Github团队协作之Pull请求
  17. wcf 发布到iis后报错
  18. 动态规划法(六)鸡蛋掉落问题(一)(egg dropping problem)
  19. innerHTML、innerText和outerHTML、outerText的区别
  20. book项目分析

热门文章

  1. linux内核分析 第五周 扒开系统调用的三层皮(下)
  2. python学习(十八)爬虫中加入cookie
  3. array_merge 优化调整
  4. Docker容器跨主机通信--overlay网络
  5. 「Vue」vue cli3中axios的基本用法
  6. 1082 线段树练习 3 &amp;&amp; 树状数组区间修改区间查询
  7. 使用SSH-Xsheel文件传输
  8. RACCommand
  9. bzoj千题计划142:bzoj3144: [Hnoi2013]切糕
  10. 【转】Ubuntu+apache绑定多个域名