了解JSP
了解jsp
1、什么是jsp?
jsp全名为java servlet pages,中文名为web服务器页面,它是在传统的网页html文件(*.htm,*.html)中插入java程序段和jsp标记,后缀名为(*.jsp),其根本是一个简化的servlet设计。
2、为什么要有jsp?
直接使用html文件(*.html)没有办法输出java当中的信息,使用servlet输出页面非常麻烦,于是出现了jsp,既能写html,又能写java代码。
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=iso-8859-1"> 7 <title>insert title here</title> 8 </head> 9 <body> 10 <% 11 string name="今夕何希"; 12 %> 13 <h1><%=name %></h1> 14 15 </body> 16 </html>
3、jsp的工作原理
(1)jsp本质是servlet
(2)jsp第一次访问时会被web容器翻译成servlet,在tomacat的work目录下(e:\program files\java\tomcat\apache-tomcat-7.0.90\work\catalina\localhost\jsp\org\apache\jsp),能够找到翻译成的.java文件:
1 /* 2 * generated by the jasper component of apache tomcat 3 * version: apache tomcat/7.0.90 4 * generated at: 2018-09-15 13:13:30 utc 5 * note: the last modified time of this file was set to 6 * the last modified time of the source file after 7 * generation to assist with modification tracking. 8 */ 9 package org.apache.jsp; 10 11 import javax.servlet.*; 12 import javax.servlet.http.*; 13 import javax.servlet.jsp.*; 14 15 public final class test_jsp extends org.apache.jasper.runtime.httpjspbase 16 implements org.apache.jasper.runtime.jspsourcedependent { 17 18 private static final javax.servlet.jsp.jspfactory _jspxfactory = 19 javax.servlet.jsp.jspfactory.getdefaultfactory(); 20 21 private static java.util.map<java.lang.string,java.lang.long> _jspx_dependants; 22 23 private volatile javax.el.expressionfactory _el_expressionfactory; 24 private volatile org.apache.tomcat.instancemanager _jsp_instancemanager; 25 26 public java.util.map<java.lang.string,java.lang.long> getdependants() { 27 return _jspx_dependants; 28 } 29 30 public javax.el.expressionfactory _jsp_getexpressionfactory() { 31 if (_el_expressionfactory == null) { 32 synchronized (this) { 33 if (_el_expressionfactory == null) { 34 _el_expressionfactory = _jspxfactory.getjspapplicationcontext(getservletconfig().getservletcontext()).getexpressionfactory(); 35 } 36 } 37 } 38 return _el_expressionfactory; 39 } 40 41 public org.apache.tomcat.instancemanager _jsp_getinstancemanager() { 42 if (_jsp_instancemanager == null) { 43 synchronized (this) { 44 if (_jsp_instancemanager == null) { 45 _jsp_instancemanager = org.apache.jasper.runtime.instancemanagerfactory.getinstancemanager(getservletconfig()); 46 } 47 } 48 } 49 return _jsp_instancemanager; 50 } 51 52 public void _jspinit() { 53 } 54 55 public void _jspdestroy() { 56 } 57 58 public void _jspservice(final javax.servlet.http.httpservletrequest request, final javax.servlet.http.httpservletresponse response) 59 throws java.io.ioexception, javax.servlet.servletexception { 60 61 final javax.servlet.jsp.pagecontext pagecontext; 62 javax.servlet.http.httpsession session = null; 63 final javax.servlet.servletcontext application; 64 final javax.servlet.servletconfig config; 65 javax.servlet.jsp.jspwriter out = null; 66 final java.lang.object page = this; 67 javax.servlet.jsp.jspwriter _jspx_out = null; 68 javax.servlet.jsp.pagecontext _jspx_page_context = null; 69 70 71 try { 72 response.setcontenttype("text/html; charset=utf-8"); 73 pagecontext = _jspxfactory.getpagecontext(this, request, response, 74 null, true, 8192, true); 75 _jspx_page_context = pagecontext; 76 application = pagecontext.getservletcontext(); 77 config = pagecontext.getservletconfig(); 78 session = pagecontext.getsession(); 79 out = pagecontext.getout(); 80 _jspx_out = out; 81 82 out.write("\r\n"); 83 out.write("<!doctype html public \"-//w3c//dtd html 4.01 transitional//en\" \"http://www.w3.org/tr/html4/loose.dtd\">\r\n"); 84 out.write("<html>\r\n"); 85 out.write("<head>\r\n"); 86 out.write("<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\r\n"); 87 out.write("<title>insert title here</title>\r\n"); 88 out.write("</head>\r\n"); 89 out.write("<body>\r\n"); 90 out.write("\t"); 91 92 string name="今夕何希"; 93 94 out.write('\r'); 95 out.write('\n'); 96 out.write(' '); 97 out.print(name ); 98 out.write("\r\n"); 99 out.write("</body>\r\n"); 100 out.write("</html>"); 101 } catch (java.lang.throwable t) { 102 if (!(t instanceof javax.servlet.jsp.skippageexception)){ 103 out = _jspx_out; 104 if (out != null && out.getbuffersize() != 0) 105 try { 106 if (response.iscommitted()) { 107 out.flush(); 108 } else { 109 out.clearbuffer(); 110 } 111 } catch (java.io.ioexception e) {} 112 if (_jspx_page_context != null) _jspx_page_context.handlepageexception(t); 113 else throw new servletexception(t); 114 } 115 } finally { 116 _jspxfactory.releasepagecontext(_jspx_page_context); 117 } 118 } 119 }
下一次访问时,会看一下里面的内容有没有发生变化,如果变化了,重新编译加载。
4、jsp组成部分
静态数据,如html;jsp指令,如include指令;jsp标签动作;用户自定义标签
- jsp脚本元素和变量
在jsp当中写java代码:
<%java代码> 内部的java代码翻译到service方法的内部
<%=java变量或者表达式> 会被翻译成service方法内部的out.print();
<%!java代码 > 会被翻译成selvlet的成员的内容
在jsp当中写注释:
html:<!-- 注释内容--> 可见范围 jsp源码,翻译后的servlet,页面
java://单行注释; /*多行注释*/ 可见范围 jsp源码,翻译后的servlet,页面看不到
jsp: <%注释内容%> 可见范围 jsp源码
- jsp指令
什么是指令?jsp指令用于设置整个jsp页面的相关信息,以及用于jsp页面与其他容器间的通信。
有哪些指令?
page (1) 用于设定整个jsp页面的属性和相关功能
(2)page指令的主要属性
(3)多个属性间用空格隔开
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
include (1) 表示在jsp编译时插入一个包含文件或代码的文件
(2)include指令所包含的文件名不能是一个url,只能是静态的文件名
(3)静态包含
taglib (1)声明jsp使用了哪些标签库
(2)jsp标准标签库,第三方标签库,自定义标签库
- 标签动作 (1)页面包含(动态包含)
-
- (2)请求转发:
<jsp:forward page="要转发的资源"></jsp:forward>
- 随式对象
jsp被翻译成servlet之后,service方法中有9个对象定义并初始化完毕,开发人员可以直接调用它们而不用显式地声明它们再调用。
out对象:
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=iso-8859-1"> 7 <title>insert title here</title> 8 </head> 9 <body> 10 aaa 11 <%="bbb" %> 12 <% 13 out.write("ccc"); 14 response.getwriter().write("ddd"); 15 %> 16 17 </body> 18 </html>
输出为:
只有response能够响应浏览器,因此out缓冲区(默认8kb)的内容会被拼接到response缓冲区之后。
pagecontext对象:可以获得其他8大隐式对象,可以向指定的其他域中存取数据
上一篇: 很经典千万不要想不开
下一篇: 囧啊,坑爹的水平那是一流的