jsp技术是在html文件内容中嵌入java代码,jsp页面发布后,由web容器的jsp引擎将jsp页面转化成java类(实际是servlet类的子类),并编译生成class文件(页面第一次访问时)。
1.jsp注释
格式:
<%-- 注释内容 --%>
jsp注释与html注释内容不同,jsp注释不会输出到客户端(浏览器),而html注释会输出到客户端。
html注释格式: <!-- 注释内容 -->
2.jsp声明
格式:
<%!
表达式1;
表达式2;
%>
如:
1 <%! 2 public int count; 3 public String info() 4 { 5 return "Hello world!"; 6 } 7 %>
3.jsp输出表达式
格式:
<%=表达式 %>
如:
1 <%=info() %> 2 <%=count++ %>
4.jsp脚本
格式:
<%
表达式1;
表达式2;
%>
如:
1 <table border="1px"> 2 <tr> 3 <td>数据内容</td> 4 <td>循环次数</td> 5 </tr> 6 <% 7 for(int i=0;i<10;i++) 8 { 9 %> 10 <tr> 11 <td>循环内容</td> 12 <td align="center"><%=i %></td> 13 </tr> 14 <%} %> 15 </table>
实例:
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>JSP知识内容</title> 8 </head> 9 <body> 10 <!-- JSP注释 --> 11 <%-- JSP注释内容 --%> 12 <!-- HTML注释 --> 13 <!-- HTML注释内容 --> 14 15 <%! 16 public int count; 17 public String info() 18 { 19 return "Hello world!"; 20 } 21 %> 22 23 <br/> 24 输出表达式:<%=info() %> 25 <br/> 26 count=<%=count++ %> 27 28 <br/> 29 <hr> 30 jsp脚本输出内容: 31 <table border="1px"> 32 <tr> 33 <td>数据内容</td> 34 <td>循环次数</td> 35 </tr> 36 <% 37 for(int i=0;i<10;i++) 38 { 39 %> 40 <tr> 41 <td>循环内容</td> 42 <td align="center"><%=i %></td> 43 </tr> 44 <%} %> 45 </table> 46 </body> 47 </html>
index.jsp
eclipse集成tomcat并启动后,在%workspace%\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\web项目名\org\apache\jsp目录下会发现index.jsp被转化成index_jsp.java文件。
注:%workspace%为eclipse的workspace路径。
1 /* 2 * Generated by the Jasper component of Apache Tomcat 3 * Version: Apache Tomcat/8.0.20 4 * Generated at: 2016-09-27 14:40:53 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 15 public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase 16 implements org.apache.jasper.runtime.JspSourceDependent, 17 org.apache.jasper.runtime.JspSourceImports { 18 19 20 public int count; 21 public String info() 22 { 23 return "Hello world!"; 24 } 25 26 private static final javax.servlet.jsp.JspFactory _jspxFactory = 27 javax.servlet.jsp.JspFactory.getDefaultFactory(); 28 29 private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; 30 31 private static final java.util.Set<java.lang.String> _jspx_imports_packages; 32 33 private static final java.util.Set<java.lang.String> _jspx_imports_classes; 34 35 static { 36 _jspx_imports_packages = new java.util.HashSet<>(); 37 _jspx_imports_packages.add("javax.servlet"); 38 _jspx_imports_packages.add("javax.servlet.http"); 39 _jspx_imports_packages.add("javax.servlet.jsp"); 40 _jspx_imports_classes = null; 41 } 42 43 private javax.el.ExpressionFactory _el_expressionfactory; 44 private org.apache.tomcat.InstanceManager _jsp_instancemanager; 45 46 public java.util.Map<java.lang.String,java.lang.Long> getDependants() { 47 return _jspx_dependants; 48 } 49 50 public java.util.Set<java.lang.String> getPackageImports() { 51 return _jspx_imports_packages; 52 } 53 54 public java.util.Set<java.lang.String> getClassImports() { 55 return _jspx_imports_classes; 56 } 57 58 public void _jspInit() { 59 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 60 _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); 61 } 62 63 public void _jspDestroy() { 64 } 65 66 public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) 67 throws java.io.IOException, javax.servlet.ServletException { 68 69 final java.lang.String _jspx_method = request.getMethod(); 70 if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) { 71 response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD"); 72 return; 73 } 74 75 final javax.servlet.jsp.PageContext pageContext; 76 javax.servlet.http.HttpSession session = null; 77 final javax.servlet.ServletContext application; 78 final javax.servlet.ServletConfig config; 79 javax.servlet.jsp.JspWriter out = null; 80 final java.lang.Object page = this; 81 javax.servlet.jsp.JspWriter _jspx_out = null; 82 javax.servlet.jsp.PageContext _jspx_page_context = null; 83 84 85 try { 86 response.setContentType("text/html; charset=utf-8"); 87 pageContext = _jspxFactory.getPageContext(this, request, response, 88 null, true, 8192, true); 89 _jspx_page_context = pageContext; 90 application = pageContext.getServletContext(); 91 config = pageContext.getServletConfig(); 92 session = pageContext.getSession(); 93 out = pageContext.getOut(); 94 _jspx_out = out; 95 96 out.write("\r\n"); 97 out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); 98 out.write("<html>\r\n"); 99 out.write("<head>\r\n"); 100 out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n"); 101 out.write("<title>JSP知识内容</title>\r\n"); 102 out.write("</head>\r\n"); 103 out.write("<body>\r\n"); 104 out.write("<!-- JSP注释 -->\r\n"); 105 out.write("\r\n"); 106 out.write("<!-- HTML注释 -->\r\n"); 107 out.write("<!-- HTML注释内容 -->\r\n"); 108 out.write("\r\n"); 109 out.write("\r\n"); 110 out.write("\r\n"); 111 out.write("<br/>\r\n"); 112 out.write("输出表达式:"); 113 out.print(info() ); 114 out.write("\r\n"); 115 out.write("<br/>\r\n"); 116 out.write("count="); 117 out.print(count++ ); 118 out.write("\r\n"); 119 out.write("\r\n"); 120 out.write("<br/>\r\n"); 121 out.write("<hr>\r\n"); 122 out.write("jsp脚本输出内容:\r\n"); 123 out.write("<table border=\"1px\">\r\n"); 124 out.write("\t<tr>\r\n"); 125 out.write("\t\t\t<td>数据内容</td>\r\n"); 126 out.write("\t\t\t<td>循环次数</td>\r\n"); 127 out.write("\t</tr>\r\n"); 128 out.write("\t"); 129 130 for(int i=0;i<10;i++) 131 { 132 133 out.write("\r\n"); 134 out.write("\t\t<tr>\r\n"); 135 out.write("\t\t\t<td>循环内容</td>\r\n"); 136 out.write("\t\t\t<td align=\"center\">"); 137 out.print(i ); 138 out.write("</td>\r\n"); 139 out.write("\t\t</tr>\r\n"); 140 out.write("\t"); 141 } 142 out.write("\r\n"); 143 out.write("</table>\r\n"); 144 out.write("</body>\r\n"); 145 out.write("</html>\r\n"); 146 } catch (java.lang.Throwable t) { 147 if (!(t instanceof javax.servlet.jsp.SkipPageException)){ 148 out = _jspx_out; 149 if (out != null && out.getBufferSize() != 0) 150 try { 151 if (response.isCommitted()) { 152 out.flush(); 153 } else { 154 out.clearBuffer(); 155 } 156 } catch (java.io.IOException e) {} 157 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 158 else throw new ServletException(t); 159 } 160 } finally { 161 _jspxFactory.releasePageContext(_jspx_page_context); 162 } 163 } 164 }
index_jsp.java
从index_jsp.java内容中可看到,index_jsp类继承org.apache.jasper.runtime.HttpJspBase(该类是HttpServlet类的子类),并包含了_jspInit()、_jspDestory()、_jspService()方法,这三个方法与servlet对应的是init()、destory()、server()方法。
jsp声明中的变量为index_jsp类的内部变量,jsp声明中的方法为index_jsp类的方法;而jsp脚本中的代码则被生成到index_jsp类的_jspService()方法中,所以,jsp脚本代码块中的变量都是局部变更,不能用private或者是public的修饰。
浏览器中访问index.jsp页面时,每次刷新页面时,count的值都会加1,因为jsp页面对应的类在web容器中是唯一一个实例(sigleton实例)。
index.jsp页面输出的源码中,看不到jsp注释内容。