• servlet编写简单计算器


    1、servlet内代码

    package lianxi;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    public class jiafa extends HttpServlet {
    	private static final long serialVersionUID = 1L;
           
       
        public jiafa() {
            super();
            
        }
    
    	
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		response.setContentType("text/html");
    		response.setCharacterEncoding("UTF-8");
    		String num1=request.getParameter("shu1");
    		String num2=request.getParameter("shu2");
    		int a=Integer.parseInt(num1);
    		int b=Integer.parseInt(num2);
    		int sum=a+b;
    		response.getWriter().write("两个数之和:"+sum);
    		int cheng=a*b;
    		response.getWriter().write("<br>两个数之积:"+cheng);
    		int cha=a-b;
    		response.getWriter().write("<br>两个数之差:"+cha);
    	}
    
    	
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		
    		doGet(request, response);
    	}
    
    }
    

     2、jsp内代码

    <%@ page language="java" 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>加法运算</title>
    </head>
    <body>
    <form action="jiafa" method="post">
    请输入第一个数字:<input type="text" name="shu1"><br>
    请输入第二个数字:<input type="text" name="shu2"><br>
    <input type="submit" value="确定">
    </form>
    </body>
    </html>
    

     3、运算结果

  • 相关阅读:
    Python Flask数据库连接池
    Python Flask 配置文件
    Flask 通过扩展来实现登录验证
    Flask 的系统学习
    Python Flask装饰器登录验证
    Django 批量导入文件
    fedora25的免密码rsync服务配置
    linux强制拷贝避免输入yes方法
    linux系统web站点设置-http基础设置
    rsync用法详细解释
  • 原文地址:https://www.cnblogs.com/jakeasd/p/5634255.html
Copyright © 2020-2023  润新知