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

服务器接收类,进行转发

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. jsp:forward与缓冲区

    jsp:forward的作用是在服务器端进行页面跳转.通常有<jsp:forward page="NewPage.jsp">语句的页面的在执行时会提前执行跳转,而不输出 ...

  2. 命令rm

    mv -r 递归删除文件夹内所有东西mv -i 交互式删除mv -f 强制删除,没有警告提示

  3. n & (n-1)

    n&(n-1)作用:将n的二进制表示中的最低位为1的改为0,先看一个简单的例子: n = 10100(二进制),则(n-1) = 10011 ==>n&(n-1) = 10000 ...

  4. window2008 64位系统没有office组件问题分析及解决

    服务器是windows server2008 64位系统, 我的系统需要用到Microsoft.Office.Interop.Excel组件 在上传Excel单据遇到错误:检索 COM 类工厂中 CL ...

  5. php框架Symfony资料

    1.http://snippets.symfony-project.org/snippets/tagged/criteria/order_by/date 2.Propel API: http://ap ...

  6. 加一个 时间戳 TimeStamp 可以解决 重复提交问题 SqlServer

     public partial class Form1 : Form{private SqlConnection mCnn = null;private long TimeStampValue; pu ...

  7. Delphi监视进程并结束进程

    监视进程并结束进程在很多地方都用到这里借前人的经验写了个小例子: 以QQ的进程qq.exe为例 关键代码如下: function CheckTask(ExeFileName: string): Boo ...

  8. hdu 1437 天气情况【概率DP】

    天气情况 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. [六]SpringMvc学习-文件上传

    1.单文件上传 1.1修改配置文件 <bean id="multipartResolver" class="org.springframework.web.mult ...

  10. oscgit

    Gitlab PaaS项目演示 git config --global user.name "你的名字或昵称" git config --global user.email &qu ...