欢迎来到站长教程网!

JSP编程

当前位置:主页 > 网络编程 > JSP编程 >

JSP动态网页开发原理详解

时间:2020-09-17|栏目:JSP编程|点击:

一、什么是JSP?

     JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术。
  JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比html而言,html只能为用户提供静态数据,而Jsp技术允许在页面中嵌套java代码,为用户提供动态数据。

二、JSP原理

2.1、Web服务器是如何调用并执行一个jsp页面的?

  浏览器向服务器发请求,不管访问的是什么资源,其实都是在访问Servlet,所以当访问一个jsp页面时,其实也是在访问一个Servlet,服务器在执行jsp的时候,首先把jsp翻译成一个Servlet,所以我们访问jsp时,其实不是在访问jsp,而是在访问jsp翻译过后的那个Servlet,例如下面的代码:

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>" rel="external nofollow" >

  <title>First Jsp</title>

 </head>

 <body>
  <%
    out.print("Hello Jsp");
  %>
 </body>
</html>

  当我们通过浏览器访问index.jsp时,服务器首先将index.jsp翻译成一个index_jsp.class,在Tomcat服务器的work\Catalina\localhost\项目名\org\apache\jsp目录下可以看到index_jsp.class的源代码文件index_jsp.java,index_jsp.java的代码如下:

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;

public final class index_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');

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

   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=\"");
   out.print(basePath);
   out.write("\">\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");
  } 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);
   }
  } finally {
   _jspxFactory.releasePageContext(_jspx_page_context);
  }
 }
}

  我们可以看到,index_jsp这个类是继承 org.apache.jasper.runtime.HttpJspBase这个类的,通过查看Tomcat服务器的源代码,可以知道在apache-tomcat-6.0.20-src\java\org\apache\jasper\runtime目录下存HttpJspBase这个类的源代码文件,如下图所示: 

JSP动态网页开发原理详解

上一篇:Javaweb工程运行报错HTTP Status 404解决办法

栏    目:JSP编程

下一篇:如何在jsp界面中插入图片

本文标题:JSP动态网页开发原理详解

本文地址:www.dtcnnet.com/JSP/31915.html

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:584415406 | 邮箱:584415406#qq.com(#换成@)

Copyright © 2015-2020 小白站长网 版权所有 苏ICP备20040415号