JSP 基础(一)
2015-11-05 18:22
519 查看
JavaServletPage(JSP)
[b]一 JSP简介[/b]
Servlet的缺陷
–Servlet的编码、部署和调试任务繁琐
–生成动态网页繁琐,不利于项目分工
为了弥补Servlet的这些缺陷,SUN公司在Servlet的基础上推出了JSP技术作为解决方案
采用JSP技术编写动态页面
–由HTML语句和嵌套在其中的Java代码组成的一个普通文本文件
–JSP页面的扩展名必须为.jsp
[b]二 JSP运行机制与生命周期[/b]
JSP的执行包括7个阶段
2.1 JSP页面翻译阶段:Web容器第一次接收到某个JSP页面的请求后,首先把自动将该页面翻译成Servlet代码。
执行了上面的这个.jsp后,会在D:\apache-tomcat-6.0.20\work\Catalina\localhost\项目名\org\apache\jsp下发现一个index_jsp.class文件和一个index_jsp.java文件
打开java文件
2.2 JSP页面编译阶段:index_jsp.java文件编译成index_jsp.class文件。
2.3 JSP页面类装载阶段:Web容器装载新生成的Servlet类。
2.4 JSP页面实例化阶段:Web容器创建实例。
2.5 JSP页面初始化阶段:web容器调用Servlet示例的_jspInit()方法。
2.6 JSP页面服务阶段:容器创建一个新线程来处理客户的请求,Servlet对象的_jspService()方法运行。
2.7 JSP页面的销毁:servlet对象的_jspDestory()方法。
如果一个Web应用程序中包含有JSP页面,部署这个应用时,在JSP生命周期中,整个翻译和编译步骤只发生一次。JSP一旦被翻译和编译,就像其他Servlet一样了。其实只有在第一次执行的时候有性能上的差别。
JSP通常用于简化创建产生文本的Servlet,二Servlet更适合用于发送原生字节到客户端或所需要用java源代码完全控制源代码的场合。
[b]三 JSP语法与语义[/b]
JSP网页主要有元素(Element)和模板数据(Template Data)两部分组成。
元素可以分成三个不同的类别:脚本元素,指令,动作。(下面进行解释)
[b]四 脚本元素[/b]
在JSP中有三种不同类型的脚本元素:scriptlet,脚本表达式,声明。
[b] 4.1 Scriptlet [/b]
JSP脚本片断(scriptlet)用于在JSP页面中编写多行Java代码。语法格式:
<% 多行java代码 %>
在<% %>中可以定义变量、编写语句,不能定义方法。
eg:
[b] 4.2 脚本表达式[/b]
JSP脚本表达式(expression)用于将程序数据输出到客户端。语法:
<%= 变量或表达式 %>
eg:
JSP引擎在翻译脚本表达式时,会将程序数据转成字符串,然后在相应位置用out.print(…) 将数据输给客户端。
JSP脚本表达式中的变量或表达式后面不能有分号(;)
[b]4.3 声明[/b]
JSP页面中编写的所有代码,默认会翻译到servlet的service方法中, 而Jsp声明中的java代码被翻译到_jspService方法的外面。语法:
<%! java代码 %>
所以,JSP声明可用于定义JSP页面转换成的Servlet程序的静态代码块、成员变量和方法 。
多个静态代码块、变量和函数可以定义在一个JSP声明中,也可以分别单独定义在多个JSP声明中。
JSP隐式对象的作用范围仅限于Servlet的_jspService方法,所以在JSP声明中不能使用这些隐式对象。
可以在JSP程序中声明一个或多个变量。但是每一个声明语句都必须以分号结束
eg:
五 注释
六 JSP指令
七 JSP标准动作
八 JSP隐式对象
[b]一 JSP简介[/b]
Servlet的缺陷
–Servlet的编码、部署和调试任务繁琐
–生成动态网页繁琐,不利于项目分工
为了弥补Servlet的这些缺陷,SUN公司在Servlet的基础上推出了JSP技术作为解决方案
采用JSP技术编写动态页面
–由HTML语句和嵌套在其中的Java代码组成的一个普通文本文件
–JSP页面的扩展名必须为.jsp
[b]二 JSP运行机制与生命周期[/b]
JSP的执行包括7个阶段
2.1 JSP页面翻译阶段:Web容器第一次接收到某个JSP页面的请求后,首先把自动将该页面翻译成Servlet代码。
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <% out.print("Hello Jsp"); %> </body> </html>
执行了上面的这个.jsp后,会在D:\apache-tomcat-6.0.20\work\Catalina\localhost\项目名\org\apache\jsp下发现一个index_jsp.class文件和一个index_jsp.java文件
打开java文件
package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.AnnotationProcessor _jsp_annotationprocessor; public Object getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName()); } public void _jspDestroy() { } public void _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=ISO-8859-1"); 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'); out.write('\n'); String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 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(" <base href=\""); out.print(basePath); out.write("\">\r\n"); out.write(" \r\n"); out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n"); out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n"); out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n"); out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n"); out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n"); out.write("\t<!--\r\n"); out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n"); out.write("\t-->\r\n"); out.write(" </head>\r\n"); out.write(" \r\n"); out.write(" <body>\r\n"); out.write(" "); out.print("Hello Jsp"); 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); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
2.2 JSP页面编译阶段:index_jsp.java文件编译成index_jsp.class文件。
2.3 JSP页面类装载阶段:Web容器装载新生成的Servlet类。
2.4 JSP页面实例化阶段:Web容器创建实例。
2.5 JSP页面初始化阶段:web容器调用Servlet示例的_jspInit()方法。
2.6 JSP页面服务阶段:容器创建一个新线程来处理客户的请求,Servlet对象的_jspService()方法运行。
2.7 JSP页面的销毁:servlet对象的_jspDestory()方法。
如果一个Web应用程序中包含有JSP页面,部署这个应用时,在JSP生命周期中,整个翻译和编译步骤只发生一次。JSP一旦被翻译和编译,就像其他Servlet一样了。其实只有在第一次执行的时候有性能上的差别。
JSP通常用于简化创建产生文本的Servlet,二Servlet更适合用于发送原生字节到客户端或所需要用java源代码完全控制源代码的场合。
[b]三 JSP语法与语义[/b]
JSP网页主要有元素(Element)和模板数据(Template Data)两部分组成。
元素可以分成三个不同的类别:脚本元素,指令,动作。(下面进行解释)
[b]四 脚本元素[/b]
在JSP中有三种不同类型的脚本元素:scriptlet,脚本表达式,声明。
[b] 4.1 Scriptlet [/b]
JSP脚本片断(scriptlet)用于在JSP页面中编写多行Java代码。语法格式:
<% 多行java代码 %>
在<% %>中可以定义变量、编写语句,不能定义方法。
eg:
<% /*声明变量*/ int sum=0; /*编写语句*/ for (int i=1;i<=100;i++){ sum+=i; } out.println("<h1>Sum="+sum+"</h1>"); out.print("Hello Jsp"); %>
[b] 4.2 脚本表达式[/b]
JSP脚本表达式(expression)用于将程序数据输出到客户端。语法:
<%= 变量或表达式 %>
eg:
<body> <% int[] balls = new int[6]; Random r = new Random(); for (int i = 0; i < 6;) { boolean flag = true; int temp = r.nextInt(33) + 1; for (int j = 0; j <= i; j++) { if (balls[j] == temp) { flag = false; break; } } if (flag) { balls[i] = temp; i++; %> <div class="red"><%=temp%></div> <% } } %> <div class="blue"><%=r.nextInt(16) + 1%></div> </body>
JSP引擎在翻译脚本表达式时,会将程序数据转成字符串,然后在相应位置用out.print(…) 将数据输给客户端。
JSP脚本表达式中的变量或表达式后面不能有分号(;)
[b]4.3 声明[/b]
JSP页面中编写的所有代码,默认会翻译到servlet的service方法中, 而Jsp声明中的java代码被翻译到_jspService方法的外面。语法:
<%! java代码 %>
所以,JSP声明可用于定义JSP页面转换成的Servlet程序的静态代码块、成员变量和方法 。
多个静态代码块、变量和函数可以定义在一个JSP声明中,也可以分别单独定义在多个JSP声明中。
JSP隐式对象的作用范围仅限于Servlet的_jspService方法,所以在JSP声明中不能使用这些隐式对象。
可以在JSP程序中声明一个或多个变量。但是每一个声明语句都必须以分号结束
eg:
<%! String s="hello"; %> <%! int a,b,c; %> <%! java.util.Date date=new java.util.Date(); %>
<%! public void method(){ } %>
五 注释
六 JSP指令
七 JSP标准动作
八 JSP隐式对象
相关文章推荐
- JS操作JSON总结
- Ext JS表单Ext.form.FormPanel
- HTML接收?号后参数和js获取工程名
- 基础篇:8.如何定义变量?js变量有什么特点?
- iOS js oc相互调用(JavaScriptCore)(二)
- iOS js oc相互调用(JavaScriptCore)
- JSON和XML
- protobuf与json转换
- 图解javascript this指向什么?
- 使用JS获取当前地理位置方法汇总
- 利用JS判断浏览器种类
- jsp的etl中进行字符串相加
- JavaScript 的性能优化:加载和执行
- javascript 内存泄漏的学习
- JS闭包的理解
- json读取+对象转换+csv读写
- jsonp
- 11月9日 Meetup and talk about JS
- jstl中empty
- JSP到Servlet的编译过程初探