jsp详解

jsp与java

jsp详解,前段时间想起好长时间没有用jsp了,都忘了,于是整理了一些jsp的东西。1.什么是JSP,为什么要用JSP?JSP即Java Server Page,Java服务端网页,在HTML页面中编写Java代码的页面; 所有的页面内容都可以在Servlet中通过out.print输出,之所以用到JSP,是为了简单和整洁,而实际上JSP本身就是一个Servlet。  在JSP书写方式中,动态的内容使用Java编写,静态的内容使用HTML来编写2.JSP原理2.1 JSP本身就是一个Servlet每次JSP页面在第一次没访问时,JSP引擎将它翻译成一个Servlet源程序,接着再把这个Servlet源程序编译成 Servlet 的 class 类文件,然后再由WEB容器(Servlet引擎)像调用普通Servlet程序一样的方式来装载和解释执行这个由JSP页面翻译成的Servlet程序。 示例如下:

Hello.java:

[java] view plain copy
  1. package roadArchitectWeb.Test;  
  2.   
  3. public class Hello {  
  4. private String name;  
  5. public Hello(String name) {  
  6.     super();  
  7.     this.name = name;  
  8. }  
  9. @Override  
  10. public String toString() {  
  11.     return "Hello [name=" + name + "]";  
  12. }  
  13. }  

test.jsp:

[html] view plain copy
  1. <%@page import="roadArchitectWeb.Test.Hello"%>  
  2. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  3.     pageEncoding="UTF-8"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <%  
  12.     Hello hello = new Hello("zhangjie");  
  13.     out.println(hello);  
  14. %>  
  15. </body>  
  16. </html>  

然后再浏览器中运行的结果是:

[html] view plain copy
  1. Hello [name=zhangjie]  

2.2 JSP对应的class文件

找到test.jsp对应的class文件,它在eclipse安装目录下,我的机器中是下面的目录:

D:\java\eclipseee\eclipse-jee-mars-R-win32-x86_64\eclipse\codes\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\roadArchitectWeb\org\apache\jsp

然后发现两个文件:

test_jsp.class

test_jsp.java

打开test_jsp.java,如下:

[java] view plain copy
  1. /* 
  2.  * Generated by the Jasper component of Apache Tomcat 
  3.  * Version: Apache Tomcat/7.0.64 
  4.  * Generated at: 2016-05-16 07:09:05 UTC 
  5.  * Note: The last modified time of this file was set to 
  6.  *       the last modified time of the source file after 
  7.  *       generation to assist with modification tracking. 
  8.  */  
  9. package org.apache.jsp;  
  10.   
  11. import javax.servlet.*;  
  12. import javax.servlet.http.*;  
  13. import javax.servlet.jsp.*;  
  14. import roadArchitectWeb.Test.Hello;  
  15.   
  16. public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase  
  17.     implements org.apache.jasper.runtime.JspSourceDependent {  
  18.   
  19.   private static final javax.servlet.jsp.JspFactory _jspxFactory =  
  20.           javax.servlet.jsp.JspFactory.getDefaultFactory();  
  21.   
  22.   private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;  
  23.   
  24.   private javax.el.ExpressionFactory _el_expressionfactory;  
  25.   private org.apache.tomcat.InstanceManager _jsp_instancemanager;  
  26.   
  27.   public java.util.Map<java.lang.String,java.lang.Long> getDependants() {  
  28.     return _jspx_dependants;  
  29.   }  
  30.   
  31.   public void _jspInit() {  
  32.     _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();  
  33.     _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());  
  34.   }  
  35.   
  36.   public void _jspDestroy() {  
  37.   }  
  38.   
  39.   public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)  
  40.         throws java.io.IOException, javax.servlet.ServletException {  
  41.   
  42.     final javax.servlet.jsp.PageContext pageContext;  
  43.     javax.servlet.http.HttpSession session = null;  
  44.     final javax.servlet.ServletContext application;  
  45.     final javax.servlet.ServletConfig config;  
  46.     javax.servlet.jsp.JspWriter out = null;  
  47.     final java.lang.Object page = this;  
  48.     javax.servlet.jsp.JspWriter _jspx_out = null;  
  49.     javax.servlet.jsp.PageContext _jspx_page_context = null;  
  50.   
  51.   
  52.     try {  
  53.       response.setContentType("text/html; charset=UTF-8");  
  54.       pageContext = _jspxFactory.getPageContext(this, request, response,  
  55.                 nulltrue8192true);  
  56.       _jspx_page_context = pageContext;  
  57.       application = pageContext.getServletContext();  
  58.       config = pageContext.getServletConfig();  
  59.       session = pageContext.getSession();  
  60.       out = pageContext.getOut();  
  61.       _jspx_out = out;  
  62.   
  63.       out.write("\r\n");  
  64.       out.write("\r\n");  
  65.       out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");  
  66.       out.write("<html>\r\n");  
  67.       out.write("<head>\r\n");  
  68.       out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");  
  69.       out.write("<title>Insert title here</title>\r\n");  
  70.       out.write("</head>\r\n");  
  71.       out.write("<body>\r\n");  
  72.   
  73.     Hello hello = new Hello("zhangjie");  
  74.     out.println(hello);  
  75.   
  76.       out.write("\r\n");  
  77.       out.write("</body>\r\n");  
  78.       out.write("</html>");  
  79.     } catch (java.lang.Throwable t) {  
  80.       if (!(t instanceof javax.servlet.jsp.SkipPageException)){  
  81.         out = _jspx_out;  
  82.         if (out != null && out.getBufferSize() != 0)  
  83.           try {  
  84.             if (response.isCommitted()) {  
  85.               out.flush();  
  86.             } else {  
  87.               out.clearBuffer();  
  88.             }  
  89.           } catch (java.io.IOException e) {}  
  90.         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);  
  91.         else throw new ServletException(t);  
  92.       }  
  93.     } finally {  
  94.       _jspxFactory.releasePageContext(_jspx_page_context);  
  95.     }  
  96.   }  
  97. }  

