JavaWeb学习-Servlet系列-24-Request请求转发和获取非表单数据

前面介绍Request对象获取表单数据,这篇先来介绍下请求的转发,然后介绍几个方法,是关于获取非表单数据,就是请求过程中设置属性,获取属性,删除属性这三个方法。

1.请求转发

准备两个ServletDemo对象,分别请求URI是/demo3和/demo4, 在Demo3中请求转发到Demo4处理。

ServletDemo3.java

package com.anthony.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletDemo3 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		
		//模拟生活中办事场景
		System.out.println("A:我要办事!");
		System.out.println("B:这事我办不了,但我可以找人帮你办!");
		//请求转发
		req.getRequestDispatcher("/demo4").forward(req, resp);
		System.out.println("B:事情办完了!");
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

ServletDemo4.java

package com.anthony.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletDemo4 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		System.out.println("C:这事我能办!");
		
	}


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

测试运行下

A:我要办事!
B:这事我办不了,但我可以找人帮你办!
C:这事我能办!
B:事情办完了!

浏览器就请求了/demo3地址,然后控制台输出以上信息。说起,请求转发,作为浏览器来讲只是请求了一次。自己对照代码和控制台输出顺序,思考下这个场景。

2.获取非表单数据

例如在Demo3中设置一个属性key1, 然后再Demo4中获取属性key1, 然后移除属性key1

package com.anthony.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletDemo3 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置编码
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		//设置一个属性
		String str1 = "111111";
		req.setAttribute("key1", str1);
		//模拟生活中办事场景
		System.out.println("A:我要办事!");
		System.out.println("B:这事我办不了,但我可以找人帮你办!");
		//请求转发
		req.getRequestDispatcher("/demo4").forward(req, resp);
		System.out.println("B:事情办完了!");
		
	}
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

Demo4.java

package com.anthony.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ServletDemo4 extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html;charset=UTF-8");
		System.out.println("C:这事我能办!");
		String key = (String)req.getAttribute("key1");
		System.out.println(key);
		//移除属性key1
		req.removeAttribute("key1");
		System.out.println((String)req.getAttribute("key1"));
	}


	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
	
}

重新部署,运行结果

A:我要办事!
B:这事我办不了,但我可以找人帮你办!
C:这事我能办!
111111
null
B:事情办完了!

3.准发和重定向区别

1)请求转发浏览器地址不变,重定向浏览器地址会变

2)请求转发只请求了一次,重定向请求了两次

3)请求转发能够传递数据,响应重定向不能传递数据

4)请求转发不能跳转到其他应用,例如不能跳转到百度,响应重定向可以到外部应用。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值