加法计算器(转发和重定向)

运行显示:

转发

重定向

代码:

index.jsp

<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/7/22 0022
Time: 下午 5:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="CountServlet" method="post">
<h3>加法计算器</h3>
加数1:<input type="number" name="one">
加数2:<input type="number" name="two">
<input type="submit" value="计算">
</form>
</body>
</html>

count.jsp

<%--

  Created by IntelliJ IDEA.

  User: Administrator

  Date: 2019/7/22 0022

  Time: 下午 5:51

  To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

计算结果:<%=request.getAttribute("count")%>

<!--计算结果:<%=application.getAttribute("count")%>-->

</body>

</html>
package servlet;

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("/CountServlet")

public class CountServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         String one=request.getParameter("one");

         int o=Integer.parseInt(one);//强制转换,将String类型的数据转换成int类型

         String two=request.getParameter("two");

         int t=Integer.parseInt(two);//强制转换,将String类型的数据转换成int类型

         System.out.println(one+"   "+two);

         int c=o+t;

         String co=String.valueOf(c);//将int类型的数据转换成String类型

         //转发,可以携带数据

         request.setAttribute("count",co);

         request.getRequestDispatcher("count.jsp").forward(request,response);

        //用于存放数据

//        ServletContext s=this.getServletContext();

//        s.setAttribute("count",co);

        //重定向只能依靠ServletContext获取数据

//        response.sendRedirect("count.jsp");

         System.out.println(co);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

         doPost(request,response);

    }

}

最新文章

  1. 入门:Java Map&lt;String,String&gt;遍历及修改
  2. android nagative drawer图标跟标题适配
  3. MVC项目创建与项目结构介绍
  4. Web Server 使用WebClient 发送https请求 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel
  5. IP地址转换成Long型数字的算法
  6. CloudStack 4.2 与CloudStack 4.1二级存储API发生变化
  7. Flex:CSS3布局利器
  8. 知识网之C++总结
  9. JavaScript之惰性函数
  10. ORM对象关系映射之使用GreenDAO进行CRUD操作
  11. rsync的详细配置
  12. [Swift]LeetCode875. 爱吃香蕉的珂珂 | Koko Eating Bananas
  13. Quartz的JobDetail没有触发器指向时会被删除的问题
  14. P2761 软件补丁问题
  15. 深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods)
  16. Tween animation
  17. MyEclipse *的安装步骤和破解(32位和64位皆适用)(图文详解)
  18. 【Python】详解Python多线程Selenium跨浏览器测试
  19. 【Leetcode】86. Partition List
  20. 百度编辑器UEditor不能插入音频视频的解决方法

热门文章

  1. Android Framework中Thread类
  2. css:设置div边框透明+渐变
  3. 数据映射-LSM Tree和SSTable
  4. HDU-3665 Seaside
  5. Redis 和 MongoDB 的优缺点??
  6. spring,get请求中带date日期格式参数,后台无法转换的问题
  7. C语言如何使输出的数字对齐
  8. websocket在springboot+vue中的使用
  9. Python之路-Python中的线程与进程
  10. 基础入门Bootstrap