JSP——Servlet老技术 新思想的表现

引入JSP

servlet向浏览器输出内容太繁琐:

需要写大量的html

修改后,需要重新编译,部署

想要调整布局,似比登天

 

JSP其实就是Servlet

   新建JSP

新建一个JSP,名为HelloWorld.jsp,只是输出一行HelloWorld

HelloWorld.jsp

<html>

  <head>

    <title>HelloWorld</title>

  </head>

 

  <body>

  <%

         out.println("HelloWorld");

  %>

  </body>

</html>

 

处理流程


 

JspServlet编译Jsp

tomcatweb.xml中可以得到确认:编译jsp之前,tomcat调用JspServlet

 <servlet-mapping>  

     <servlet-name>jsp</servlet-name>  

     <url-pattern>*.jsp</url-pattern>  

 </servlet-mapping>  

  

    <servlet>

       <servlet-name>jsp</servlet-name>

       <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>

    </servlet>

 

 

     tomcat下的work记录

进入E:\Environment\apache-tomcat-6.0.36\work\Catalina\localhost\DRP2.4\org\apache\jsp发现


 

其中HelloWorld_jsp.java中的_jspService()方法

publicvoid _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=GB18030");

      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\n");

      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("   <title>HelloWorld</title>\r\n");

      out.write("  </head>\r\n");

      out.write("  \r\n");

      out.write("  <body>\r\n");

      out.write("  ");

 

         out.println("HelloWorld");

 

      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);

        else log(t.getMessage(), t);

      }

    } finally {

     _jspxFactory.releasePageContext(_jspx_page_context);

    }

  }

 

发现什么没有?这个函数的参数为HttpServletRequest和HttpServletResponse,可能的异常为ServletException;可见此处是把jsp当做了Servlet处理。

 

     小结处理过程

  • 客户端首先发出Request请求,Tomcat接收到请求
  • 执行JspServlet的service()函数
  • 如果jsp第一次被请求,则编译jsp为Servlet;以后的执行顺序和普通的Servlet一样先执行new()
  • 再执行_jspService()
  • 形成html,返回给浏览器

 

 

扩展名,只是个标识

tomcatweb.xml中的*.jsp改成*.test

 <servlet-mapping>  

     <servlet-name>jsp</servlet-name>  

     <url-pattern>*.jsp</url-pattern>  

 </servlet-mapping>  

依然可以访问网站http://127.0.0.1:8080/JspTest/HelloWorld.test,得到相同结果

       

可见,java这种映射机制可以完美的进行解耦。通过<servlet-name>这个中间变量,将<url-pattern>和<servlet-class>形成映射。

  

同时,JSP只是Servlet的一个表现形式而已。

 

 

小结

虽然JSP是继Servlet之后出现的,却换汤没换药,加入了新的思想,解决了servlet向浏览器输出内容过于繁琐的难题。


  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值