抓取jsp网页源代码( Java排版标签是如何被发送到客户端的?(一))
优采云 发布时间: 2022-01-14 09:13抓取jsp网页源代码(
Java排版标签是如何被发送到客户端的?(一))
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.runtime;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
import javax.servlet.jsp.JspFactory;
import org.apache.jasper.compiler.Localizer;
/**
* This is the super class of all JSP-generated servlets.
*
* @author Anil K. Vijendran
*/
public abstract class HttpJspBase
extends HttpServlet
implements HttpJspPage
{
protected HttpJspBase() {
}
public final void init(ServletConfig config)
throws ServletException
{
super.init(config);
jspInit();
_jspInit();
}
public String getServletInfo() {
return Localizer.getMessage("jsp.engine.info");
}
public final void destroy() {
jspDestroy();
_jspDestroy();
}
/**
* Entry point into service.
*/
public final void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
_jspService(request, response);
}
public void jspInit() {
}
public void _jspInit() {
}
public void jspDestroy() {
}
protected void _jspDestroy() {
}
public abstract void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException;
}
HttpJspBase类继承了HttpServlet,所以HttpJspBase类是一个Servlet,而index_jsp类继承了HttpJspBase类,所以index_jsp类也是一个Servlet,所以当浏览器访问m6官方入口的index.jsp页面时,实际上就是在访问index_jsp servlet时,index_jsp servlet使用_jspService方法来处理请求。
2.2、Jsp页面中的html排版标签是怎么发给客户端的?
浏览器接收到的数据
First Jsp
Hello Jsp
它们都使用 _jspService 方法中的以下代码输出到浏览器:
out.write('\r');
out.write('\n');
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
out.write("\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\n");
out.write(" First Jsp\r\n");
out.write("\t\r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" \r\n");
out.write(" ");
out.print("Hello Jsp");
out.write("\r\n");
out.write(" \r\n");
out.write("\r\n");
用jsp写的java代码和html代码会被翻译成_jspService方法,用jsp写的java代码会被原封不动的翻译成java代码,比如直接翻译成out.print("Hello Jsp");,而HTML 代码会被翻译成 out.write("\r\n"); 的形式 并输出到浏览器。将jsp页面中编写的html排版标签以out.write("\r\n");的形式输出到浏览器,浏览器只有拿到html代码后才能解析并执行html代码。
2.3、Jsp页面中的java代码m6官方入口是如何执行的?
jsp中写的java代码会被翻译成_jspService方法。当执行_jspService方法处理请求时,会执行jsp中编写的java代码,所以jsp页面中的java代码是在调用_jspService方法处理请求时执行M6的官方入口。
2.4、Webm6 Mile官方门户在调用jsp的时候会提供一些java对象给jsp吗?
查看_jspService方法可以看到,Webm6 Mile的官方入口调用jsp时,会为Jsp提供以下8个java对象
PageContext pageContext;
HttpSession session;
ServletContext application;
ServletConfig config;
JspWriter out;
Object page = this;
HttpServletRequest request,
HttpServletResponse response
其中page对象、request和response已经实例化,其他5个没有实例化的对象按照如下方式实例化
pageContext = _jspxFactory.getPageContext(this, request, response,null, true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
这8个java对象可以直接在Jsp页面中使用,如下:
结果如下:
2.5、Jsp 最佳实践
jsp最佳实践是如何在开发中使用jsp技术。
不管是JSP还是Servlet,虽然都可以用来开发动态web资源。但是由于这两种技术各自的特点,在长期的软件实践中,人们逐渐将servlet作为web应用中的控制器组件,使用JSP技术作为数据展示模板。原因是程序的数据通常需要美化后输出:让jsp使用java代码生成动态数据,美化页面会导致难以维护。让servlet不仅生成数据,还要在其中嵌套HTML代码美化数据,也会导致程序可读性差,难以维护。所以,最好的办法就是根据这两种技术的特点,让他们自己负责。
2.6、Tomcatm6官方入口执行流程
第一次执行:
客户端通过电脑连接到m6 Mile的官方入口。因为请求是动态的,所以所有请求都交给WEB容器处理。在容器中找到要执行的*.jsp 文件后,将*.jsp 文件转换为*.jsp 文件。java文件*.java文件编译后,形成*.class文件。最后要执行的 *.class 文件
第二次执行:
因为*.class文件已经存在,所以不需要转换和编译过程
修改后执行:
1.源文件已经修改,需要转换重新编译。
这是文章的介绍JSP动态网页开发原理详解。更多相关JSP动态网页开发原理,请搜索米乐官网往期文章或继续浏览下方相关话题。文章希望大家以后多多支持Milo官方!