客户端向服务器发送请求,服务器将请求进行转发,获得响应信息,客户端只发送一次请求,地址栏信息不变。

服务器接收类,进行转发

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中的转发的更多相关文章

  1. Servlet中的转发

    public class OneServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServ ...

  2. Servlet中的转发与重定向

    Sevlet 的转发与重定向都可以使得浏览器指向另一个资源文件,但它们的运行机制不相同. 一.Servlet的转发 有两种方式获得转发对象(RequestDispathcer): HttpServle ...

  3. Java Web中请求转发和请求包含

    1.都是在一个请求中跨越多个Servlet 2.多个Servlet在一个请求中,他们共享request对象.就是在AServle中setAttribute()保存数据在BServlet中由getAtt ...

  4. servlet中请求转发(forword)与重定向(sendredirect)的区别

    摘自:http://www.cnblogs.com/CodeGuy/archive/2012/02/13/2349970.html 通俗易懂 servlet请求转发与重定向的区别: request.s ...

  5. servlet中的转发和重定向问题

    重定向和请求转发在学习servlet的时候很容易混淆,故在此特意记录. 1. 重定向---------sendRedirect()方法 Servlet响应请求有两种方式,一个是重定向,返回一个页面给客 ...

  6. java web 中的转发和重定向

    假设应用程序的 contextPath 为 /ctx,在 http://localhost:8080/ctx/a/b 资源中,我们转发和重定向到 http://localhost:8080/ctx/x ...

  7. servlet中请求转发(forword)与重定向(sendredirect)

    请求转发和重定向 request.setAttribute("test","hello"); request.getRequestDispacther(&quo ...

  8. servlet中请求转发获取数据等,,,

    String uname= req.getParameter("uname");  获取请求的字符串 req.setAttribute("str"," ...

  9. struts2 中请求转发与请求重定向方法

    本文转自:http://blog.csdn.net/a327736051/article/details/50240491 一.Chain Result:这个result调用另外的一个action,连 ...

随机推荐

  1. CentOS安装nvidia显卡驱动

    1.下载 nvidia 相应的驱动: 2.修改/etc/modprobe.d/blacklist.conf文件,在里面加入blacklist nouveau. 3.重建image $ mv /boot ...

  2. 关于图像读取函数imread()的一点使用经验,注意默认参数的赋值

    读入数字图像到数组,用CNN进行训练,发现关于图像读取的一个问题. 问题描述:读取灰度数字图像,在验证时发现存在错误,从图像到数组中的值不完全一样? main code as follows: int ...

  3. HW5.27

    public class Solution { public static void main(String[] args) { int totalCount = 0; int lineCount = ...

  4. leetcode@ [134] Gas station (Dynamic Programming)

    https://leetcode.com/problems/gas-station/ 题目: There are N gas stations along a circular route, wher ...

  5. RDMA编程实例

    1,RDMA verbs Multicast Code for Multicast Using RDMA_CM(Remote directory memory access_connect manag ...

  6. 转载Entity Framework 5.0(EF first)中的添加,删除,修改,查询,状态跟踪操作

    转载原出处:http://www.cnblogs.com/kenshincui/p/3345586.html Entity Framework将概念模型中定义的实体和关系映射到数据源,利用实体框架可以 ...

  7. 7个改变世界的Java项目

    Java的开源生态系统是强大而健康的,这是我们(Oreilly)创建OSCON Java(Open Source Convention Java)的主要原因之一.在过去10年中,一些项目已经被广泛接受 ...

  8. Packetbeat协议扩展开发教程(3)

    原文链接:http://elasticsearch.cn/article/54 书接上回:http://elasticsearch.cn/article/53 前面介绍了Packetbeat的项目结构 ...

  9. JAVA的设计模式之单例设计模式

    1.确保一个类只有一个实例,自行提供这个实例并向整个系统提供这个实例. 1)理论 Java Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 使用Singl ...

  10. C++学习笔记(二):基本数据类型

    带符号整数: short至少16位: int至少与short—样长: long至少32位,且至少与int—样长: long long至少64位,且至少与long—样长: 无符号整数: unsigned ...