博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web--Request对象传递数据、转发与重定向的区别
阅读量:2442 次
发布时间:2019-05-10

本文共 3966 字,大约阅读时间需要 13 分钟。

通过request对象传递数据

Request不仅可以获取一系列数据,还可以通过属性传递数据。在ServletRequest接口中,定义了一系列操作属性的方法,具体如下:

  • setAttrbute()方法:
    该方法用于将一个对象与一个名称关联后存储进ServletRequest对象中,其完整语法定义如下:
public void setAttribute(String name, Object o);

需要注意的是,如果ServeltRequest对象中已经存在指定名称的属性,setAttribute()方法将会先删除原来的属性,然后再添加新的属性。如果传递给setAttribute()方法的属性值对象为null,则删除指定名称的属性,这时的效果等同于removeAttribute()方法。

  • getAttribute()方法:
    该方法用于从ServletRequest对象中返回指定名称的属性,其完整的语法定义如下:
public Object getAttribute(String name);
  • removeAttribute()方法:
    该方法用于从ServletRequest对象中删除指定名称的属性,其完整的语法定义如下:
 
  • getAttributeNames()方法:
    该方法用于返回一个包含ServletRequest对象中的所有属性名的Enumeration对象,再此基础上,可以对ServletRequest对象中的所有属性进行遍历处理。getAttributeNames()方法的完整语法定义如下:
public Enumeration getAttributeNames();

注 意 : 只 有 属 于 同 一 个 请 求 中 的 数 据 才 可 以 通 过 S e r v l e t R e q u e s t 对 象 传 递 数 据 。 \color{red}{注意:只有属于同一个请求中的数据才可以通过ServletRequest对象传递数据。} ServletRequest

示例:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//向request中存入数据 request.setAttribute("str1","helloWorld1"); request.setAttribute("str2","helloWorld2"); request.setAttribute("str3","helloWorld3"); //从request对象中获取数据 String str1 = (String) request.getAttribute("str1"); System.out.println(str1); //从request对象中删除数据 request.removeAttribute("str1"); //返回所有属性名,并遍历 Enumeration attributeNames = request.getAttributeNames(); while (attributeNames.hasMoreElements()){
String str = (String) request.getAttribute((String) attributeNames.nextElement()); System.out.println(str); } }

浏览器访问,控制台输出:

helloWorld1helloWorld3helloWorld2

request请求转发

在Servlet中,如果当前Web资源不想处理请求时,可以通过forward()方法将当前请求转发给其它的Web资源进行处理,这种方式称为请求转发。为了更好的理解使用forward()方法实现请求转发的工作原理,下面通过一张图来描述:

在这里插入图片描述
从图中可以看出,当客户端访问Servlet1时,可以通过forward()方法将请求转发给其它Web资源,其它Web资源处理完请求后,直接将响应结果返回到客户端。

示例:表单提交到AttributeServlet,AttributeServlet转发到RequestServlet,RequestServlet获取表单数据然后打印 :

表单:

用户名:
密  码:
爱  好:
唱歌
跳舞
足球

AttributeServlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//转发到RequestServlet request.getRequestDispatcher("/request").forward(request, response); }

RequestServlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username"); username = new String(username.getBytes("iso8859-1"), "utf-8"); String password = request.getParameter("password"); System.out.println("用户名:" + username); System.out.println("密码:" + password); //获取参数名为hobby的值 String[] hobbys = request.getParameterValues("hobby"); System.out.println("爱好:"); for (int i = 0; i < hobbys.length; i++) {
System.out.println(hobbys[i] + ", "); } }

通过浏览器访问表单页面填写数据并提交:

在这里插入图片描述
控制台打印:

用户名:tom密码:123456爱好:sing, dance, football,

response重定向

当文档移动到新的位置,我们需要向客户端发送这个新位置时,我们需要用到网页重定向。当然,也可能是为了负载均衡,或者只是为了简单的随机,这些情况都有可能用到网页重定向。

重定向请求到另一个网页的最简单的方式是使用 response 对象的 sendRedirect() 方法。下面是该方法的定义:

public void HttpServletResponse.sendRedirect(String location)throws IOException

该方法把响应连同状态码和新的网页位置发送回浏览器。您也可以通过把 setStatus() 和 setHeader() 方法一起使用来达到同样的效果:

....String site = "http://www.runoob.com" ;response.setStatus(response.SC_MOVED_TEMPORARILY);response.setHeader("Location", site); ....

示例:访问AttributeServlet然后重定向到表单页面

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//重定向到form表单页面 response.sendRedirect(getServletContext().getContextPath() + "/form.jsp"); }

通过浏览器访问AttributeServlet,页面显示:

在这里插入图片描述

转发和重定向的区别(redirect和forward的区别)

在这里插入图片描述

  1. 重定向的地址栏会发生变化,转发的地址栏不变
  2. 重定向为两次请求两次响应,转发为一次请求一次响应
  3. 重定向路径需要加工程名,转发的路径不需要加工程名
  4. 重定向可以跳转到任意网站,转发只能在服务器内部进行转发

转载地址:http://mnsqb.baihongyu.com/

你可能感兴趣的文章
ipv6强制加密_为什么“物联网”强制需要IPv6地址?
查看>>
谷歌gmail注册入口_如何阻止Gmail将事件添加到Google日历
查看>>
sonos 服务器_如何关闭Sonos播放器上的LED
查看>>
微软edge如何退出登录_如何在Microsoft Edge中为以后设置选项卡
查看>>
charles 排除 pc_如何使Windows为您排除PC问题
查看>>
如何在Windows,Mac或Linux上编辑主机文件
查看>>
photoshop文件巨大_如何手动清除Photoshop的大量临时文件
查看>>
android怎么卸载应用_如何在Android TV上卸载应用
查看>>
twitter账户受限_如何将您的Twitter帐户设为私有
查看>>
开县影剧院座次_如何使用剧院模式关闭Apple Watch的屏幕
查看>>
安装浏览器扩展程序_如何查看所有浏览器中安装的扩展列表
查看>>
nest keyword_如何将Nest Thermostat预设添加到手机的主屏幕
查看>>
android强制全屏_如何强制任何Android应用进入全屏浸入模式(无生根)
查看>>
如何将巢式恒温器从加热切换到冷却(反之亦然)
查看>>
safari chrome_Mac用户应放弃Safari的Google Chrome浏览器
查看>>
cpu导热膏安装_我应该在CPU上涂抹多少导热膏?
查看>>
kickass替代_如何为色相灯设置Kickass派对模式
查看>>
您可以在PlayStation 4上使用的30种语音命令
查看>>
febe_如何使用FEBE备份和恢复Firefox配置文件
查看>>
如何在iPhone上查看(和删除)链接到Apple ID的设备
查看>>