jsp
编译生成class,部署在服务器。注意并不是在客户端生成html,而是服务器根据jsp生成html后再返回给客户端。
records: <% = records%>
now: <% = new java.util.Date() %>
保存为/examples/time.jsp
将来通过http://localhost:8080/examples/time.jsp
来访问。
jsp语法
<%@ page session="true" %> <!-- @ variable; @ page; -->
<%@ page import="java.util.*", "java.io.*"
language="java"
isErrorPage="true <!-- 必考 -->
errorPage="/error.jsp"%>
涉及的内容在javax.servlet.jsp
包中。
jspDestroy()
& jspInit()
实际上jsp就是一个servlet组件。
<%@ include file="a.html"%>
<jsp:include page="/servlet/a"></jsp:include> <!-- jsp动作 -->
<jsp:include page="/servlet/a"/> <!-- xml风格的结束 -->
值得注意的是这俩include
的不同点:
<%@ include file="/servlet/a"%> <!-- 错误,这是静态的,只能包含静态文本文件,不能有servlet -->
<jsp:include page="a.html" /> <!-- 正确,可以包含servlet,因为是运行时加载 -->
jsp中的java表达式
<%= new Student() %> <!-- Student().toString() -->
<%! public void a() {} %> <!-- 变成目标类的成员函数 -->
jsp动作*
<jsp:useBean id="a" class="com.abc.Student" scope="page|request|session|application" />
<!-- a = new Student() -->
<%
int a = 9; // a is defined in jsp:useBean
%>
<jsp:getProperty name="a" property="age" /> <!-- a.getAge() -->
<jsp:setProperty name="a" property="age" value="20"/>
为什么使用jsp
因为如果要在servlet中打印出一个html页面的话势必会需要打印大量常量。考虑到常量不应该在程序中大量出现,所以应当使用jsp。
jsp的9个隐含变量
- HttpServletRequest request
- HttpServletResponse response
- HttpSession session
- ServletConfig config
- ServletContext application
- JspWriter out 注意和PrintWriter的区别。
- PageContext pageContext
- page this
- Exception exception
PageContext
jsp的上下文,包括request,response和其他一大堆东西
pageContext.getServletConfig();
pageContext.getServletContext();
// ...
就是门面类。
有这个东西之后会变得非常方便:
request.getRequestDispatcher("/b.jsp").forward(request, response);
pageContext.forward("/b.jsp");
适配器类和门面类
设计模式,adaptor和fascade。
没仔细听。总归这俩确实不大一样,fascade差不多就是把俩类拼到一起,adaptor算是对父类的细化。
回去要自己学的
- PrintWriter
- PageContext
- jsp动作
- Hello Tiger(Tag?) (in Tomcat Examples)
- jsp
- mui (from mvc)