[JSP/Servlet] 두수의 합을 구하는 예제

두 수에 대한 합을 구하는 서블릿 클래스

package unit02;


import java.io.IOException;


import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


@WebServlet("/AdditionServlet")

public class AdditOnServlet extends HttpServlet{


@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

int num1 = 20;

int num2 = 10;

int add = num1+num2;

req.setAttribute("num1", num1);

req.setAttribute("num2", num2);

req.setAttribute("add", add);

RequestDispatcher dispatcher = req.getRequestDispatcher("addition03.jsp");

dispatcher.forward(req, resp);

}

}


두 수의 합을 출력하는 JSP
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<title>Addition</title>
</head>
<body>
${num1}+${num2}=${add}
</body>
</html>


결과


댓글

Designed by JB FACTORY