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

需求说明:

// 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. python+selenium自动化测试之登录

    selenium_login.py import unittest from selenium import webdriver class LoginTest(unittest.TestCase): ...

  2. 林轩田机器学习基石课程学习笔记5 — Training versus Testing

    上节课,我们主要介绍了机器学习的可行性.首先,由NFL定理可知,机器学习貌似是不可行的.但是,随后引入了统计学知识,如果样本数据足够大,且hypothesis个数有限,那么机器学习一般就是可行的.本节 ...

  3. Invalid action class configuration that references an unknown class问题原因之s:select

    早先做个练习项目就出现了这个错误,各种查资料,然后各种尝试,依然没有解决,不过可以确定是前台页面导致的. 今天又碰到了这个问题,头疼啊!不能再略过了,使用最笨的方法,一个模块一个模块的排除.先看下我的 ...

  4. [LC] 303. Range Sum Query - Immutable

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  5. MAYA 卸载工具,完美彻底卸载清除干净maya各种残留注册表和文件

    是不是遇到MAYA/CAD/3DSMAX/INVENTOR安装失败?AUTODESK系列软件着实令人头疼,MAYA/CAD/3DSMAX/INVENTOR安装失败之后不能完全卸载!!!(比如maya, ...

  6. python3下应用requests

    模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 那么我们先来说说不需要用户登录的方法 这种方式直接可以获取源码,用get的请求方式 登录的方式 获取这种页面 ...

  7. ZOJ-4089-Little Sub and Isomorphism Sequences

    给定你个数组,以及一些单点修改,以及询问,每次询问需要求得,最长的字串长度,它在其他位置存在同构. 当存在两个不相交的区间同构时,如: 1.2.…….n -1.n.n + 1.…….m.m + 1.m ...

  8. 吴裕雄--天生自然 R语言开发学习:图形初阶

    # ----------------------------------------------------# # R in Action (2nd ed): Chapter 3 # # Gettin ...

  9. EMP平台简介(转载)

    1.什么是EMP EMP平台是一个基于J2EE体系的.WEB应用的.基础框架平台: 表现逻辑框架(MVCFrameWork)与业务逻辑框架(EMPBizLogic)分离: 组件化.配置化设计技术: 可 ...

  10. Navicat导入导出数据表

    当我们对mysql数据库进行了误操作,造成某个数据表中的部分数据丢失时,肯定就要利用备份的数据库,对丢失部分的数据进行导出.导入操作了.Navicat工具正好给我们提供了一个数据表的导入导出功能. 1 ...