JSP 原理
程序员文章站
2022-03-24 23:45:40
1、jsp的由来
vcwxviamizqzoybku1agserhqsjwumhukaosslnq1tdi/dbwsmf2ysc9xbg+o7o8l3a+cjxwpqghoagh8sa8js4uli...
1、jsp的由来
vcwxviamizqzoybku1agserhqsjwumhukaosslnq1tdi/dbwsmf2ysc9xbg+o7o8l3a+cjxwpqghoagh8sa8js4uliu+o7pkyxzhtprc68asts6jrnpd09q2qnllmh5ozpvkyxzhinpvvusjrle9t6jw0mtcubvqtmqyw7sjrnxiwo/d5r7nxny3xcqyw7sjuzxicj4kpc9wpgo8cd6hoaghofegpcu9icalpqo6smf2yscx7btvyr2jrnpd09rk5lp20rvm9bhtto/kvbvysetbv7xeveg5+6gjihjlc3bvbnnllmdldfdyaxrlcigplnbyaw50kckgt723qnbqxny5u9c0yrldtkos1ela78pmvs3e3lm70ltksso0o7s8l3a+cjxwpqghoagh8sa8jsegoa0gjt4go7rj+cp3o6ztw8c0tls9qmdgtcszydsxsetbv7rns8nusbe9t6ijrephdmegwodw0mtcubvqtmqyw7sjrnxiwo/d5r7nxny5u9c0yrldtkos0qrxotlitctkx6oswo/d5rxexnri3bk71nogx2pzcfnlcnzpy2uoksc3vbeo1q7e2qos1rg907g7slnq16q7r7rztcta4mzlspy6rkgjpc9wpgo8cd48yni+cjwvcd4kpggxpjohokptucdurcdtpc9omt4kpgjypgokpha+oaghoceww+bs0b6tsvvk9rn9o6xku1c1xlg+1srktdbkysfsu9bwznjk4tdoyr21xcbtzxj2bgv0iko6pc9wpgo8cd6hoaghofegtbhtw7unt8poytk7upzku1dss8pmyrgjrlvhz/igu2vydmxldcdi3cb3o6jv4sdvysdub21jyxsjqbeis/bh68fzo7s8l3a+cjxwpqghoagh8sdi57n71ek49kptunkzw+bkx7xa0ru0zrg7t8poyrvy1d/v4rj20rpd5rg7ums2r7n9yrgjrlf+zvhg97vhsnfku1agseds67pjic5qyxzhzss8/qostbhiu6os1ek49i5qyxzhil7nysfsu7j2c2vydmxldmdgo6ziu7rz1nmw0sauamf2ysdoxlz+seds67pjlmnsyxnzcidoxlz+oaps8s6qseds67vhuss30dk7tqjksbzko6zl+dlu0rpd5ttatdrsu7tosbu3w87ku/k4xlavuvoxu7fdzsrksbvhu6i30b3ps6s1xlfdzsrksbzko7s8l3a+cjxwpqghoagh8sc0tl2oumpa4lbuz/ojrnfuuvptyvnlcnzszxqgyn3g97x308pl/lxec2vydmljzsgpile9t6ijuzwvcd4kpha+oaghoahxilxatv60zsfrx/pnrnk7slnqyrgjrnaxvdo199pdc2vydmljzsgpile9t6ihozwvcd4kpha+pgjypgo8l3a+cjxomt40oalr6dakslnq1k3a7twvade+cjxicj4kcjxwpqghoahk18/io6zo0sphwltqtnk7upzozwxsby5qc3doxlz+o7o8l3a+cjxwpjxwcmugy2xhc3m9"brush:java;">姓名 | 年龄 | 性别 |
---|---|---|
张三 | 18 | 男 |
/* * generated by the jasper component of apache tomcat * version: apache tomcat/7.0.42 * generated at: 2015-12-27 10:03:19 utc * note: the last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.*; public final class hello_jsp extends org.apache.jasper.runtime.httpjspbase implements org.apache.jasper.runtime.jspsourcedependent { public void fun1() { //要特别注意,这是在中定义的方法,没有被放在service方法中 system.out.println("hello"); } private static final javax.servlet.jsp.jspfactory _jspxfactory = javax.servlet.jsp.jspfactory.getdefaultfactory(); private static java.util.map _jspx_dependants; private javax.el.expressionfactory _el_expressionfactory; private org.apache.tomcat.instancemanager _jsp_instancemanager; public java.util.map getdependants() { return _jspx_dependants; } public void _jspinit() { _el_expressionfactory = _jspxfactory.getjspapplicationcontext(getservletconfig().getservletcontext()).getexpressionfactory(); _jsp_instancemanager = org.apache.jasper.runtime.instancemanagerfactory.getinstancemanager(getservletconfig()); } public void _jspdestroy() { } public void _jspservice(final javax.servlet.http.httpservletrequest request, final javax.servlet.http.httpservletresponse response) throws java.io.ioexception, javax.servlet.servletexception { //这是_jspservice()方法 final javax.servlet.jsp.pagecontext pagecontext; //内置对象的初始化 javax.servlet.http.httpsession session = null; final javax.servlet.servletcontext application; final javax.servlet.servletconfig config; javax.servlet.jsp.jspwriter out = null; final java.lang.object page = this; javax.servlet.jsp.jspwriter _jspx_out = null; javax.servlet.jsp.pagecontext _jspx_page_context = null; try { response.setcontenttype("text/html;charset=utf-8"); pagecontext = _jspxfactory.getpagecontext(this, request, response, null, true, 8192, true); _jspx_page_context = pagecontext; application = pagecontext.getservletcontext(); config = pagecontext.getservletconfig(); session = pagecontext.getsession(); out = pagecontext.getout(); _jspx_out = out; out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); string s = request.getheader("user-agent"); //它的原身是string s = request.getheader("user-agent");,被直接拿过来了 out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); //这些都是html代码,底层被包装成和servlet 一样的实现方式 out.write(" \r\n"); out.write(" my jsp 'a.jsp' starting page\r\n"); out.write(" \r\n"); out.write("\t\r\n"); out.write("\t\r\n"); out.write("\t \r\n"); out.write("\t\r\n"); out.write("\t\r\n"); out.write("\t\r\n"); out.write("\r\n"); out.write(" \r\n"); out.write(" \r\n"); out.write(" \r\n"); out.write(" \r\n"); out.write("\t\r\n"); out.write("\t 姓名\r\n"); out.write("\t 年龄\r\n"); out.write("\t 性别\r\n"); out.write("\t\r\n"); for(int i = 0; i \r\n"); out.write("\t 张三\r\n"); out.write("\t 18\r\n"); out.write("\t 男\r\n"); out.write("\t\r\n"); } out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write("\r\n"); out.write('\r'); out.write('\n'); int a = 10; //这些是定义的变量,可以看到是放在了service方法中的 out.write("\r\n"); out.write("\r\n"); a++; out.write("\r\n"); out.write("\r\n"); out.print(a ); out.write("\r\n"); out.write("\r\n"); out.write(" \r\n"); out.write("\r\n"); } catch (java.lang.throwable t) { if (!(t instanceof javax.servlet.jsp.skippageexception)){ out = _jspx_out; if (out != null && out.getbuffersize() != 0) try { out.clearbuffer(); } catch (java.io.ioexception e) {} if (_jspx_page_context != null) _jspx_page_context.handlepageexception(t); else throw new servletexception(t); } } finally { _jspxfactory.releasepagecontext(_jspx_page_context); } } }得出结论,jsp中虽然能直接写出html代码,但是在底层依然是被包装成 servlet 实现方式的,所以更印证了jsp是特殊的servlet 。
其次,在 和 脚本中定义的java 代码都会放在jsp 的 _jspservice() 方法中(实际上就是servlet中的service 方法),而 脚本中定义的却会放到 hello_jsp 类的成员位置的,这一点很重要,因为jsp中鼎鼎大名的九大内置对象是在_jspservice() 方法中初始化的,只有在本方法中才能够使用内置对象,所以 脚本中是不能使用内置对象的(在实际开发中,本脚本很少用到)。
5、jsp和html、servlet 的比较
● servlet:
缺点:不适合设置html响应体,需要大量的response.getwriter().print("")
优点:动态资源,可以。
● html:
缺点:html是静态页面,不能包含动态信息
优点:不用为输出html标签而发愁
● jsp:
优点:在原有html的基础上添加java脚本,构成jsp页面。
6、jsp 和 servlet 的分工
在设计中,jsp和servlet 是相互配合使用的,其分工为:
● jsp:
作为请求发起页面,例如显示表单、超链接,并将请求发给 servlet ;
作为请求结束页面,例如显示数据。
● servlet:
作为请求中处理数据的环节。