jsp转html静态页面(高并发web架构介绍)
选中eclipse里创建的jsp文件:
test.jsp的源代码:
“`jsp
<%@ page language=”java” contenttype=”text/html; charset=utf-8″
pageencoding=”utf-8″%>
<!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=utf-8″>
<title>w3cschool教程 2</title>
</head>
<body>
<%
out.println(“hello world 2!”);
%>
</body>
</html>
“`
点击run as server:
在我本机tomcat文件夹的路径work/catalina/localhost下面,自动生成了一个以jsp项目命名的文件夹jerryjsp:
打开这个自动被apache tomcat jasper组件生成的test_jsp.java文件:
/*
* generated by the jasper component of apache tomcat
* version: apache tomcat/9.0.29
* generated at: 2020-07-15 09:04:14 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.*;
public final class test_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;
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 {
if (!javax.servlet.dispatchertype.error.equals(request.getdispatchertype())) {
final java.lang.string _jspx_method = request.getmethod();
if ("options".equals(_jspx_method)) {
response.setheader("allow","get, head, post, options");
return;
}
if (!"get".equals(_jspx_method) && !"post".equals(_jspx_method) && !"head".equals(_jspx_method)) {
response.setheader("allow","get, head, post, options");
response.senderror(httpservletresponse.sc_method_not_allowed, "jsps only permit get, post or head. jasper also permits options");
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,
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("n");
out.write("<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">n");
out.write("<html>n");
out.write("<head>n");
out.write("<meta http-equiv="content-type" content="text/html; charset=utf-8">n");
out.write("<title>w3cschool教程 2</title>n");
out.write("</head>n");
out.write("<body>n");
out.println("hello world 2!");
out.write("n");
out.write("</body>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);
}
}
}
终端用户在浏览器里看到的静态页面的源代码:
是在这个java文件此处被拼接的:
关于对jsp文件的请求是如何被服务器的jsp容器转换成静态网页并发送给浏览器,可以参考w3c的jsp教程:
https://www.w3cschool.cn/jsp/jsp-architecture.html
网络服务器需要一个jsp引擎,也就是一个容器来处理jsp页面。容器负责截获对jsp页面的请求。本教程使用内嵌jsp容器的apache来支持jsp开发。
jsp容器与web服务器协同合作,为jsp的正常运行提供必要的运行环境和其他服务,并且能够正确识别专属于jsp网页的特殊元素。
下图显示了jsp容器和jsp文件在web应用中所处的位置。
# jsp处理
以下步骤表明了web服务器是如何使用jsp来创建网页的:
(1) 就像其他普通的网页一样,浏览器发送一个http请求给服务器。
(2) web服务器识别出这是一个对jsp网页的请求,并且将该请求传递给jsp引擎。通过使用url或者.jsp文件来完成。
(3) jsp引擎从磁盘中载入jsp文件,然后将它们转化为servlet。这种转化只是简单地将所有模板文本改用println()语句,并且将所有的jsp元素转化成java代码。
(4) jsp引擎将servlet编译成可执行类,并且将原始请求传递给servlet引擎。
(5) web服务器的某组件将会调用servlet引擎,然后载入并执行servlet类。在执行过程中,servlet产生html格式的输出并将其内嵌于http response中上交给web服务器。
(6) web服务器以静态html网页的形式将http response返回到您的浏览器中。
(7) 最终,web浏览器处理http response中动态产生的html网页,就好像在处理静态网页一样。
以上提及到的步骤可以用下图来表示:
一般情况下,jsp引擎会检查jsp文件对应的servlet是否已经存在,并且检查jsp文件的修改日期是否早于servlet。如果jsp文件的修改日期早于对应的servlet,那么容器就可以确定jsp文件没有被修改过并且servlet有效。这使得整个流程与其他脚本语言(比如php)相比要高效快捷一些。
总的来说,jsp网页就是用另一种方式来编写servlet而不用成为java编程高手。除了解释阶段外,jsp网页几乎可以被当成一个普通的servlet来对待。