ServletContext中的转发
客户端向服务器发送请求,服务器将请求进行转发,获得响应信息,客户端只发送一次请求,地址栏信息不变。
服务器接收类,进行转发
package com.itheima.zhuanfa; import java.io.IOException; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class ServletContextDemo3 extends HttpServlet{ @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 转发
//获得ServletContext对象
ServletContext sc=getServletContext();
System.out.println("转发前");
resp.getOutputStream().write("before".getBytes());
//获得转发器
RequestDispatcher rd=sc.getRequestDispatcher("/Demo4");//得到请求转发器
rd.forward(req,resp);
/*
* 转发的特点:请求的地址栏不变,两者共享request和response对象
* 转发前和转发后的页面输出都不会发送到客户端
* 转发前不要刷新response中的内容,会吧response中的内容清空
*/
System.out.println("转发后");
resp.getOutputStream().write("after".getBytes()); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { } }
转发到的类把这个类的信息传到客户端
package com.itheima.zhuanfa; 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 ServletContextDemo4 extends HttpServlet { @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getOutputStream().write("转发成功,我来自Demo4".getBytes());
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { } }
ServletContext中的转发的更多相关文章
- Servlet中的转发
public class OneServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServ ...
- Servlet中的转发与重定向
Sevlet 的转发与重定向都可以使得浏览器指向另一个资源文件,但它们的运行机制不相同. 一.Servlet的转发 有两种方式获得转发对象(RequestDispathcer): HttpServle ...
- Java Web中请求转发和请求包含
1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...
- servlet中请求转发(forword)与重定向(sendredirect)的区别
摘自:http://www.cnblogs.com/CodeGuy/archive/2012/02/13/2349970.html 通俗易懂 servlet请求转发与重定向的区别: request.s ...
- servlet中的转发和重定向问题
重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...
- java web 中的转发和重定向
假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...
- servlet中请求转发(forword)与重定向(sendredirect)
请求转发和重定向 request.setAttribute("test","hello"); request.getRequestDispacther(&quo ...
- servlet中请求转发获取数据等,,,
String uname= req.getParameter("uname"); 获取请求的字符串 req.setAttribute("str"," ...
- struts2 中请求转发与请求重定向方法
本文转自:http://blog.csdn.net/a327736051/article/details/50240491 一.Chain Result:这个result调用另外的一个action,连 ...
随机推荐
- 关于Java线程
1 概念 通常来说,我们编写的Java代码是以进程的形式来运行的,所编写的代码就是“程序”,而执行中的程序就是“进程”.进程是系统进行资源分配和调度的独立单位. 线程是位于进程的下一级,是系统中的最小 ...
- localtime()方法的疑惑
在做一个时间管理的APP中遇到一些问题 windows linux mac下time.h中都有关于localtime()的定义. 它不是一个保险可靠的方法,使用的时候需要小心. 参考 http://b ...
- 【转】 hive简介,安装 配置常见问题和例子
原文来自: http://blog.csdn.net/zhumin726/article/details/8027802 1 HIVE概述 Hive是基于Hadoop的一个数据仓库工具,可以将结构化 ...
- Hadoop概念学习系列之hadoop、spark常备查询网址(二十九)
http://archive.apache.org/dist
- ubuntu完全卸载一个软件
今天卸载一个软件,老是有配置残留,网上找到了解决方案: 查看已安装的软件: dpkg -l |grep 软件名 找到一大堆相关的包,然后卸载核心的包: sudo apt-get remove --pu ...
- AutoLayout框架Masonry使用心得
AutoLayout框架Masonry使用心得 字数1769 阅读1481 评论1 喜欢17 我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayout,会后我决定将很久前挖的一个坑给填起 ...
- UITextView详解
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease]; //初始化大小并自动释放 sel ...
- ASP.NET中配置应用程序
1. 配置文件简介 1.1 分类 1.2关系 Machine.Config和Web.Config都是设置应用程序的配置信息,它们按照类似于继承的关系对应用程序起作用. Machine.Config ...
- Windows 环境下基于 Redis 的 Celery 任务调度模块的实现
搭建环境: Windows-x64 10 Celery 3.1.23 Celery-with-redis 3.0 Redis-win32-win64 2.4.5 实现步骤: 1.安装 Redis ...
- linux快速入门 1.1命令行操作
http://lovesoo.org/linux-command-line-operation.html 1.1命令行操作 目录: <wp_nokeywordlink>Shell简介 &l ...