웹 계산기 만들기_Application
목차
- 목차
- Application 객체
- 1.1 Application 저장소
- 1.2. Application setter, getter
- 1.3. Application CODE
- References
연결자료
Application 객체
ServletContext application = getServletContext();
1.1 Application 저장소
- 1.1 Application 저장소
- 1.2. Application setter, getter
- 1.3. Application CODE
- References
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
Author And Source
이 문제에 관하여(웹 계산기 만들기_Application), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@withcolinsong/Application저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)