码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet简单计算器(只有除法,其它一样)

时间:2016-07-02 09:14:03      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

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!="")
    {
        if(f.equals("/")&&bb!=0)
        {
            pw.println(aa/bb);
        }
        else
        {
            pw.println("除数不能为0");
        }
    }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简单计算器(只有除法,其它一样)

标签:

原文地址:http://www.cnblogs.com/miss123/p/5634759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!