idea创建简单web项目分析Servlet的请求转发与重定向的区别
- 注:如需转载,请附上原文链接,如有建议或意见,欢迎批评指正!
需求说明:
- // index.jsp页面
- 1 <%
- 2 String basePath = request.getScheme()+":"+"//"+request.getServerName()+":"+request.getServerPort()+"/"
- 3 +request.getServletContext().getContextPath()+"/";
- 4 %>
- 5 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- 6 <html>
- 7 <head>
- 8 <title>ServletDemo加法运算</title>
- 9 </head>
- 10 <body>
- 11 <%--action: 表示访问的servlet路径--%>
- 12 <%out.print("basePath意味着:" + basePath);%>
- 13 <form action="<%=basePath%>ServletDemo1" method="post">
- 14 a: <input type="text" name="a"><br>
- 15 b: <input type="text" name="b"><br>
- 16 <input type="submit" value="计算"/><br>
- 17 </form>
- 18 </body>
- 19 </html>
- // ServletDemo1.java
- import javax.servlet.ServletContext;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- @WebServlet("/ServletDemo1")
- public class ServletDemo1 extends HttpServlet {
- @Override
- public void init() throws ServletException {
- System.out.println("init()方法");
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- System.out.println("doPost()方法");
- doGet(request, response);
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String a = request.getParameter("a");
- String b = request.getParameter("b");
- int sum = Integer.valueOf(a) + Integer.valueOf(b);
- request.setAttribute("sum", sum);
- // 方式一:PrintWriter对象写入
- // response.getWriter().print(sum);
- // 方式二:请求转发
- // request.getRequestDispatcher("sum.jsp").forward(request, response);
- // 方式三:重定向
- ServletContext sc = request.getServletContext();
- sc.setAttribute("sum2", sum);
- response.sendRedirect("sum2.jsp");
- System.out.println("doGet()方法");
- }
- @Override
- public void destroy() {
- System.out.println("destroy()方法");
- }
- }
- // sum.jsp页面
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>请求跳转求和</title>
</head>
<body>
a + b = <%=request.getAttribute("sum")%>
</body>
</html>- // sum2.jsp页面
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>重定向跳转求和</title>
</head>
<body>
a + b = <%=application.getAttribute("sum2")%>
</body>
</html>
index.jsp页面效果图展示:
1. 方式一:PrintWriter对象写入效果图:
2. 方式二:请求转发效果图:
3. 重定向效果图:
idea创建简单web项目分析Servlet的请求转发与重定向的区别的更多相关文章
- Servlet到Servlet的请求转发与重定向的区别
Servlet跳转到另一个Servlet中: request.getRequestDispatcher().forward();代表默认的是post方法,即在被跳转的Servlet的doPost()方 ...
- SERVLET API中转发与重定向的区别?
SERVLET API中转发与重定向的区别? 1.转发(forward方法) 转发仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址. 转发是服务器请求资源,服务器直接访问目标地址的 ...
- Servlet的请求转发和重定向
在学习servlet中对于转发和重定向的理解是非常重要的,但是常常把重定向和转发给混了,今天特地花点时间来总结以下. 一.servlet的转发 1.请求原理图如下所示: 2.可以简单理解转发就好比一 ...
- Java 请求转发和重定向的区别以及JavaWeb三大作用域
三大作用域以及转发和重定向 学习总结 1. 转发和重定向 转发 重定向 转发和重定向的区别: 什么时候用转发什么时候用重定向 三大作用域 作用域类型 作用域方法 如何选择作用域 总结 学习总结 1. ...
- servlet的请求转发与重定向
重定向: Spring的重定向 spring的请求转发:
- jsp servlet 的 请求转发和重定向
以前对于servlet和servlet/jsp之间的跳转路径问题感到很迷惑,今天亲自动手实验了一把,总结如下: servlet已经是项目根路径下面的资源了,所以servlet跳转的时候,在跳转路径上面 ...
- servlet中请求转发(forword)与重定向(sendredirect)的区别
摘自:http://www.cnblogs.com/CodeGuy/archive/2012/02/13/2349970.html 通俗易懂 servlet请求转发与重定向的区别: request.s ...
- servlet中请求转发(forword)和重定向(redirect)的区别
servlet请求转发与重定向的区别: request.setAttribute("test","hello"); request.getRequestDisp ...
- 创建简单web项目
Intellij Idea直接安装(可根据需要选择自己设置的安装目录),jdk使用1.6/1.7/1.8都可以,主要是配置好系统环境变量,tomcat7上tomcat的官网下载压缩包解压即可. 一.创 ...
随机推荐
- Amsterdam Distance
题目描述 Your friend from Manhattan is visiting you in Amsterdam. Because she can only stay for a short ...
- python语法基础-基础-运算符
############################################ Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员 ...
- Robustness|Variability|Diversification|Complexity|自组装|
生命组学 进化方向有以下四个特性:Robustness:变稳定,比如杀虫剂最大浓度也有杀不死的虫子.Variability易变性与Diversification多样性,容易变多和变多.Complexi ...
- 最大流/最小割模板(isap) POJ1273
isap模板核心代码: //d[]为距离标号数组,d[i]表示节点i到汇点的距离 //gap[]为GAP优化数组,gap[i]表示到汇点距离为i的节点个数 int dfs(int k,int flow ...
- Archives: 2018/11
There are 35 posts in total till now. 11月 11, 2018 HTTP 11月 11, 2018 TCP与UDP 11月 10, 2018 Python测试 1 ...
- Integer 中的缓存类 IntegerCache
我们先看一段代码: public class TestAutoBoxing { public static void main(String[] args) { //-128到127之间 Intege ...
- QT .和::和:和->
在学习C++的过程中我们经常会用到.和::和:和->,在此整理一下这些常用符号的区别.1.A.B则A为对象或者结构体2.A->B则A为指针,->是成员提取,A->B是提取A中的 ...
- 原创:CentOS 环境中 Zabbix 3.4 的安装部署实践
IT管理工作中,如果没有对服务器.网络设备.服务.进程.应用等的监控,往往是用户发送问题报告后才知道出了问题.事后救火显得被动,不能从容面对问题. 才有了部署一套网络监控系统的想法,机缘巧合下结识了Z ...
- mvn -v报java.lang.ClassNotFoundException
Tips: 比如要下载版本3.2.5的,请选择binaries下的apache-maven-3.2.5-bin.zip. binaries 指的是可以执行的. source 指的源码. 下载地址:ht ...
- npm安装依赖太慢问题
执行 npm install 会发现很慢,可以在安装时手动指定从哪个镜像服务器获取资源,我使用的是阿里巴巴在国内的镜像服务器. 命令如下: npm install --registry=https:/ ...