首页 技术 正文
技术 2022年11月6日
0 收藏 560 点赞 982 浏览 5342 个字

1. 和属性相关的方法:

①. 方法

void setAttribute(String name, Object o): 设置属性

Object getAttribute(String name): 获取指定的属性

Enumeration getAttributeNames(): 获取所有的属性的名字组成的 Enumeration 对象

removeAttribute(String name): 移除指定的属性

②. pageContext, request, session, application 对象都有这些方法,

     这四个对象也称之为域对象.

pageContext: 属性的作用范围仅限于当前 JSP 页面

request: 属性的作用范围仅限于同一个请求.

session: 属性的作用范围限于一次会话: 浏览器打开直到关闭称之为一次会话(在此期间会话不失效)

application: 属性的作用范围限于当前 WEB 应用. 是范围最大的属性作用范围,

只要在一处设置属性, 在其他各处的 JSP 或 Servlet 中都可以获取到.

实验:

attr.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--
和属性相关的方法
void setAttribute(String name, Object o): 设置属性
Object getAttribute(String name): 获取指定的属性Enumeration getAttributeNames(): 获取所有的属性的名字组成的 Enumeration 对象
removeAttribute(String name): 移除指定的属性pageContext request session application 对象都有这些方法
这四个对象也称为域对象pageContext: 属性的作用范围仅限于当前 JSP 页面
request: 属性的作用范围仅限于同一个请求.
session: 属性的作用范围限于一次会话: 浏览器打开直到关闭称之为一次会话(在此期间会话不失效)
application: 属性的作用范围限于当前 WEB 应用. 是范围最大的属性作用范围, 只要在一处设置属性, 在其他各处的 JSP 或 Servlet 中
都可以获取到.
--> <%
//设置属性
pageContext.setAttribute("pageContextAttr", "pageContextValue");
request.setAttribute("requestAttr", "requestValue");
session.setAttribute("sessionAttr", "sessionValue");
application.setAttribute("applicationAttr", "applicationValue");
%>
<h2>Attr 1 Page</h2>
<br><br>
pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
<br><br>
requestAttr: <%=request.getAttribute("requestAttr") %>
<br><br>
sessionAttr:<%=session.getAttribute("sessionAttr") %>
<br><br>
aplicationAttr:<%=application.getAttribute("applicationAttr") %>
<br><br> <a href="arrt_2.jsp" rel="external nofollow" >To Attr 2 Page</a></body>
</html>

attr_2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>Attr 2 Page</h2>
<br><br>
pageContextAttr: <%=pageContext.getAttribute("pageContextAttr") %>
<br><br>
requestAttr: <%=request.getAttribute("requestAttr") %>
<br><br>
sessionAttr:<%=session.getAttribute("sessionAttr") %>
<br><br>
aplicationAttr:<%=application.getAttribute("applicationAttr") %>
<br><br></body>
</html>

2. 请求的转发和重定向:

本质区别: 请求的转发只发出了一次请求, 而重定向则发出了两次请求.

具体:

①. 请求的转发: 地址栏是初次发出请求的地址.
请求的重定向: 地址栏不再是初次发出的请求地址. 地址栏为最后响应的那个地址

②. 请求转发: 在最终的 Servlet 中, request 对象和中转的那个 request 是同一个对象.
请求的重定向: 在最终的 Servlet 中, request 对象和中转的那个 request 不是同一个对象.

③. 请求的转发: 只能转发给当前 WEB 应用的的资源
请求的重定向: 可以重定向到任何资源.

④. 请求的转发: / 代表的是当前 WEB 应用的根目录
请求的重定向: / 代表的是当前 WEB 站点的根目录.

test.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><!-- 请求的转发和重定向
本质区别: 请求的转发只发出一次请求,而重定向则发出了两次请求
具体:
①.请求的转发: 地址栏是初次发出的地址,
请求的重定向:地址栏不再是初次发出请求的地址,而是最后响应的那个地址
②.请求转发:在最终的Servlet中,request 对象和中转的那个 request 是同一个对象
请求重定向:在最终的Servlet中,request 对象和中转的那个 request 不是同一个对象
③.请求的转发:只能转发给当前的 WEB 应用的资源
请求的重定向:可以重定向到任何资源,例如:
response.sendRedirect("http://www.baidu.com");
④.请求的转发: / 代表的是当前 WEB 应用的根目录 http://localhost:9090/WebT2/
请求的重定向: / 代表的是当前 WEB 站点的根目录 http://localhost:9090/
-->
<a href="loginServlet" rel="external nofollow" >Test</a>
<br/><br/>
<a href="forwardServlet" rel="external nofollow" >Forward</a>
<br/><br/>
<a href="redirectServlet" rel="external nofollow" >Redirect</a>
<br/><br/>
</body>
</html>

ForwardServlet

package com.aff.java;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/forwardServlet")
public class ForwardServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ForwardServlet's doGet 方法");
// 请求的转发
// 1. 调用HttpServletRequest 的 getRequestDispatch()
// 方法获取RequestDispatcher对象
// 调用 getRequestDispatch 需要传入转发的地址 String path = "testServlet";
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/" + path);
// /代表当前web应用的根目录 // 2. 调用HttpServletRequest的forward(request,response)进行请求的转发
requestDispatcher.forward(request, response); }}

RedirectServlet

package com.aff.java;import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/redirectServlet")
public class RedirectServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("redirectServlet's do Get 方法");
// 执行请求的重定向,直接调用 response.sendRedirect(path);
// path为要重新向的地址
String path = "testServlet";
response.sendRedirect(path);
}}

TestServlet

package com.aff.java;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;@WebServlet("/testServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("TestServlet's do Get 方法");
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900