Jsp基础语法
page指令介绍
Language : 用来定义要使用的脚本语言;
contentType:定义 JSP 字符的编码和页面响应的 MIME 类型;
pageEncoding:Jsp 页面的字符编码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
scriptlet标签
通过 scriptlet 标签我们可以在 Jsp 里嵌入 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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <%! String str="全局变量"; %> <%! public void fun1(){ System.out.println("全局方法"); } %> <%! class C{ private int a; public void f(){ System.out.println("全局类"); } } %> <% int a=1234; String b="java"; out.println(a+b+"局部变量"); %> <title>Insert title here</title> </head> <body> <%=b %> </body> </html>
当你通过http请求一个JSP页面是,首先Tomcat会将JSP编译成为Servlet,然后执行Servlet.在其java文件中可以看出哪些是全局变量,哪些是局部变量。
JSP编译后的路径: apache-tomcat-7.0.65\work\Catalina\localhost\XX

/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.65 * Generated at: 2017-03-04 08:10:55 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 scriptlet_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { String str="全局变量"; public void fun1(){ System.out.println("全局方法"); } class C{ private int a; public void f(){ System.out.println("全局类"); } } 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; out.write("\r\n"); out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n"); out.write("<html>\r\n"); out.write("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n"); out.write('\r'); out.write('\n'); out.write('\r'); out.write('\n'); out.write('\r'); out.write('\n'); int a=1234; String b="java"; out.println(a+b+"局部变量"); out.write("\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("<body>\r\n"); out.print(b ); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } 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); } } }
Jsp注释
<!-- --> Html 注释 客户端可见
<%-- --%> Jsp注释 客户端不可见
// java 单行注释
/* */ java 多行注释
Jsp包含指令
<%@ include file=”要包含的文件”%> 静态包含 先包含,后编译处理 (合并后编译可能会有变量冲突);
<jsp:include page=”要包含的文件”> 动态包含 先编译处理,后包含;以后开发用动态包含;

<body> <%-- <h1>静态包含</h1> <%@ include file="common/head.html" %> <p>content</p> <%@ include file="common/footer.jsp" %> --%> <h1>动态包含</h1> <jsp:include page="common/head.html"/> <p>content</p> <jsp:include page="common/footer.jsp"/> </body>
Jsp跳转指令,服务器内部跳转,可带参数;

<body> <jsp:forward page="target.jsp"> <jsp:param value="java1234" name="userName"/> <jsp:param value="123456" name="password"/> </jsp:forward> </body> ----------------------------target.jsp------------------------------------------ <body> 服务器内部跳转后的页面<br/> userName:<%=request.getParameter("userName") %><br/> password:<%=request.getParameter("password") %><br/> </body>
http://blog.csdn.net/lioncode/article/details/8155374