JSP动态网页开拓道理详解(2)

/* * 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 * * * * 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,所以当欣赏器会见处事器上的index.jsp页面时,其实就是在会见index_jsp这个Servlet,index_jsp这个Servlet利用_jspService这个要领处理惩罚请求。

2.2、Jsp页面中的html排版标签是如何被发送到客户端的?

欣赏器吸收到的这些数据

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://localhost:8080/JavaWeb_Jsp_Study_20140603/" > <title>First Jsp</title> </head> <body> Hello Jsp </body> </html>

都是在_jspService要领中利用如下的代码输出给欣赏器的:

out.write('\r'); out.write('\n'); String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; out.write("\r\n"); out.write("\r\n"); out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n"); out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" <base href=https://www.jb51.net/article/\""); out.print(basePath); out.write("https://www.jb51.net/article/\">\r\n"); out.write(" \r\n"); out.write(" <title>First Jsp</title>\r\n"); out.write("\t\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" "); out.print("Hello Jsp"); out.write("\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n");

  在jsp中编写的java代码和html代码城市被翻译到_jspService要领中去,在jsp中编写的java代码会原封不动地翻译成java代码,如<%out.print("Hello Jsp");%>直接翻译成out.print("Hello Jsp");,而HTML代码则会翻译成利用out.write("<html标签>\r\n");的形式输出到欣赏器。在jsp页面中编写的html排版标签都是以out.write("<html标签>\r\n");的形式输出到欣赏器,欣赏器拿到html代码后才气够理会执行html代码。

2.3、Jsp页面中的java代码处事器是如何执行的?

  在jsp中编写的java代码会被翻译到_jspService要领中去,当执行_jspService要领处理惩罚请求时,就会执行在jsp编写的java代码了,所以Jsp页面中的java代码处事器是通过挪用_jspService要领处理惩罚请求时执行的。

2.4、Web处事器在挪用jsp时,会给jsp提供一些什么java工具?

  查察_jspService要领可以看到,Web处事器在挪用jsp时,会给Jsp提供如下的8个java工具

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsddjx.html