Web学习-jsp实现servlet过程赏析
Jsp在某种程度上就是一种servelt。
来看看tomcat容器如何将jsp页面翻译成一个”servlet”。
一、
F:\apache-tomcat-6.0.51\work\Catalina\localhost\day0416\org\apache\jsp
这个路径是容器翻译后的java代码和字节码文件的存放路径,当然必须在访问了这个jsp页面后才会生成,大家都知道jsp页面在第一次被访问的时候会被翻译成“类servlet”文件。
但是,如果你写一个servlet,然后部署访问后,这个路径是不会有相应字节码文件的。
二、
首先看引入的包
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
引入了servlet相关的几个包,说明这个“类servlet”文件和原始的servlet类有很大的联系。但是又有些细微差别。
public final class jsp1_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent{}
这个地方继承了apache的一个什么类,还实现了个接口,都是和jsp运行环境有关的。
三、与原生servlet的几个异同点
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
原生的servlet中有初始化方法Init(),在翻译后的“类servlet”文件,变成了_jspInit()方法。
类似的还有_jspDestroy()等方法。实现的与原生servlet类似的功能。
四、_jspService()方法
与原生servlet的类似,_jspService()也传入了HttpServletRequest request, HttpServletResponse response两个对象,这样就能与容器良好的适配了。
异常也同样的跑出了ServletException,如果有输出还会抛出IOException。
(测试了一下,如果jsp里是一个空页面,任何内容都没有,仍然会抛出IO异常,并且response.setContentType(“text/html”),默认还是个页面,然后后面的的异常处理也是完整的,不会因为你内容少而减少处理)
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
在这里声明了8个对象,其实是10个,包括传进来的请求对象、响应对象。
其中很有意思的是JspWriter _jspx_out = null; JspWriter其实就相当于PrintWriter对象,而且它还会和OutputStream对象争宠,两者在一个页面中总能用一个。
五、响应的正文内容
response.setContentType(“text/html; charset=UTF-8”)会将jsp页面中使用page指令的相关设置写进了,最重要的就是这个charset编码了。
后面异常处理语句写的非常经典,简直是异常处理的典范,值得多读几遍。
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.print("</br>这里是jsp1");
out.flush();
} catch (Throwable t) {
if (!(t instanceof 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 log(t.getMessage(), t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
下面是源代码:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class jsp1_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
private static java.util.List _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}
public void _jspDestroy() {
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
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');
out.write('\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=UTF-8\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.print("</br>这里是jsp1,引入了jsp2.jsp");
out.flush();
out.write("\r\n");
out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof 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 log(t.getMessage(), t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
Web学习-jsp实现servlet过程赏析的更多相关文章
- java web 学习总结之 Servlet/JSP 编码问题
Servlet和JSP编码问题 字节流: 1.得到OutputStream 字节流 OutputStream os = response.getOutputStream(); 用默认编码输出数据 ...
- java web学习总结(五) -------------------servlet开发(一)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- java web学习总结(六) -------------------servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- java web 学习六(servlet开发2)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- java web 学习五(servlet开发1)
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- 如何在java代码中调用一个web项目jsp或者servlet
有时候需要调用一个web项目的jsp或者servlet,但是执行内部的代码,并不是打开jsp,例如需要在一段java代码中清除一个web项目中的缓存,那么可以把清除缓存的代码放在该web项目的一个se ...
- 【WEB】jsp向servlet传参中文乱码问题解决
传参方式:POST.GET.link方式 servlet向jsp传中文参数msg if(username.equals("") || password.euqals("& ...
- [Java Web学习]JSP中uri="http://java.sun.com/jsp/jstl/core"报红错误
在官网下载jstl.jar和standard.jar,问题解决.
- JSP和Servlet那些事儿系列--HTTPS
原文:http://qingkangxu.iteye.com/blog/1614053 <JSP和Servlet那些事儿 >系列文章旨在阐述Servlet(Struts和Spring的MV ...
随机推荐
- 【ztree】ztree例子
<script language="javascript" type="text/javascript" src="js/jquery.js&q ...
- HTML5 开发APP( 环境配置)
上一篇我写了关于新建项目,这一篇说一下配置环境我们新建一个移动app后,会发现一个mainifest.json文件,开发app所要配置的环境就在这个文件里 点击打开文件后会有配置界面,在配置界面的下方 ...
- 构建 MariaDB Galera Cluster 分布式数据库集群(一)
MariaDB Galera Cluster 介绍 简介 MariaDB集群是MariaDB同步多主机集群,仅支持XtraDB(详见本文结尾注释)/InnoDB存储引擎(虽然有对MyISAM实验支持 ...
- mysql导出指定字段或指定数据到文件中
使用mysqldump把mysql数据库的数据导出到文件中还是挺方便的:比如说要导出baijunyao数据库: // mysqldump -u用户名 -p 数据库名 [表名]> 导出的文件名 m ...
- phantomjs submit click
phantomjs 一般提交时都会用submit,前提条件就是网页中有form表单: 在网页中无form表单的情况下,用submit()会报错:selenium.common.exceptions.N ...
- Linux(9)后台运行python程序并输出到日志文件
后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...
- wamp+python+CGI+wingIDE
一.环境配置情况 windows7 32位 wamp2.5 python3.5.2 wingIDE5.1.12 上述软件下载安装按照自己的意愿行事. 二.配置 wamp支持cgi和python: ...
- mongodb分片部署
Mongodb 分片部署 配置mongodb集群,比如 在3个server上配置 3 shard的Mongodb集群: 架构: 1.每片数据需要3个mongod server,2个为主从数据节点:1个 ...
- 百度之星2017初赛A轮 1001 小C的倍数问题
小C的倍数问题 Accepts: 1990 Submissions: 4931 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Jmeter中引入class文件的方法
------------------------------------------------------------------------------