我们发现:

A:init,destroy和service方法都有,如上面代码所示,test_jsp这个方法继承自HttpJspBase;而如下代码说明,HttpJspBase是继承自HttpServlet:

[html] view plain copy
  1. public abstract class HttpJspBase extends HttpServlet implements HttpJspPage {  

B:而且jsp的呈现都在service中的out.write中。如上面的out.write

3.JSP的九个隐含对象

1)pageContext

2)request

3)session

4)application

5)out

6)response

7)config

8)page

9)exception

注意到下面test_jsp.java中的代码:除了exception外,标明了每个隐含对象的类型;

[java] view plain copy
  1. public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)  
  2.       throws java.io.IOException, javax.servlet.ServletException {  
  3.   
  4.   final javax.servlet.jsp.PageContext pageContext;  
  5.   javax.servlet.http.HttpSession session = null;  
  6.   final javax.servlet.ServletContext application;  
  7.   final javax.servlet.ServletConfig config;  
  8.   javax.servlet.jsp.JspWriter out = null;  
  9.   final java.lang.Object page = this;  

下面的代码说明了,各个隐含对象的出处:

[java] view plain copy
  1. response.setContentType("text/html; charset=UTF-8");  
  2. pageContext = _jspxFactory.getPageContext(this, request, response,  
  3.             nulltrue8192true);  
  4. _jspx_page_context = pageContext;  
  5. application = pageContext.getServletContext();  
  6. config = pageContext.getServletConfig();  
  7. session = pageContext.getSession();  
  8. out = pageContext.getOut();  
  9. _jspx_out = out;  

下面就来使用下经常用到的隐含对象:

不常用的:response,config,exception,page;  剩下的经常使用的(out的用法已经展示了,下面是其他四个的用法):

A)pageContext

B)request

C)session

D)application

其中B,C,D都是由pageContext的get方式得到;上面四个对象的作用于由小到大。pageContext的作用域就是当前页面,request的作用域是一次请求应答,session的作用域是一次会话(一次会话指的是打开浏览器到关闭浏览器的整个过程),application指的是整个web应用,指整个应用从开启到关闭的整个过程,所以它的作用域是最大的。

以常见的方法为例来分析几个对象的作用域:经过setAttribute和getAttrbute为例,测试后发现上面的作用域是完全正确的。

4.JSP的组成部分:

1)Jsp模板元素,指的是静态HTML,它才是整个jsp的框架

2)Jsp的表达式,形如 <%=%>

[html] view plain copy
  1. <%Hello hello = new Hello("zhangjie");%>  
  2. <%out.println(hello);%>  
  3. <%=hello%>  

后两个是完全一样的。
3)Jsp脚本片段 ,形如<%%>

4)Jsp声明,由于整个jsp实际上是在servlet的service方法中执行的,所以示无法在其中声明一个方法的,但可以通过形如<%!  %>的方式声明一个函数,不过很少用

5)Jsp注释格式:

<%--    --%>

html注释格式:

<!--  -->

5.JSP请求转发和请求重定向:本质的区别是请求转发只发出了一次请求,而重定向则发出两次请求;

具体区别如下:

1)请求的转发: 地址栏是初次发出请求的地址.

请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址       

2)请求的转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象. 

请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.          

3)请求的转发: 只能转发给当前 WEB 应用的的资源.

请求的重定向: 可以重定向到任何资源. 

4)请求的转发: / 代表的是当前 WEB 应用的根目录

请求的重定向: / 代表的是当前 WEB 站点的根目录. 

  • 发表于 2017-11-17 19:22
  • 阅读 ( 1298 )
  • 分类:Java

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
不写代码的码农
江南烟雨

可以跨境界的java开发攻城狮

36 篇文章

作家榜 »

  1. 威猛的小站长 124 文章
  2. Jonny 65 文章
  3. 江南烟雨 36 文章
  4. - Nightmare 33 文章
  5. doublechina 31 文章
  6. HJ社区-肖峰 29 文章
  7. 伪摄影 22 文章
  8. Alan 14 文章