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

需求说明:

  1. // index.jsp页面
  2. 1 <%
  3. 2 String basePath = request.getScheme()+":"+"//"+request.getServerName()+":"+request.getServerPort()+"/"
  4. 3 +request.getServletContext().getContextPath()+"/";
  5. 4 %>
  6. 5 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  7. 6 <html>
  8. 7 <head>
  9. 8 <title>ServletDemo加法运算</title>
  10. 9 </head>
  11. 10 <body>
  12. 11 <%--action: 表示访问的servlet路径--%>
  13. 12 <%out.print("basePath意味着:" + basePath);%>
  14. 13 <form action="<%=basePath%>ServletDemo1" method="post">
  15. 14 a: <input type="text" name="a"><br>
  16. 15 b: <input type="text" name="b"><br>
  17. 16 <input type="submit" value="计算"/><br>
  18. 17 </form>
  19. 18 </body>
  20. 19 </html>
  1. // ServletDemo1.java
  1. import javax.servlet.ServletContext;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8.  
  9. @WebServlet("/ServletDemo1")
  10. public class ServletDemo1 extends HttpServlet {
  11. @Override
  12. public void init() throws ServletException {
  13. System.out.println("init()方法");
  14. }
  15.  
  16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  17. System.out.println("doPost()方法");
  18. doGet(request, response);
  19. }
  20.  
  21. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  22. String a = request.getParameter("a");
  23. String b = request.getParameter("b");
  24. int sum = Integer.valueOf(a) + Integer.valueOf(b);
  25. request.setAttribute("sum", sum);
  26. // 方式一:PrintWriter对象写入
  27. // response.getWriter().print(sum);
  28. // 方式二:请求转发
  29. // request.getRequestDispatcher("sum.jsp").forward(request, response);
  30. // 方式三:重定向
  31. ServletContext sc = request.getServletContext();
  32. sc.setAttribute("sum2", sum);
  33. response.sendRedirect("sum2.jsp");
  34. System.out.println("doGet()方法");
  35. }
  36.  
  37. @Override
  38. public void destroy() {
  39. System.out.println("destroy()方法");
  40. }
  41. }
  42.  
  43. // sum.jsp页面
  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>请求跳转求和</title>
    </head>
    <body>
    a + b = <%=request.getAttribute("sum")%>
    </body>
    </html>
  2.  
  3. // sum2.jsp页面
  1. <%@ 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. Amsterdam Distance

    题目描述 Your friend from Manhattan is visiting you in Amsterdam. Because she can only stay for a short ...

  2. python语法基础-基础-运算符

    ############################################ Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算符 赋值运算符 逻辑运算符 位运算符 成员 ...

  3. Robustness|Variability|Diversification|Complexity|自组装|

    生命组学 进化方向有以下四个特性:Robustness:变稳定,比如杀虫剂最大浓度也有杀不死的虫子.Variability易变性与Diversification多样性,容易变多和变多.Complexi ...

  4. 最大流/最小割模板(isap) POJ1273

    isap模板核心代码: //d[]为距离标号数组,d[i]表示节点i到汇点的距离 //gap[]为GAP优化数组,gap[i]表示到汇点距离为i的节点个数 int dfs(int k,int flow ...

  5. 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 ...

  6. Integer 中的缓存类 IntegerCache

    我们先看一段代码: public class TestAutoBoxing { public static void main(String[] args) { //-128到127之间 Intege ...

  7. QT .和::和:和->

    在学习C++的过程中我们经常会用到.和::和:和->,在此整理一下这些常用符号的区别.1.A.B则A为对象或者结构体2.A->B则A为指针,->是成员提取,A->B是提取A中的 ...

  8. 原创:CentOS 环境中 Zabbix 3.4 的安装部署实践

    IT管理工作中,如果没有对服务器.网络设备.服务.进程.应用等的监控,往往是用户发送问题报告后才知道出了问题.事后救火显得被动,不能从容面对问题. 才有了部署一套网络监控系统的想法,机缘巧合下结识了Z ...

  9. mvn -v报java.lang.ClassNotFoundException

    Tips: 比如要下载版本3.2.5的,请选择binaries下的apache-maven-3.2.5-bin.zip. binaries 指的是可以执行的. source 指的源码. 下载地址:ht ...

  10. npm安装依赖太慢问题

    执行 npm install 会发现很慢,可以在安装时手动指定从哪个镜像服务器获取资源,我使用的是阿里巴巴在国内的镜像服务器. 命令如下: npm install --registry=https:/ ...