servlet编写简单计算器
Posted jakermk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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、运算结果
以上是关于servlet编写简单计算器的主要内容,如果未能解决你的问题,请参考以下文章