[关闭]
@duyao 2015-05-03T07:59:53.000000Z 字数 3881 阅读 1520

jsp原理

jsp


jsp(java server page)
  • 运行在服务器端
  • 基础是servlet,因此也是动态网页
  • 是一种综合技术=html+css+javasricp+java片段+jsp标签
  • jsp无需配置,修改后无需重新发布
  • 访问方法:http://ip:8080/web应用名/jsp路径(路径从webRoot开始)

原理

web服务器是如何调用并执行jsp页面的

例如文件为myjsp.java
第一次访问jsp文件,服务器会将文件翻译为servlet文件myjsp_jsp.java并编译为.class文件,在文件tomcat根目录的..\work\Catalina中可以找到,即D:\Program Files\apche\apache-tomcat-6.0.43\work\Catalina\localhost\web应用名\org\apache\jsp,然后加载到服务器内存中去,如果是第二次访问,就会直接访问内存中的实例,因此jsp也是单例的,因此第一访问jsp网站速度比较慢,以后访问速度比较快。如果某个jsp被修改了,就相当于第一次访问jsp。(web服务器有一张表记录每个jsp是否是第一次访问)
注意:jsp出错后会报出翻译后servlet中的位置
在该xxx_jsp.java文件中,有几分主要函数如下,其实就是servlet中的需要实现的函数init,destory,service

  1. //myjsp_jsp.java
  2. public final class jsp1_jsp {
  3. public void _jspInit() {
  4. ...
  5. }
  6. public void _jspDestroy() {
  7. }
  8. public void _jspService(HttpServletRequest request, HttpServletResponse response)
  9. throws java.io.IOException, ServletException {
  10. ...
  11. }

jsp的html标签的排版空间,java代码段是如何发送到客户端的

  1. //myjsp_jsp.java的_jspService函数
  2. public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {
  3. PageContext pageContext = null;
  4. HttpSession session = null;
  5. ServletContext application = null;
  6. ServletConfig config = null;
  7. JspWriter out = null;
  8. Object page = this;
  9. //这里out已经设置为内部流(JspWriter out = null),因此可以在jsp中直接使用
  10. //html的标签直接使用out输出
  11. out.write("<html>\r\n");
  12. out.write(" <head>\r\n");
  13. out.write(" </head>\r\n");
  14. out.write(" <body>\r\n");
  15. out.write(" <h1>hello,world!</h1>\r\n");
  16. out.write("</html>");
  17. //对于内嵌的java代码则定义为内部变量,放在_jspService函数中
  18. /*jsp中的源代码
  19. <%
  20. int i=1;
  21. out.print("i="i);
  22. %>
  23. */
  24. int i=1;
  25. out.print("i="i);

web服务器调用jsp时会哪些java对象

一共提供了9个,在生成的xxx_jsp.java文件中_jspService函数中可以找到

  1. request.getParameter(String);
  2. request.getParameterValues(String);
  3. request.getAttribute(String);
  4. request.getAttributeNames();
  5. request.getCookies();
  1. response.addCookie(Cookie);
  2. response.sendRedirect(String);
  1. session.getAttribute(String);
  2. session.setAttribute(String,Object);
  1. application.getAttribute(String);
  2. application.setAttribute(String,Object);

jsp的4个作用域

名称 作用域
application 在所有应用程序中有效
session 在当前会话中有效
request 在当前请求中有效
page 在当前页面有效

jsp语法

指令元素

概念:用于从jsp发送一个信息到容器,比如设置全局变量,文字编码,引入宝

脚本元素

  1. <%! int i=900; //全局变量 %>
  2. <% int i=90; //局部变量
  3. out.print("i="+i);
  4. %>

在对相应的xx_jsp.java文件中声明的位置完全不同,declaration变量时该servlet的成员变量

  1. //jsp1_jsp.java
  2. public final class jsp1_jsp {
  3. int i=900;//全局变量
  4. public void _jspInit() {
  5. ...
  6. }
  7. public void _jspDestroy() {
  8. }
  9. public void _jspService(HttpServletRequest request, HttpServletResponse response)throws java.io.IOException, ServletException {
  10. int i=90;//局部变量
  11. out.print("i="+i);
  12. ...
  13. }

动作元素

jsp注释

2种方式
<-- 注释 --><%-- 注释 --%>
没有%的可以在源码中显示,而有%的在源码中不能显示
建议使用<%-- 注释 --%>

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注