JSP学习------为何jsp是servlet?

Posted 晓锋残月

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP学习------为何jsp是servlet?相关的知识,希望对你有一定的参考价值。

1.jsp基本介绍

(1)jsp解释

JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术。JSP/Servlet规范。JSP实际上就是Servlet


(2)jsp的执行过程

通常jsp要经历“翻译--------编译”过程,翻译将jsp页面变成.java文件,编译将.java文件变成.class文件

图解为下面所示:



下面以一个单的例子测试:

<%@page import="java.util.Date"%>
<%@ 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>Insert title here</title>
</head>
<body>
	<%
		Date date=new Date();
		out.print(date.toLocaleString());
	 %>
</body>
</html>
部署成功后,在下面路径:D:\\javatool\\apache-tomcat-7.0.75\\work\\Catalina\\localhost\\ee12_jsp\\org\\apache\\jsp得到

(3)对翻译的jsp页面解读

在_1_jsp.java页面中有下面代码(只保留了部分代码)

public final class _1_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent 

  

  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 

      out.write("\\r\\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.write("\\t");

		Date date=new Date();
		out.print(date.toLocaleString());
	 
      out.write("\\r\\n");
      out.write("</body>\\r\\n");
      out.write("</html>");
     
  

①可以看 _1_jsp extends org.apache.jasper.runtime.HttpJspBase, 表示其继承了HttpJspBase类,往下看HttpJspBase的源码

public abstract class HttpJspBase extends HttpServlet implements HttpJspPage 
    
    private static final long serialVersionUID = 1L;

    protected HttpJspBase() 
    

    @Override
    public final void init(ServletConfig config) 
        throws ServletException 
    
        super.init(config);
        jspInit();
        _jspInit();
    
    
    @Override
    public String getServletInfo() 
        return Localizer.getMessage("jsp.engine.info");
    

    @Override
    public final void destroy() 
        jspDestroy();
        _jspDestroy();
    

    /**
     * Entry point into service.
     */
    @Override
    public final void service(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
    
        _jspService(request, response);
    
    
    @Override
    public void jspInit() 
    

    public void _jspInit() 
    

    @Override
    public void jspDestroy() 
    

    protected void _jspDestroy() 
    

    @Override
    public abstract void _jspService(HttpServletRequest request, 
                                     HttpServletResponse response) 
        throws ServletException, IOException;

综上可以得到 _1_jsp.java继承了HttpServlet,故可以说JSP就是Servlet


②jsp页面的html元素最终以out.write("<html>");的形式输出

③在_1_jsp.java的代码中,同样有servlet有的init、serve、destroy等方法。


2.总结:

以上便是对于jsp的初步学习,如果有错误,请告知,谢谢!





以上是关于JSP学习------为何jsp是servlet?的主要内容,如果未能解决你的问题,请参考以下文章

jsp和servlet学习总结

Servlet和JSP学习总结

JSP 学习笔记

JSP基础学习

JSP学习 —— 开篇:JSP,servlet容器,Tomcat,servlet容器之间的关系

jsp/servlet学习笔记(核心编程)servlet部分