• JSP基础


    JSP的出现

      上篇文章中提到了用Servlet开发,渲染client页面是须要一句一句输出的,这样,编写和改动HTML都会非常不方便。也就是说Servlet写“业务层”非常合适。可是写“表现层”非常繁琐。

      于是写“表现层”非常方便的JSP就诞生了。这里须要指明,JSP并没有添加不论什么本质上不能用Servlet实现的功能。可是,在JSP中编写静态HTML更加方便,不必再用println语句来输出每一行HTML代码。更重要的是,它将业务层与表现层分离开来,使得编码更方便和易维护。

     

    JSP与Servlet

      书写方便仅仅是SUN为我们编码所提供的一种服务。有了JSP也不能忘了Servlet,由于后者正是JSP的本质。JSP能够说是对Servlet更高层次的一种包装。请求JSP时。经过转化,褪去包装后,就能够看到它的真是面目还是Servlet。渲染页面还是一行一行的输出HTML代码。

     

    JSP的使用

      如今一般我们写的JSP就是将JavaServlet)代码嵌入到了HTML页中,也就是将“业务层”与“表现层”混到了一起,与ASP的代码页很相像,如以下一个ASP代码页:

    <html>
    <head>
    <%Response.Write "<TITLE>"&my_name&"</TITLE>"%>
    </head>
    <%
    if my_domains<>"" then
    if Request.ServerVariables("SERVER_NAME")<>my_url Then response.redirect "url.asp"
    End if
    
    If rikeenet("a")<>"" And IsNumeric(rikeenet("a")) Then 
    Response.redirect "shop/1/myshop.asp?shopid="&rikeenet("a")&""
    Response.End
    End If
    
    %>
    <body class="<%= PageStyle %>">
    
    	<!--#include file="FHeadAll.asp"-->
    	<div class="HackBox"></div>
    	<div id="Content" class="L250">
    	<a name="main"></a>
    	<%
    	ShowIndexNewProduct(9)
    	shopuser()%>
    	</div>
    </body>
    </html>

      我们能够看到。server代码与HTML代码合在一起。非常混乱。JSP也非常类似。

    也就是说由Java代码与HTML代码合在一起的JSP是非常混乱的。

      ASP.NET的出现弥补了ASP的这个缺点。它将显示(aspx)与业务(aspx.cs)全然分离。当然依据SUN自己的推荐,JSP中应该只存放与“显示层”有关的东西,也就是说,只放输出HTML网页的部分。而全部的数据计算,数据分析,数据库连接处理等,统统是属于“业务层”,应该放在Java BEANS中。通过JSP调用Java BEANS。实现两层的整合。

      由上可知JSP的本质就是Servlet,运行时先由JSP引擎将它转化为Servlet,然后再编译为.class文件。并且JSP类似Servlet也是一次编译多次运行的。在第一次请求时才须要将它进行转化和编译。

     

    HelloWorld----实例:

    JSP页----HelloWorld.jsp

    <html>
    	<head>
    		<title> HelloWorld </title>
    	</head>
    	<body>
    		<%
    			out.println("HelloWorld");
    		%>
    	</body>
    </html>

    JSP引擎转化后的页----HelloWorld_jsp.java

     

    package org.apache.jsp;
    
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    
    public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {
    
      private static java.util.List _jspx_dependants;
    
      public Object getDependants() {
        return _jspx_dependants;
      }
    
      public void _jspService(HttpServletRequest request, HttpServletResponse response)
            throws java.io.IOException, ServletException {
    
        JspFactory _jspxFactory = null;
        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 {
          _jspxFactory = JspFactory.getDefaultFactory();
          response.setContentType("text/html");
          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("<html>
    ");
          out.write("	<head>
    ");
          out.write("		<title> HelloWorld </title>
    ");
          out.write("	</head>
    ");
          out.write("	<body>
    ");
          out.write("		");
    
    			out.println("HelloWorld");
    		
          out.write("
    ");
          out.write("	</body>
    ");
          out.write("</html>");
        } catch (Throwable t) {
          if (!(t instanceof SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              out.clearBuffer();
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
          }
        } finally {
          if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }
    

    运行步骤:

     

     

    总结

      感觉JSP是为了方便编码与维护而对Servlet的一种高层次的包装,它由HTML代码和嵌入当中的Java代码所组成,它的请求响应(server解析)过程与ASP/.NET大同小异,可參考例如以下文章:

    http://blog.csdn.net/wang379275614/article/details/9771659

  • 相关阅读:
    zeromq学习记录(五)vc下多线程
    zeromq学习记录(七)订阅发布消息封装
    Mozilla研究—从输入URL到显示内容的基本过程
    Mozilla研究—深入理解mozilla所需的背景知识
    Mozilla研究—组件加载机制
    Mozilla研究—传输协议
    Mozilla研究—mozilla中的设计亮点
    GTK+主循环(main loop)的工作原理
    Mozilla研究—mozilla能为我们做什么
    Mozilla研究—组件的创建过程
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6912571.html
Copyright © 2020-2023  润新知