<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="calc3" method="post">
<div>
<label>입력 : </label>
<input type="text" name="value">
</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.codingbox.web.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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("/calc3")
public class MyServlet7 extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// application 저장소 선언
ServletContext application = arg0.getServletContext();
String value_ = arg0.getParameter("value");
String op = arg0.getParameter("operator");
PrintWriter out = arg1.getWriter();
int value = 0;
if(!value_.equals("")) { //빈값이 아닐 경우에 연산하기
value = Integer.parseInt(value_);
}
if(op.equals("=")) {
//계산
int x = (Integer)application.getAttribute("value");
int y = value;
String operator = (String)application.getAttribute("op");
int result = 0;
if(operator.equals("+")) {
result = x + y;
} else {
result = x - y;
}
out.printf("결과 값 : %d", result);
} else {
//application에다가 값을 저장하기
application.setAttribute("value", value);
application.setAttribute("op", op);
}
}
}
2-4. session 사용하기
세션값 설정
session.setAttribute("설정이름", 값);
세션 유지시간 설정하기(초 단위)
1) 60분동안 저장
session.setMaxInactiveInterval(60*60);
2) 무한대로 설정
session.setMaxInactiveInterval(-1);
세션에 저장된 값 가져오기
session.getAttribute("설정이름")
세션삭제
session.removeAttribute("설정이름")
//세션 전체 제거
session.invalidate();
2-5. application과 비교하여 session에 값저장하는 것 예제
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="calc" method="post">
<div>
<label>입력 : </label>
<input type="text" name="value">
</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.codingbox.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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;
import javax.servlet.http.HttpSession;
@WebServlet("/calc")
public class MyServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest arg0,
HttpServletResponse arg1) throws ServletException, IOException {
//application 저장소 선언
//ServletContext application = arg0.getServletContext();
//session
HttpSession session = arg0.getSession();
String value_ = arg0.getParameter("value");
String op = arg0.getParameter("operator");
PrintWriter out = arg1.getWriter();
int value = 0;
if(!value_.equals("")) {
value = Integer.parseInt(value_);
}
if( op.equals("=") ) {
// 계산
// int x = (Integer)application.getAttribute("value");
int x = (Integer)session.getAttribute("value");
int y = value;
// String operator = (String)application.getAttribute("op");
String operator = (String)session.getAttribute("op");
int result = 0;
if(operator.equals("+")) {
result = x + y;
} else {
result = x - y;
}
out.printf("결과값 : %d", result);
} else {
//application 값 저장
// application.setAttribute("value", value);
// application.setAttribute("op", op);
session.setAttribute("value", value);
session.setAttribute("op", op);
}
}
}
3-1. 쿠키란
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="calc2" method="post">
<div>
<label>입력 : </label>
<input type="text" name="value">
</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.codingbox.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/calc2")
public class MyServlet2 extends HttpServlet {
@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
// application 저장소 선언
// ServletContext application = arg0.getServletContext();
// session에 저장해보자, 이것도 request에서 꺼내오자
// HttpSession session = arg0.getSession();
// Cookie 선언
Cookie[] cookies = arg0.getCookies();
String value_ = arg0.getParameter("value");
String op = arg0.getParameter("operator");
PrintWriter out = arg1.getWriter();
int value = 0;
if (!value_.equals("")) { // 빈값이 아닐 경우에 연산하기
value = Integer.parseInt(value_);
}
if (op.equals("=")) {
//계산 하는 부분
// int x = (Integer)application.getAttribute("value");
// int y = value;
// String operator = (String)application.getAttribute("op");
// int x = (Integer)session.getAttribute("value");
// int y = value;
// String operator = (String)session.getAttribute("op");
//0번째가 이거맞다면 이렇게 해도된다. 근데 아닐 수도 있자나? 여기저기 막 돌아다닌다거나한다면말이야
// Cookie c = cookies[0];
// if(c.getName().equals("value")) {
// x = Integer.parseInt(c.getValue());
// }
int x = 0;
// for문의 i번째 까지 써서 해도되지만 아래 방법이 훨씬 간단해서 많이쓴다.
for (Cookie c : cookies) {
if (c.getName().equals("value")) {
x = Integer.parseInt(c.getValue());
break;
}
}
int y = value;
String operator = "";
for (Cookie c : cookies) {
if (c.getName().equals("op")) {
operator = c.getValue();
break;
}
}
int result = 0;
if (operator.equals("+")) {
result = x + y;
} else {
result = x - y;
}
out.printf("결과 값 : %d", result);
} else {
// application에다가 값을 저장하기
// application.setAttribute("value", value);
// application.setAttribute("op", op);
// session.setAttribute("value", value);
// session.setAttribute("op", op);
// cookie로 저장, 저장시 문자열 형태만 사용
Cookie valueCookie = new Cookie("value", String.valueOf(value));
Cookie opCookie = new Cookie("op", op);
// 사용자에게 cookie전달
arg1.addCookie(valueCookie);
arg1.addCookie(opCookie);
}
}
}
쿠키
클라이언트(=접속자PC)에 저장
저장형식 : text
만료시점 : 쿠키 저장시 설정
사용자원 : 클라이언트 리소스
용량제한 : 총 300개 (제한적이라 큰것을 저장할 수는 없다.)
속도 : 세션보다는 빠르다.
보안 : 세션보다는 안 좋다.
세션
웹서버에 저장
저장형식 : Object
만료시점 : 브라우저 종료시 설정
사용자원 : 웹 서버 리소스
용량 제한 : 서버가 허용하는 용량제한(무제한에 가깝다)
속도 : 쿠키보다는 느리다.
보안 : 쿠키보다는 좋다.