Servlet简单计算器(只有除法,其它一样)
Posted 鱼在我这里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet简单计算器(只有除法,其它一样)相关的知识,希望对你有一定的参考价值。
Servlet实现计算器
package com.hanqi.web; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class HWjisuan */ public class HWjisuan extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HWjisuan() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter pw=response.getWriter(); String f=request.getParameter("f"); String a=request.getParameter("a"); String b=request.getParameter("b"); double aa=Double.parseDouble(a); double bb=Double.parseDouble(b);
if(a!=""&&b!=""&&f!="")
{
if(f.equals("/"))
{
if(bb!=0)
{
pw.println(aa/bb);
}else
{
pw.println("除数不能为0");
}
}
else
{
pw.println("请输入正确的除法运算符");
}
}else
{
pw.println("不能为空");
}
} /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<%@ 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="jisuan" method="post"> <table> <tr> <td><input type="text" name="a" style="font-size: 15px; width: 50px;"/></td> <td><input type="text" name="f" style="font-size: 15px; width: 15px;"/></td> <td><input type="text" name="b" style="font-size: 15px; width: 50px;"/></td> <td><input type="submit" value="等于"/></td> </tr> </table> </form> </body> </html>
运行结果:
以上是关于Servlet简单计算器(只有除法,其它一样)的主要内容,如果未能解决你的问题,请参考以下文章
Servlet通过 @Autowired注入service后事务失效,但是controller其它注入service事务正常,求指正