<%@ …… %> 这是指令
<%@ page ……import="java.util.Date" %> 这是page指令,import是page指令的一个属性
<% …… %> 这是Scriptlet,里面是纯Java代码,需要分号结尾
<% = …… %> 这是表达式,表达式不用分号结尾;绝对不能把一个返回类型为void的方法用作表达式
JSP经过Web容器的转换,生成了Servlet:
JSP中的Scriptlet和表达式的内容都会放在Servlet的service()方法中,因此都是局部变量。
那么,怎么声明Servlet类的成员变量和方法呢?答案就是:JSP声明
<% ! …… %> 这就是JSP声明。例如<%! int count = 0; %>
JSP声明使用感叹号,后面需要分号结尾。
为JSP配置Servlet初始化参数:
1 <web-app> 2 ????<servlet> 3 ????????<servlet-name>MyTestInit</servlet-name> 4 ????????<jsp-file>/TestInit.jsp</jsp-file> ?//这个与常规的Servlet不同 5 ????????<init-param> 6 ????????????<param-name>email</param-name> 7 ????????????<param-value>1234@qq.com</param-value> 8 ????????</init-param> 9 ????</servlet>10 ????11 ????<servlet-mapping> //为一个JSP定义Servlet时,还必须为JSP页面定义一个Servlet映射12 ????????<servlet-name>MyTestInit</servlet-name>13 ????????<url-pattern>/TestInit.jsp</url-pattern>14 ????</servlet-mapping>15 </web-app>
覆盖jspInit()方法:(这个方法由Servlet的init()方法来调用)
1 <%! public void jspInit(){ ?//使用JSP声明来覆盖jspInit()方法2 ????ServletConfig scg = getServletConfig();3 ????String emailAddr = scg.getInitParameter("email");4 ????5 ????ServletContext sct = getServletContext();6 ????sct.setAttribute("mail", emailAddr);7 }
JSP增加了一个作用域,页面作用域:
标准的Servlet作用域有三个:请求、会话、应用(上下文)作用域;
JSP增加了页面作用域(pageContext)
在JSP中,使用4个隐式对象来得到和设置属性:
Servlet中 | JSP中(隐式对象) | JSP中(隐式对象) | |
请求作用域(request) | request.setAttribute("foo", Object); | request.setAttribute("foo", Object); | request.getAttribute("foo"); |
会话作用域(session) | request.getSession().setAttribute("foo", Object); | session.setAttribute("foo", Object); | session.getAttribute("foo"); |
应用作用域(context) | getServletContext().setAttribute("foo", Object); | application.setAttribute("foo", Object); | application.getAttribute("foo"); |
页面作用域(pageContext) | 不适用 | pageContext.setAttribute("foo", Object); | pageContext.getAttribute("foo"); |
JSP还可以通过pageContext引用来得到任意作用域的属性,以及设置任意作用域的属性:
1.设置一个会话作用域属性:<% pageContext.setAttribute("foo", Object, PageContext.SESSION_SCOPE); %>
2.设置一个应用作用域属性:<% pageContext.setAttribute("foo", Object, PageContext.APPLICATION_SCOPE); %>
3.得到一个应用作用域属性:<% pageContext.getAttribute("foo", PageContext.APPLICATION_SCOPE); %>
指令有三种:
page指令、taglib指令、include指令
在JSP中禁用脚本元素:(只有这一种方法)
1 <web-app> 2 ????<jsp-config> 3 ????????<jsp-property-group> 4 ????????????<url-pattern>*.jsp</url-pattern> 5 ????????????<scripting-invalid> //这个标记让JSP禁止使用脚本元素 6 ????????????????true 7 ????????????</scripting-invalid> 8 ????????</jsp-property-group> 9 ????</jsp-config>10 </web-app>
在JSP中忽略EL:
方法一:在web.xml中指明:
1 <web-app> 2 ????<jsp-config> 3 ????????<jsp-property-group> 4 ????????????<url-pattern>*.jsp</url-pattern> 5 ????????????<el-ignored> //这个标记表示忽略EL 6 ????????????????true 7 ????????????</el-ignored> 8 ????????</jsp-property-group> 9 ????</jsp-config>10 </web-app>
方法二:或者通过page指令:
<%@ page isELIgnored="true" %>
动作 —— 标准动作和非标准动作:
1 <jsp:include page="wickedFooter.jsp" /> ? //标准动作2 <c:set var="rate" value="32" /> //非标准动作
Head First Servlets & JSP 学习笔记 第七章 —— 作为JSP
原文地址:https://www.cnblogs.com/czp2bconfident/p/8893899.html