jsp内置对象
来源:互联网 发布:数据标准管理需求 编辑:程序博客网 时间:2024/05/21 19:29
jsp内置对象
共9个内置对象,web容器会为用户准备好内置对象
一.四种属性范围
pageContext:只在一个页面中保存属性,跳转之后无效
request:只在一次请求中保存,服务器跳转后依然有效
session:在一次会话范围中,无论何种跳转都可以使用,但是新开浏览器无法使用
application:在整个服务器上保存,所有用户都可以使用
四种属性的操作方法
pageContext中还有一种属性设置方法:
public void setAttribute(String name,Object value,int scope)
scope取不同的值对应不同的属性范围:
public static final int PAGE_SCOPE
public static final int REQUEST_SCOPE
public static final int SESSION_SCOPE
public static final int APPLICATION_SCOPE
同时有getAttribute(String name,int scope),removeAttribute(String name,int scope)
二.request对象
(1)、用来接受客户端发送而来的请求信息
对应接口:public interface HttpServletRequest extends ServletRequset
<%request.setCharacterEncoding("GBK");//设置编码String a=request.getParameter("属性名称");//接受传递的属性内容String b[] =request.getParameterValues("属性名称");//接受传递的一组属性内容,输出时需要判断b是否为空Enumeration enu=request.getParameterNames();//需要import java.util.*,用于接受属性名称,一般是在表单不确定的时候使用while(enu.hasMoreElements()){ String c=(String)enu.nextElement(); out.print(c);//输出属性名称 out.pirntln(request.getParameter(c));//输出属性内容}%>
(2)、显示头信息
<%Enumeration enu=request.getHeaderNames();//取得全部头信息while(enu.hasMoreElements()){ String headername=(String)enu.nextElement(); String headervalue=request.getHeader(headername); out.print(headername); out.println(headervalue);//输出头信息内容}%>
(3)、其他
String method=request.getMothod();//请求方式String ip=request.getRemoteAddr();//IP地址String path=request.getServletPath();//访问路径String contextPath=request.getContextPath();//上下文名称
三.response对象
(1)、用于服务器对客户端的回应
对应接口:public interface HttpServletResponse extends ServletResponse
(2)、response对象的常用方法
(3)、设置头信息
<%response.setHeader("refresh","时间");//设置页面每隔一定时间刷新response.setHeader("refresh","时间;URL=要跳转的页面");//隔一定时间后跳转%>
(4)、页面跳转
<%response.sendRedirect("要跳转的页面");%>
(5)、操作Cookie
Cookie定义的常用方法
<%Cookie c=new Cookie("属性名称","属性内容");//设置Cookie信息response.addCookie(c);//添加Cookiec.setMaxAge(1000);//设置Cookie的保存时间。若未设置Cookie保存时间,默认是在一个浏览器上保存的,关闭浏览器则Cookie消失Cookie cs[]=request.getCookies();//取得客户端的全部Cookiefor(int i=0;x<c.length;x++){ out.print(cs[i].getName());//输出Cookie名称 out.println(cs[i].getValue());//输出Cookie内容}%>
四.session对象
是javax.servlet.http.HttpSession接口的实例
(1).HttpSession接口的主要方法
五、application对象
是javax.servlet.ServletContext接口的实例,表示整个Servlet的上下文
方法|类型|描述
String getRealPath(String path)|普通|得到虚拟目录对应的绝对路径
public Enumeration getAttributeName()|普通|得到所有属性的名称
public String getContextPath()|普通|取得当前的虚拟路径名称
(1).取得绝对路径
String path=application.getRealPath("/");//与this.getServletContext().getRealPath("/");效果相同
request.setCharacterEncoding("GBK");String name=request.getParameter("filename");String content=request.getParameter("filecontent");String filename=this.getServletContext().getRealPath("/")+"note"+File.separator+name;//保存在note文件夹中File file=new File(filename);if(!file.getParentFile().exists()){ file.getParentFile().mkdir();//建立一个文件夹}PrintStream ps=null;ps=new PrintStream(new FileOutputStream(file));ps.println(content);ps.close();Scanner scan=new Scanner(new FileInputStream(file));//读文件scan.useDelimiter("\n");StringBuffer buf=new StringBuffer();while(scan.hasNext()){ buf.append(scan.next()).append("<br>");}scan.close();out.print(buf);
(2).取得全部属性
Enumeration enu=this.getServletContext().getAttributeName();while(enu.hasMoreElements()){ String name=(String)enu.nextElement(); String content=this.getServletContext().getAttribute(name);}
- JSP - JSP内置对象
- JSP--JSP内置对象
- JSP内置对象
- JSP内置对象
- JSP内置对象
- JSP内置对象
- jsp内置对象
- JSP 内置对象
- JSP内置对象
- JSP内置对象
- JSP内置对象
- JSP的内置对象
- jsp内置对象集合
- jsp内置对象
- JSP内置对象
- jsp内置对象
- JSP内置对象
- JSP的内置对象
- Shell字符串比较
- MySQL5.5.28的安装及mysql-font的安装
- 【ZOJ3932 The 16th Zhejiang University Programming ContestF】【水题】Handshakes 每人与之前认识的人握手 最大可能认识人数
- Oracle11g RMAN 丢失归档日志:ORA-19625
- Linux编程,signal
- jsp内置对象
- Ubuntu下载安装运行shadowsocks
- 解决iOS开发中删除Main.storyboard后黑屏的现象
- MySQL Flush命令用法
- 关于android实现fastindexbar(快速索引)详解
- 【程序的流程】—— 顺序 / 分支 / 循环
- 【ZOJ3933 The 16th Zhejiang University Programming ContestG】【费用流】Team Formation 最多组队条件下女
- UVA10025
- mongoDB-java