Servlet转发
可以使用ServletContext中的getRequestDispatcher(url).forward(request, response)方法进行转发
myservlet2.java
public class myservlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("我要办事");
System.out.println("你的事我办不了");
System.out.println("你的事我找人帮你办");
ServletContext application=this.getServletContext();
//将请求向下传递
application.getRequestDispatcher("/demo3").forward(request, response);
System.out.println("事办完了");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
myservlet3.java
public class myservlet3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("这个事我能办");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name> <servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>myservlet2</servlet-name>
<servlet-class>com.zk.myservlet.myservlet2</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>myservlet3</servlet-name>
<servlet-class>com.zk.myservlet.myservlet3</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>myservlet2</servlet-name>
<url-pattern>/demo2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myservlet3</servlet-name>
<url-pattern>/demo3</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
运行效果:此案例从myservlet2.java中间转发至myservlet3.java

程序结构

Servlet转发的更多相关文章
- Servlet转发到JSP页面的路径问题
一.现象与概念 1. 问题 在Servlet转发到JSP页面时,此时浏览器地址栏上显示的是Servlet的路径,而若JSP页面的超链接还是相对于该JSP页面的地址且该Servlet和该JSP页面不在同 ...
- servlet转发与重定向区别
servlet转发与重定向 HttpServletRequest: 表示浏览器向服务器发送的请求报文,由Tomcat创建然后作为参数传递给我们doget,dopost方法. 功能:getParam ...
- Servlet转发和重定向的区别
附上视频教学的一张图: 区别: 1.转发产生一次请求,一次响应: 重定向产生2次请求 两次响应 2.转发客户端不可见的: 重定向客户端是可以察觉的. 3.转发时候url不变: 重定向URL会改变 案例 ...
- Servlet 转发请求与重定向,以及路径问题
转发请求 当一个servlet接收到请求后,如果需要将请求转发给另外一个servlet或者jsp文件,可使用下面这种方法: package cn.ganlixin.servlet; import ja ...
- servlet转发重定向
1.request.getRequestDispacther("/test.jsp").forword(request,response); 转发 浏览器URL是一个地 ...
- servlet 转发和超链接转发
超链接属于客户端跳转,request是无法取得属性的 我们知道一个jsp相当与一个servlet 例如,客户端请求A.jsp页面,在A.jsp页面调用request.getAttribute方法放入属 ...
- IT兄弟连 JavaWeb教程 Servlet转发
Servlet对象由Servlet容器创建,并且Servlet对象的service()方法也由容器调用,一个Servlet对象可否直接调用另一个Servlet对象的service()方法呢?答案是否定 ...
- 使用内部Servlet转发JSP后页面的JS,CSS等资源引入问题的解决
转载自:https://blog.csdn.net/weixin_44353336/article/details/90677792 问题原因 我们在进行web项目开发过程中,经常会在web页面引入一 ...
- 关于servlet转发和重新定向
1:重新定向, 是sendRdix(记忆关键词R),firbug中的请求是两个 2:转发 是dispt,(记忆关键词是F),firbug中的请求时一个
随机推荐
- Codeforces Round #617 (Div. 3) 题解
又是隔了一年才来补题的我 A.B水题就不用说了 C - Yet Another Walking Robot C题我居然卡了一会,最后决定用map水,结果出来看了看题解,居然真的是map...没想到会出 ...
- Oracle 中的 Incarnation 到底是个什么?实验操作篇
对于“化身”Incarnation概念了解之后,本篇通过手工恢复实验来具体操作演示,加深对Incarnation的理解,来自于博客园AskScuti. 你可以点击此处查看<概念理解篇>. ...
- 更新centos本地仓库(换源)
/etc/yum.repos.d/CentOS-Base.repo 1,首先进行备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...
- ReLU(inplace=True),这里的inplace=true的意思
ReLU(inplace=True),这里的inplace=true的意思 待办 inplace=True means that it will modify the input directly, ...
- 牛客多校第七场H Pair 数位dp理解
Pair 题意 给出A B C,问x取值[1,A]和y取值[1,B]存在多少组pair<x,y>满足以下最小一种条件,\(x \& y >c\),\(x\) xor \(y& ...
- sql查询 —— 分页
-- 分页 -- limit -- limit start count (start 显示骑士值,单页数量) select *from student where gender=1 limit 6,3 ...
- android toolbar 显示返回按钮并改变按钮颜色
<android.support.design.widget.AppBarLayout android:id="@+id/about_appbar" android:layo ...
- 1059 Prime Factors (25分)
1059 Prime Factors (25分) 1. 题目 2. 思路 先求解出int范围内的所有素数,把输入x分别对素数表中素数取余,判断是否为0,如果为0继续除该素数知道余数不是0,遍历到sqr ...
- 前端框架vue.js系列(9):Vue.extend、Vue.component与new Vue
前端框架vue.js系列(9):Vue.extend.Vue.component与new Vue 本文链接:https://blog.csdn.net/zeping891103/article/det ...
- Spring Cloud Netflix Eureka【服务治理】
一.简介 二.使用 一.源码分析