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

servlet编写简单计算器

时间:2016-07-01 21:28:46      阅读:1250      评论:0      收藏:0      [点我收藏+]

标签:

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编写简单计算器

标签:

原文地址:http://www.cnblogs.com/jakeasd/p/5634255.html

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