JSP技术(二)
参考网址:https://blog.csdn.net/king_cannon_fodder/article/details/79835463
(1)JSP隐式对象(9个内置对象)
Servlet容器会传递几个对象给它运行的Servlet。例如,可以通过Servlet的service方法拿到HttpServletRequest和HttpServletResponse对象,以及可以通过init方法访问到ServletConfig对象。此外,可以通过调用HttpServletRequest对象的getSession方法访问到HttpSession对象。
在JSP中,可以通过使用隐式对象来访问上述对象。
对象 | 类型 |
request | javax.servlet.http.HttpServletRequest |
response | javax.servlet.http.HttpServletResponse |
out | javax.servlet.jsp.JspWriter |
session | javax.servlet.http.HttpSession |
application | javax.servlet.ServletContext |
config | javax.servlet.ServletConfig |
pageContext | javax.servlet.jsp.PageContext |
page | javax.servlet.jsp.HttpJspPage |
exception | java.lang.Throwable |
在JSP中无需创建就可以使用的9个对象,它们是:
l out(JspWriter):等同与response.getWriter(),用来向客户端发送文本数据;
l config(ServletConfig):对应“真身”中的ServletConfig;
l page(当前JSP的真身类型):当前JSP页面的“this”,即当前对象;
l pageContext(PageContext):页面上下文对象,它是最后一个没讲的域对象;
l exception(Throwable):只有在错误页面中可以使用这个对象;
l request(HttpServletRequest):即HttpServletRequest类的对象;
l response(HttpServletResponse):即HttpServletResponse类的对象;
l application(ServletContext):即ServletContext类的对象;
l session(HttpSession):即HttpSession类的对象,不是每个JSP页面中都可以使用,如果在某个JSP页面中设置<%@page session=”false”%>,说明这个页面不能使用session。
(2)request、application、session对象
具体介绍一下这三个对象的使用
例如,从HttpServletRequest对象中返回username参数值:
1 2 3 | <% String userName=request.getParameter( "userName" ); %> |
index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 | <body> <% request.setAttribute( "p1" , "request" ); session.setAttribute( "p2" , "session" ); application.setAttribute( "p3" , "application" ); request.getRequestDispatcher( "/2.jsp" ).forward(request, response); %> //这里使用El表达式取值 ${requestScope.p1} ${session.p2} ${application.p3} </body> |
2.jsp
1 2 3 4 5 6 7 8 9 10 | <body> <% out.print(request.getAttribute( "p1" )); out.println(); out.print(session.getAttribute( "p2" )); out.println(); out.print(application.getAttribute( "p3" )); out.println(); %> </body> |
运行效果:
index.jsp
2.jsp
可以看到,application和session都可以取到index.jsp页面传过来的值,为什么request取不到值?
因为request对象既是jsp的内置对象,也是jsp的作用域之一。
此处遇到的错误原因跟request的作用域有关。在index.jsp中使用setAttribute,在当前页面index.jsp 里可以使用getAttribute来取到其值。但在2.jsp中就不行了,因为request过期了。
若想让参数在整个项目过程中前后可用,应将参数存在会话(Session)里。
部分隐式对象的使用实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <body> <% for (Enumeration<String> e=request.getHeaderNames();e.hasMoreElements();) { String header=e.nextElement(); out .println(header+ ":" +request.getHeader(header)+ "<br/>" ); } out .println( "Buffer size:" +response.getBufferSize()+ "<br/>" ); out .println( "Session id:" +session.getId()+ "<br/>" ); out .println( "Servlet name:" +config.getServletName()+ "<br/>" ); out .println( "Servle info:" +application.getServerInfo()); %> </body> |
实例运行效果:
(3)PageContext
pageContext用于javax.servlet.jsp.PageContext。它提供了有用的上下文信息,并通过其自说明方法来访问各种Servlet相关对象。如getRequest、getResponse、getServletContext、getServletConfig和getSession。我们也可以直接使用隐式对象来访问request、response、session、application。
PageContext也提供了一组用于获取和设置属性的方法,即getAttribute和setAttribute方法。属性的值可以存储在4个范围之一:页面、请求、会话和应用程序。最小的范围是页面范围,存储的属性只可以本页面使用。请求范围是指当前的ServletRequest中。会话范围指当前的HttpSession中。应用程序范围指应用的ServletContext中。
PageContext的setAttribute方法签名如下:
public abstract void setAttribute(java.lang.String name,java.lang.Object value,int scope)
scope的取值范围为PageContext对象的最终静态int值:PAGE_SCOPE、REQUEST_SCOPE、SESSION_SCOPE和APPLICATION_SCOPE。
pageContext.setAttribute("x", "X");
pageContext.setAttribute("x", "XX",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("x", "XXX",PageContext.SESSION_SCOPE);
pageContext.setAttribute("x", "XXXX", PageContext.APPLICATION_SCOPE);
void setAttribute(String name, Object value, int scope):在指定范围中添加数据;
Object getAttribute(String name, int scope):获取指定范围的数据;
void removeAttribute(String name, int scope):移除指定范围的数据
实例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <body> <% pageContext.setAttribute( "p1" , "pp" ,PageContext.PAGE_SCOPE); pageContext.setAttribute( "p2" , "pp" , PageContext.REQUEST_SCOPE); pageContext.setAttribute( "p3" , "pp" , PageContext.APPLICATION_SCOPE); pageContext.setAttribute( "p4" , "pp" ,PageContext.SESSION_SCOPE); String p1=(String)pageContext.getAttribute( "p1" ); out.println(p1); String str1=(String)pageContext.getRequest().getAttribute( "p2" ); out.println(str1); String str2=(String)pageContext.getServletContext().getAttribute( "p3" ); out.println(str2); String str3=(String)pageContext.getSession().getAttribute( "p4" ); out.println(str3); %> </body> |
运行效果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)