• Java项目案例之---加法计算器(转发和重定向)


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

    运行显示:

     

    转发

     

    重定向

     

    代码:

    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);
    
        }
    
    }
  • 相关阅读:
    苹果是如何做到快速发货的?
    亚马逊危险了!面临创业公司和科技巨头的颠覆
    We are writing to let you know we have removed your selling privileges
    亚马逊获20亿美元信用额度:有助新业务投资
    亚马逊与美国邮政合作试营快递生鲜服务
    2013下半年(11月)信息系统项目管理师考试题型分析(综合知识、案例分析、论文)
    SQL Server Profiler参数说明
    SQL2005查询所有表的大小
    SQL中EXISTS的用法和效率
    在线生成APK
  • 原文地址:https://www.cnblogs.com/dyddzhp/p/11227760.html
Copyright © 2020-2023  润新知