一、JSP中的JAVA代码块:JSP小脚本
JSP的Java代码块也称为JSP小脚本,是以<% %>括起来的部分,其中可以编写Java代码。编写在Java代码块中的代码,会被JSP翻译引擎将其原样的植入到Servlet的_jspService()方法中作为方法中的Java语句出现。所以,其中存放的必须是以分号结尾的Java语句。
需要注意的是,JSP代码块可以放置在JSP页面的任意位置(头部、主体、<html>标签外),可以放置任意数量。但它们都会被按照顺序放置到Servlet的_jspService()方法中。也就是说,其执行顺序是与其在JSP页面中的出现顺序是一致的。
二、由于JSP小脚本是被翻译到Servlet的_jspService()方法中,所以JSP小脚本中是不能出现如下内容的:
2.1 声明的变量是不能添加权限访问控制符的。(Illegal modifier for parameter a; only final is permitted)
2.2 不能定义方法。
2.3 不能定义静态语句块。
三、示例
3.1 JSP源码
<% System.out.println("我是Java代码块!我可以放在位置一!"); %> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <% System.out.println("我是Java代码块!我可以放在位置二!"); %> <html> <head> <% int a=2; %> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>first_jsp</title> <% System.out.println("我是Java代码块!我可以放在位置三!"); %> </head> <% int b=5; %> <body> <!-- HTML注释 --> <%--JSP注释 --%> My First jsp! <% System.out.println("我是Java代码块!我可以放在位置四!"); %> </body> <% int num=a*b; System.out.println("num="+num); %> </html> <% System.out.println("我是Java代码块!我可以放在位置五!"); %>
3.2将上面的JSP页面 经Tomcat遵循JSP规范将其翻译为.java源文件内容如下:
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.85 * Generated at: 2019-01-22 22:15:21 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private volatile javax.el.ExpressionFactory _el_expressionfactory; private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public javax.el.ExpressionFactory _jsp_getExpressionFactory() { if (_el_expressionfactory == null) { synchronized (this) { if (_el_expressionfactory == null) { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); } } } return _el_expressionfactory; } public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() { if (_jsp_instancemanager == null) { synchronized (this) { if (_jsp_instancemanager == null) { _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } } } return _jsp_instancemanager; } public void _jspInit() { } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.PageContext _jspx_page_context = null; try { response.setContentType("text/html; charset=UTF-8"); 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; System.out.println("我是Java代码块!我可以放在位置一!"); out.write(" "); out.write(" "); out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> "); System.out.println("我是Java代码块!我可以放在位置二!"); out.write(" "); out.write("<html> "); out.write("<head> "); int a=2; out.write(" "); out.write("<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> "); out.write("<title>first_jsp</title> "); System.out.println("我是Java代码块!我可以放在位置三!"); out.write(" "); out.write("</head> "); int b=5; out.write(" "); out.write("<body> "); out.write("<!-- HTML注释 --> "); out.write(" "); out.write("My First jsp! "); System.out.println("我是Java代码块!我可以放在位置四!"); out.write(" "); out.write("</body> "); int num=a*b; System.out.println("num="+num); out.write(" "); out.write("</html> "); System.out.println("我是Java代码块!我可以放在位置五!"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { if (response.isCommitted()) { out.flush(); } else { out.clearBuffer(); } } catch (java.io.IOException e) {} if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }
3.3 JSP源码和其遵循JSP规范被Tomcat翻译成.java源文件的图解
测试结果截图:
jsp小脚本中是可以使用Java注释的,原样编译。