웹 계산기 만들기_Application

ColinSong·2020년 12월 21일
0

WebApplicationServer

목록 보기
8/26
post-thumbnail
post-custom-banner

목차

연결자료

  1. 덧셈, 뺄셈 기능
  2. 입력된 값을 배열로 받기

Application 객체

ServletContext application = getServletContext();

1.1 Application 저장소

getServletContext()

1.2. Application setter, getter

setAttribute("name", "value")

  • 값을 저장

getAttribute("value")

  • 값을 가져옴.

1.3. Application CODE

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>calc</title>
</head>
<body>
	<form action="calc" method="post">
		<div>
		<input type="text" name="v">
		</div>
		<div>
		<input type="submit" name="operator" value="+">
		<input type="submit" name="operator" value="-">
		<input type="submit" name="operator" value="=">
		</div>
	</form>
</body>
</html>
package com.song.pra;

import java.io.IOException;

import javax.servlet.ServletContext;
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("/calc2")
public class Calc2 extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          ServletContext application = request.getServletContext();

          response.setCharacterEncoding("utf-8");
          response.setContentType("text/html; charset=utf-8");
          request.setCharacterEncoding("utf-8");

          String v_ = request.getParameter("v");
          String op = request.getParameter("operator");

          int v = 0;
          if(!v_.equals("")) {
              v = Integer.parseInt(v_);
          }

          int result = 0;

          if(op.equals("=")) {

              int x = (Integer) application.getAttribute("value");// 에플리케이션에 저장되어 있는 걸 꺼내옴.
              int y = v; // 사용자가 저장할 value값

              String operlator = (String)application.getAttribute("op");

              if(operlator.equals("+")) {
                  result = x+y;
              } else {
                  result = x-y;
              }

              response.getWriter().printf("result is %d\n", result);

              // 값을 저장

          }else {
              application.setAttribute("value", v);
              application.setAttribute("op", op);
          }

      }

  }

References

  • 뉴렉처: Servlet / JSP
  • 🎈vscode 정리
  • 🎈2020.12.18

profile
안녕하세요:)
post-custom-banner

0개의 댓글