注:如需转载,请附上原文链接,如有建议或意见,欢迎批评指正!

需求说明:

// 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的请求转发与重定向的区别的更多相关文章

  1. Servlet到Servlet的请求转发与重定向的区别

    Servlet跳转到另一个Servlet中: request.getRequestDispatcher().forward();代表默认的是post方法,即在被跳转的Servlet的doPost()方 ...

  2. SERVLET API中转发与重定向的区别?

    SERVLET API中转发与重定向的区别? 1.转发(forward方法) 转发仅是容器中控制权的转向,在客户端浏览器地址栏中不会显示出转向后的地址. 转发是服务器请求资源,服务器直接访问目标地址的 ...

  3. Servlet的请求转发和重定向

    在学习servlet中对于转发和重定向的理解是非常重要的,但是常常把重定向和转发给混了,今天特地花点时间来总结以下. 一.servlet的转发 1.请求原理图如下所示:  2.可以简单理解转发就好比一 ...

  4. Java 请求转发和重定向的区别以及JavaWeb三大作用域

    三大作用域以及转发和重定向 学习总结 1. 转发和重定向 转发 重定向 转发和重定向的区别: 什么时候用转发什么时候用重定向 三大作用域 作用域类型 作用域方法 如何选择作用域 总结 学习总结 1. ...

  5. servlet的请求转发与重定向

    重定向: Spring的重定向 spring的请求转发:

  6. jsp servlet 的 请求转发和重定向

    以前对于servlet和servlet/jsp之间的跳转路径问题感到很迷惑,今天亲自动手实验了一把,总结如下: servlet已经是项目根路径下面的资源了,所以servlet跳转的时候,在跳转路径上面 ...

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

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

  8. servlet中请求转发(forword)和重定向(redirect)的区别

    servlet请求转发与重定向的区别: request.setAttribute("test","hello"); request.getRequestDisp ...

  9. 创建简单web项目

    Intellij Idea直接安装(可根据需要选择自己设置的安装目录),jdk使用1.6/1.7/1.8都可以,主要是配置好系统环境变量,tomcat7上tomcat的官网下载压缩包解压即可. 一.创 ...

随机推荐

  1. 1088. Rational Arithmetic (20)

    1.注意在数字和string转化过程中,需要考虑数字不是只有一位的,如300转为"300",一开始卡在里这里, 测试用例: 24/8 100/10 24/11 300/11 2.该 ...

  2. 树形dp(最小支配集)

    http://poj.org/problem?id=3659 #include<iostream> #include<cstring> #include<algorith ...

  3. [蓝桥杯2015初赛]方程整数解 unordered_map

    unordered_map: 如果直接写报错加上tr1: #include<tr1/unordered_map>//注意写法 using namespace std; using name ...

  4. OpenCV 对两幅图像求和(求混合(blending))

    #include <cv.h> #include <highgui.h> #include <iostream> using namespace cv; int m ...

  5. QT "debugger unknow type"问题解决办法

    windows上使用qt编译有两种,一种是vs的编译器,一种是MinGW的. 这种问题主要是发生在选择了MSVC2013编译器的版本,这个版本的qt需要再装一个调试器,而MinGW不用. 解决办法:下 ...

  6. winform窗体中webbrowser如何屏蔽脚本错误弹窗

    在构造函数中加入: webBrowser.ScriptErrorsSuppressed = true;

  7. 有用户及目录判断的删除文件内容的Shell脚本

    [root@localhost Qingchu]# cat Qingchu_version2.sh #!/bin/bash #描述: # 清除脚本! #作者:孤舟点点 #版本:2.0 #创建时间:-- ...

  8. 杂记:OSX下编译安装最新版RedisDesktopMmanager

    之前使用Redis数据库时因为操作简单,一直使用的是“redis-cli”连接Redis:后来因为数据展示的不是很直观,所以开始使用带有图形界面的Redis客户端:RedisDesktopMmanag ...

  9. Java8-19-lambda 重构代码

    通过本书的前七章,我们了解了Lambda和Stream API的强大威力. 你可能主要在新项目的代码中使用这些特性.如果你创建的是全新的Java项目,这是极好的时机,你可以轻装上阵,迅速地将新特性应用 ...

  10. STM32F103驱动ADS1118

    ADS1118 作为常用温度测量芯片被越来越多的开发者熟知,TI官方给出的是基于 MSP430 的驱动测试程序,由于 STM32 的普及,闲暇中移植了 MSP430 的 ADS1118 驱动程序到 S ...