• servlet+jsp结合完成加法运算


    <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form action="/day_11/Aservlet" method="post">
        数据A:<input type="text" name="data1"/><br/>
        数据B:<input type="text" name="data2"/><br/>
        <input type="submit" value="提交"/><hr/>
    </form>
    </body>
    </html>
    <%@ page language="java" import="java.util.*"contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <% 
    Integer c = (Integer)request.getAttribute("aaa");
    %>
    <%=c %>
    </body>
    </html>
    package cn.code.jia;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class Aservlet
     */
    @WebServlet("/Aservlet")
    public class Aservlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public Aservlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            String s1 = request.getParameter("data1");
            String s2 = request.getParameter("data2");
            int a =Integer.parseInt(s1);
            int b =Integer.parseInt(s2);
            int sum=a+b;
            request.setAttribute("aaa", sum);
            request.getRequestDispatcher("/jsps/result.jsp").forward(request, response);
        }
    
    }
  • 相关阅读:
    寒假学习进度报告(一)
    【web】公文流转系统制作进度(一)(2019/12/9)
    【规律】A Rational Sequence
    【记忆化搜索】Happy Happy Prime Prime
    【背包问题】PACKING
    【动态规划】正则表达式匹配
    【数据结构】P1310 表达式的值
    【数据结构】P1449 后缀表达式
    【数据结构】P1054 等价表达式
    【数据结构】表达式求值
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/7402309.html
Copyright © 2020-2023  润新知