理解JSP
2016-02-16 21:57
513 查看
1. JSP本质
JSP的本质是Servlet,当用户向指定Servlet发送请求时,Servlet利用输出流动态生成HTML页面,包括每一个静态HTML标签和所有在HTML页面中出现的内容。因此,当用户写了一个JSP的页面后,JSP页面由系统编译成Servlet,Servlet再负责响应用户。也是说,JSP其实也是Servlet的一种简化,使用JSP时,其实还是使用Servlet,因为Web应用中每个JSP页面都会由Servlet容器生成对应的Servlet。对Tomcat而言,JSP页面生成的Servlet放在work路径对应的Web应用下。2. JSP对应的java类
(1).编写一个helloWorldTag.jsp页面<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" errorPage=""%> <%@ taglib uri="http://www.linjw.org/mytaglib" prefix="mytag"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>自定义标签示范</title> <meta name="website" content="http://www.linjw.org" /> </head> <body bgcolor="#ffffc0"> <mytag:helloWorld/><br/> </body> </html>(2).编译后的helloWorld_jspTag.java
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class helloWorldTag_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent, org.apache.jasper.runtime.JspSourceImports { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; static { _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1); _jspx_dependants.put("/WEB-INF/mytaglib.tld", Long.valueOf(1451719668194L)); } private static final java.util.Set<java.lang.String> _jspx_imports_packages; private static final java.util.Set<java.lang.String> _jspx_imports_classes; static { _jspx_imports_packages = new java.util.HashSet<>(); _jspx_imports_packages.add("javax.servlet"); _jspx_imports_packages.add("javax.servlet.http"); _jspx_imports_packages.add("javax.servlet.jsp"); _jspx_imports_classes = null; } private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public java.util.Set<java.lang.String> getPackageImports() { return _jspx_imports_packages; } public java.util.Set<java.lang.String> getClassImports() { return _jspx_imports_classes; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { } 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 { final java.lang.String _jspx_method = request.getMethod(); if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); return; } 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, "", 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("<!--导入标签库,指定mytag前缀的标签,由URI为http://www.linjw.org/mytaglib的标签库处理-->\r\n"); out.write("\r\n"); out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n"); out.write("<title>自定义标签示范</title>\r\n"); out.write("<meta name=\"website\" content=\"http://www.linjw.org\" />\r\n"); out.write("</head>\r\n"); out.write("<body bgcolor=\"#ffffc0\">\r\n"); out.write("<!--使用标签其中mytag是标签前缀,根据taglib的编译指令,mytaglib前缀将由URI为。。的标签库处理helloWorld-->\r\n"); if (_jspx_meth_mytag_005fhelloWorld_005f0(_jspx_page_context)) return; out.write("<br/>\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { 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); } } private boolean _jspx_meth_mytag_005fhelloWorld_005f0(javax.servlet.jsp.PageContext _jspx_page_context) throws java.lang.Throwable { javax.servlet.jsp.PageContext pageContext = _jspx_page_context; javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut(); // mytag:helloWorld com.linjw.demo.HelloWorldTag _jspx_th_mytag_005fhelloWorld_005f0 = (new com.linjw.demo.HelloWorldTag()); _jsp_getInstanceManager().newInstance(_jspx_th_mytag_005fhelloWorld_005f0); _jspx_th_mytag_005fhelloWorld_005f0.setJspContext(_jspx_page_context); _jspx_th_mytag_005fhelloWorld_005f0.doTag(); _jsp_getInstanceManager().destroyInstance(_jspx_th_mytag_005fhelloWorld_005f0); return false; } }
(3). 理解生成的java类
其实从生成的java类中,看似是比较多,也是比较复杂的。但是对我们的理解,主要是看下面几个方法:init():初始化JSP/Servlet的方法
destroy():销毁JSP/Servlet之前的方法
service():对用户请求生成响应的方法
(4). 理解JSP页面的工作原理
1).JSP文件必须在JSP服务器内运行。2).JSP文件必做生成Servlet才能执行。
3).每个JSP页面的第一个访问者速度很慢,因为必须等待JSP编译成Servlet。
4).JSP页面的访问者无须安装任何客户端,甚至不需要可以运行Java的运行环境,因为JSP页面输送到客户的是标准HTML页面。
五、总结
JSP并不是脱离了Java的面向对象的特征,实际上它依然是一个特殊的Java类。同时,理解JSP还要考虑到它是由静态部分(HTML的标签)和动态部分(就是通过<%%>将java代码嵌入到页面中)组成。最后,要知道JSP Server在接收Client请求时,是先编译JSP文件成为对应的Servlet,然后将Servlet生成标准的HTML响应给用户。相关文章推荐
- "0" != 0
- Javascript自学-2
- 能说明你的Javascript技术很烂的五个原因
- JavaScript中的消息框的分类
- BAT及各大互联网公司2014前端笔试面试题:JavaScript篇
- JavaScript中的数据类型
- JSF 2 outputText example
- Javascript模块等高级知识
- (转)常用的js设计模式
- js子窗口修改父窗口内容
- Js中的this指向问题
- javaScript一些基本知识
- JS中数组Array的用法示例用法详解
- js基础篇——变量
- 《JavaScript高级程序设计》读书笔记(二):引用类型
- 读取单条json数据代码
- 【 bzoj 1444 】 [Jsoi2009]有趣的游戏 - AC自动机+矩阵乘法
- javascript dom编程艺术阅读笔记
- js 代码的优化 ,完全是照搬,不过挺好使
- JavaScript权威指南__第三章