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、運算結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM