Java Web笔记 – JSP的语法和相关指令语法原理分析 编译器指令 动作语法 声明指令

1、JSP的语法:
1.1、JSP的三种语法:
1.1.1、编译器指令(DIRECTIVE):
<%@ page import="java.io.*"%>

包含指令,页指令,taglib指令

包含在<%@ %>卷标里

两个主要的指令page include

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ include file=""%>
1.1.2、脚本语法:
1.1.3、动作语法:
<jsp:forward>
<jsp:forward page="">
    <jsp:param name="username" value="arthinking" />
</jsp:forward>

参数可以从另外一个页面的getParameter方法获取。

在指令后的所有代码都不会执行,而直接跳转到另外一个页面进行执行。

<jsp:forward>实际上是调用了PageContext的forward方法。

<jsp:include>
<jsp:include page="">
    <jsp:param name="username" value="arthinking" />
</jsp:include>

参数可以从另外一个页面的getParameter方法获取。

request的getRequestDispatcher和getRequestDispatcher的forward方法说明:

请求转发,保存了ServletRequest中setAttribute的参数,可以使用这种方法传递参数:

req.setAttribute("username", "arthinking");
RequestDispatcher rd = req.getRequestDispatcher("message.jsp");
rd.forward(req, resp);

在同一个请求里面,可以使用getParameter方法获取setAttribute方法设置的参数。

getRequestDispatcher
public RequestDispatcher getRequestDispatcher(String path)
The default behavior of this method is to return getRequestDispatcher(String path) on the wrapped request object.
Specified by: 
getRequestDispatcher in interface ServletRequest
Parameters: 
path - a String specifying the pathname to the resource. If it is relative, it must be relative against the current servlet.
Returns: 
a RequestDispatcher object that acts as a wrapper for the resource at the specified path, or null if the servlet container cannot return a RequestDispatcher
See Also: 
RequestDispatcher, ServletContext.getRequestDispatcher(java.lang.String)
1.2、注释:

HTML注释:<!-- -->

隐藏注释:<%-- --%> 不发送到客户端

1.3、表达式:
<%=new java.util.Date() %>

等号开头,不用分号作为结尾。

1.4、声明语句:
<%! int a = 1; %>

声明和脚本段转换为Servlet代码的位置是不一样的,声明语句定义的变量会方法成员变量的位置,而脚本段中的变量会转换为局部变量。每个用户都会访问到成员变量,而脚本段的局部变量每个用户保存一个副本,互不影响。声明语句的变量在重新刷新页面时不会被重新定义,所以在_jspService方法中修改的状态是会保留下来的。

对于Servlet来说,是单例模式的。

但是这种声明语句用的比较少,因为这种需求不是很多。

你可能感兴趣的:(Java Web笔记 – JSP的语法和相关指令语法原理分析 编译器指令 动作语法 声明指令)