ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

 ๐Ÿš€ JSP๋กœ ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ค๊ธฐ ์‹ค์Šต

์œ„์™€ ๊ฐ™์€ ํ๋ฆ„์œผ๋กœ ์„œ๋น„์Šค๋ฅผ ์ฒ˜๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค :)

1. ์šฐ์„  ํ™”๋ฉด์„ ๋งŒ๋“ค์ž

๐Ÿ“œ WebContent/calc.jsp ํŒŒ์ผ

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>์ •์ˆ˜ ์ž…๋ ฅ</title>
</head>
<body>
	<form action="" name="expression-form">
		<input type="text" placeholder="์ˆ˜์‹ ์ž…๋ ฅ" id="expression" name="expression">
		<input type="button" id="send" value="๊ฒฐ๊ณผ ํ™•์ธ">
	</form>
</body>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
	$("#send").on("click", () => {
		if(!$("#expression").val()) {
			alert("์ˆ˜์‹์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.");
			return;
		}
		
		$("form[name='expression-form']").submit();
	})
</script>
</html>

์‹คํ–‰ํ™”๋ฉด

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ input ์ฐฝ, button ํ•˜๋‚˜ ์ด๋ ‡๊ฒŒ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.์šฐ๋ฆฌ๋Š” ์ˆ˜์‹์ž…๋ ฅ์ด๋ผ๊ณ  ๋ณด์ด๋Š” input์— 1+1 ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฌด์กฐ๊ฑด ์ˆซ์ž 2๊ฐœ์— ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ 1๊ฐœ ์ด๋ ‡๊ฒŒ ๋“ค์–ด์˜จ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ๋กœ์ง์„ ์ž‘์„ฑํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ•ด๋‹น ์‚ฌ์น™์—ฐ์‚ฐ ๊ธฐํ˜ธ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์—ฐ์‚ฐํ•˜๊ฒŒ ๋˜๊ฒ ์ฃ ?

 

์ฐธ๊ณ ๋กœ ์•„์ง action="" ์ž๋ฆฌ๋Š” ๋น„์›Œ๋’€์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฆฌ์—๋Š” ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•  ์„œ๋ธ”๋ฆฟ์ด ์žˆ๋Š” ํŒŒ์ผ๋ช… ํ˜น์€ ๊ฒฝ๋กœ๋ฅผ ์ ์–ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ €๋Š” ์„œ๋ธ”๋ฆฟ์„ ์ž‘์„ฑํ•˜๊ณ , web.xml์—์„œ ๊ฐ€์ƒ์ฃผ์†Œ๋ฅผ ๋“ฑ๋กํ•œ ํ›„์— ๋‹ค์‹œ ์ฑ„์šฐ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

2. ์‹ค์ œ ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•  ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์ž

๐Ÿ“œ src/com/task/app/Calc.java ํŒŒ์ผ

package com.task.app;

public class Calc {
    private int number1;
    private int number2;
    
    public Calc() {;}
    
    public Calc(String number1, String number2) {
        super();
        this.number1 = Integer.parseInt(number1);
        this.number2 = Integer.parseInt(number2);
    }
    
    public int add() {return number1 + number2;}
    public int sub() {return number1 - number2;}
    public int mul() {return number1 * number2;}
    public int div() {return number1 / number2;}
}

3. ์„œ๋ธ”๋ฆฟ์„ ๋งŒ๋“ค์ž - ์„œ๋ธ”๋ฆฟ์€ HttpServlet์„ ์ƒ์†๋ฐ›์€ Class์ผ๋ฟ์ด๋‹ค..

๐Ÿ“œ src/com/task/app/Oper.java ํŒŒ์ผ

package com.task.app;

import java.io.IOException;

public class Oper extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html; charset=utf-8");
        
        boolean isError = false;
        String expression = request.getParameter("expression");
        String opers = "+-*/";
        char oper = ' ';
        
        Calc calc = null;
        String[] numbers = null;
        int result = 0;
        PrintWriter out = response.getWriter();
        
        for (int i=0; i<expression.length(); i++) {
            for (int j=0; j<opers.length(); j++) {
              if (expression.charAt(i) == opers.charAt(j)) {
                  oper = opers.charAt(j);
              }
            }
        }
        
        numbers = expression.split("\\" + oper);
        calc = new Calc(numbers[0], numbers[1]);
        
        switch (oper) {
        case '+':
            result = calc.add();
            break;
        case '-':
            result = calc.sub();
            break;
        case '*':
            result = calc.mul();
            break;
        case '/':
            try {
                result = calc.div();
            } catch (Exception e) {
                isError = true;
                out.print("<h1>0์œผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.</h1>");
                out.print("<a href='/calc'>๋‹ค์‹œ ๊ณ„์‚ฐํ•˜๊ธฐ</a>");
                out.close();
            }
            break;
        }
        if (!isError) {
            out.print("<h1>๊ฒฐ๊ณผ: " + result + "</h1>");
            out.print("<a href='/calc'๋‹ค์‹œ ๊ฒŒ์‚ฐํ•˜๊ธฐ></a>");
            out.close();
        }
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
728x90
LIST