@duyao
2015-05-03T07:59:53.000000Z
字数 3881
阅读 1630
jsp
例如文件为myjsp.java
第一次访问jsp文件,服务器会将文件翻译为servlet文件myjsp_jsp.java并编译为.class文件,在文件tomcat根目录的..\work\Catalina中可以找到,即D:\Program Files\apche\apache-tomcat-6.0.43\work\Catalina\localhost\web应用名\org\apache\jsp,然后加载到服务器内存中去,如果是第二次访问,就会直接访问内存中的实例,因此jsp也是单例的,因此第一访问jsp网站速度比较慢,以后访问速度比较快。如果某个jsp被修改了,就相当于第一次访问jsp。(web服务器有一张表记录每个jsp是否是第一次访问)
注意:jsp出错后会报出翻译后servlet中的位置
在该xxx_jsp.java文件中,有几分主要函数如下,其实就是servlet中的需要实现的函数init,destory,service
//myjsp_jsp.javapublic final class jsp1_jsp {public void _jspInit() {...}public void _jspDestroy() {}public void _jspService(HttpServletRequest request, HttpServletResponse response)throws java.io.IOException, ServletException {...}
//myjsp_jsp.java的_jspService函数public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {PageContext pageContext = null;HttpSession session = null;ServletContext application = null;ServletConfig config = null;JspWriter out = null;Object page = this;//这里out已经设置为内部流(JspWriter out = null),因此可以在jsp中直接使用//html的标签直接使用out输出out.write("<html>\r\n");out.write(" <head>\r\n");out.write(" </head>\r\n");out.write(" <body>\r\n");out.write(" <h1>hello,world!</h1>\r\n");out.write("</html>");//对于内嵌的java代码则定义为内部变量,放在_jspService函数中/*jsp中的源代码<%int i=1;out.print("i="i);%>*/int i=1;out.print("i="i);
一共提供了9个,在生成的xxx_jsp.java文件中_jspService函数中可以找到
out.print();
HttpServletRequest
request.getParameter(String);request.getParameterValues(String);request.getAttribute(String);request.getAttributeNames();request.getCookies();
HttpServletResponse
response.addCookie(Cookie);response.sendRedirect(String);
HttpSession
session.getAttribute(String);session.setAttribute(String,Object);
ServletContext
application.getAttribute(String);application.setAttribute(String,Object);
pageContext
只在本页面生效的域对象,可以setAttribute等
exception
代表运行时的一个异常
page
代表jsp这个实例,相当于this
config
得到web.xml的配置信息,相当于servlet的ServletConfig
| 名称 | 作用域 |
|---|---|
| application | 在所有应用程序中有效 |
| session | 在当前会话中有效 |
| request | 在当前请求中有效 |
| page | 在当前页面有效 |
概念:用于从jsp发送一个信息到容器,比如设置全局变量,文字编码,引入宝
page指令
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
include指令
<%@include file="index.jsp" %>
该指令用于引入一个文件,jsp引擎通常会把这2个页面翻译成一个jsp页面,也成为静态引入,以webRoot为根目录
被引入的jsp页面只保留page指令,标签<html>,<body>均要省略
taglib指令
<% java代码 %>_jspService的变量 <%=java表达式%> <%=rs.getInt(1)%>xxx——jsp.java的成员变量和成员函数 <%! java代码 %>,<%! java函数代码 %>
<%! int i=900; //全局变量 %><% int i=90; //局部变量out.print("i="+i);%>
在对相应的xx_jsp.java文件中声明的位置完全不同,declaration变量时该servlet的成员变量
//jsp1_jsp.javapublic final class jsp1_jsp {int i=900;//全局变量public void _jspInit() {...}public void _jspDestroy() {}public void _jspService(HttpServletRequest request, HttpServletResponse response)throws java.io.IOException, ServletException {int i=90;//局部变量out.print("i="+i);...}
<jsp:forward >
<jsp:forward page="index.jsp"></jsp:forward>
这里起到跳转作用,通常开发时,将各种jsp文件不直接放在WebRoot中,因为这样利用<a>标签可以将网站源代码下载下来,而且得到路径后可以直接访问,很不安全,因此要把jsp文件放在/WebRoot/WEB-INF文件夹中,这样即使得到路径也不能直接访问和下载源码。而访问网站的方法是WebRoot放一个xxx.jsp通过<jsp:forward page="/WEB-INF/xxx.jsp"></jsp:forward>转向/WebRoot/WEB-INF文件夹要访问的jsp文件
<jsp:include >
<jsp:include page=""></jsp:include>
这里也是引入文件,注意与`<%@include>区分,但是这里的引入是动态引入,即jsp引擎会将这2个servlet分别编译形成2个java文件,因此不必省略标签<html>,<body>
2种方式
<-- 注释 -->和<%-- 注释 --%>
没有%的可以在源码中显示,而有%的在源码中不能显示
建议使用<%-- 注释 --%>