JSP基础:
jsp文件编译过程:
jsp-.java-.servlet-编译后再tomcat的work目录下
配置全局错误页面:
在web.xml中
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
jsp页面9个对象:
(4个域对象:request\session\pageContext\application)
(4个方法:setAttribute\getAttribute\RemoveAttribute\getAttributeName)
>session:浏览器与服务器的一次回话session.getId()
>pageContext:(注:可获取其他8个隐式的对象及servlet对象)
>page:相当于this
>request
1、request:HttpServletRequest类型
参数传递:
<a href="index.jsp?id=1">获取参数</a>
<%=request.getParameter("id")%>
4、为request对象域管理属性(在用户页面与服务器间传递数据)
request.setAttribute("date",new Date());
<%=request.getAttribute("date")%>
<%request.removeAttribute("date")%>
5、cookie:服务器端生成发送给浏览器
对象集合request.getCookies()
>response对象
1、重定向:
response.sendRedirect("URL");
2、处理HTTP头文件:
response.setHeader("refresh","2;URL=welcome.jsp");
设置2秒后刷新跳转到welcome.jsp页面
3、设置缓冲区的大小:服务器发送到客户端先写入缓冲区
response.setBufferSize(0);
>session对象:一次浏览器会话
1、创建获取session信息(map型):
setAttribute(key,obj);
getAttribute(key);
注:用于浏览器的一次回话
2、移除与销毁:
session.removeAttribute(key)
session.invalidate();
3、session ID获取:
session.getId()
4、设置会话有效时间:
session.setMaxInactiveInterval(s);
>application对象:当前web应用,是servletContext对象
1、访问应用程序的初始参数
<context-param>
<param-name>url</param-name>
<param-value>www.baidu.com</param-value>
</context-param>
String url=application.getInitParameter("url");
out.println("URL:"+url+"<br>");
注:配置xml时需重启服务器
2、管理应用程序的环境属性:与session类似,但保存在服务器
getAttributeNames();
getAttribute(name);
setAttribute(key,obj);
removeAttribute(name);
>config对象:
获取xml的配置信息
<init-param>
<param-name>qq</param-name>
<param-value>123456</param-value>
</init-param>
<%=config.getInitParameter("qq")%>
5、out:
out.write()与response.getWrite().write()区别
>out.write():先输出固定大小的缓冲区,最后刷入到response中让服务器获取
>response.getWrite().write():直接刷入
6、等价:
request.setAttribute(“name”,”zhang”)
||
pageContext.setAttribute(“n”, “li”,pageContext.REQUEST_SCOPE); request.getAttribute(“name”);
||
pageContext.getAttribute(“name”,pageContext.REQUEST_SCOPE)`
7、作用域的大小:
page<request<session<application
注:调试时通常用out.println(),或断点
8、请求转发与重定向:
转发:
String path="";
RequestDispatcher requestDispatcher=request.getRequestDispatcher("/"+path);
requestDispatcher.forward(request,response)
重定向:
String path="";
response.sendRedirect(path);
区别:
请求转发:URL不变,request始终为同一个对象,当前web资源,"/"代表当前
web应用的根目录
重定向:URL变为响应的新地址,新的request对象,可访问任意资源,"/"代表
当前web站点的根目录
9、jsp指令:
<%@ include、page、taglib %>
1、page指令
设置当前页面session是否可用:
session="true/false"
<%=session.getId() %>
设置错误页面:
errorpage="/error.jsp"出错的页面
isErrorPage="true":转到的页面,通过exception.getMessage()获取异常
导入java类:
import
设置发送的页面格式及编码:
response.setContextType("text/html;charset="UTF-8")
设置当前页面编码:
pageEncoding
2、include指令
静态引入(形成一个文件):
<%@ include file="b.jsp" %>(源码包含,头部合并形成class文件)
<%@ include file="jsp\html\java"%>
动态引入(形成两个文件):
<jsp:include page="b.jsp"></jsp:include>
注:jsp页面的转发方式
<jsp:forward page="/b.jsp"></jsp:include>
<%
request.getRequestDispatcher("/b.jsp").forward(request,response);
%>
3、taglib指令
<%@ taglib prefix="fix前缀" uri="自定义标签位置"%>
10、中文乱码:
设置编码:
pageEncoding、charset
post请求的编码格式设定:
<% request.setCharacterEncoding("UTF-8"); %>
<%=request.getParameter("username")%>
get请求:
String val=request.getParameter("username");
String user=new String(val.getBytes("ISO-8859-1"),"UTF-8")
out.print(user)
11、注释:
jsp注释:
<%----%>
html注释:
<!---->
12、EL
<c:set var="a" value="5"/>
<c:out value="${a}"/>
<c:if test="${1>2}">
<c:out value="1大于2"/>
循环:
<c:foreach begin="0" end="8" varStatus="i">
<c:out value="${i.index}">
</c:foreach>
数组(集合循环类似):
String [] arrs={"gh","jk"};
request.setAttribute("arrs",arrs);
<c:foraech items="$arrs" var="arr">
<c:out value="${arr}">
页面定时刷新:
<%Thread.sleep(5000);%><br/>
5、动作标签:
参数传递
<jsp:param name="paramname" value="paramvalue">
实例化JavaBean
<jsp:useBean id="Bean" scope="page" class="lin.Bean"/>
<jsp:setProperty>\<jsp:getProperty>
如下:
<jsp:setProperty name="id" property="password" value="123"/>
<jsp:getProperty name="id" property="password"/>
servlet过滤器
过滤器接口
Filter:init\doFilter\destroy
FilterChain:doFilter
FilterConfig
Servlet监听器
1、上下文监听
2、监听HTTP
HttpSession操作及属性操作、HTTP的active与passivate
3、客户端请求监听
4、HttpSessionBindingLister接口对象的Session属性绑定
本文详细介绍JSP的基础概念,包括JSP文件的编译过程、配置全局错误页面的方法、JSP页面中的九个内置对象及其使用方式,并对比了请求转发与重定向的区别。
208

被折叠的 条评论
为什么被折叠?



