선택장애

JSP - Application(name이름넘기기, count값 올리기) 본문

JSP

JSP - Application(name이름넘기기, count값 올리기)

yes or yes 2017. 8. 14. 14:53
반응형

application.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<h1>application 예제</h1>
<hr>
1. 서버 정보 : <%= application.getServerInfo() %> <br>
2. 서블릿 API 버젼정보 : <%= application.getMajorVersion() + "." +application.getMinorVersion() %> <br>
3. application.jsp 화일의 실제 경로 : <%= application.getRealPath("application.jsp") %> <br>
<hr>
setAttribute 로 username 변수에 "홍길동" 설정 <p>
<% application.setAttribute("username","홍길동");
application.log("username=홍길동");
application.setAttribute("count", 1);
%>
<a href = "application3.jsp">확인하기</a>
 
</body>
</html>

application3.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
   USERNAME 에 설정된 값은 :<br>
   <%=application.getAttribute("username")%><br>
   <%
   //application scope에 저장된 count 라는 이름의 Integer 객체를 가져옴.
   Integer count = (Integer)application.getAttribute("count");
   //랩퍼 클래스인 Inter를 int형으로 만들어 1을 더함.
   int cnt = count.intValue()+1;
   application.setAttribute("count", cnt);
   %>
   
   <%//새로 고침하면 1씩 카운터가 올라간다. %>
      count : <%= cnt %>
</body>
</html> 

아니면 똑같은 결과 또 다른 방법 
application3.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
   USERNAME 에 설정된 값은 :<br>
   <%=application.getAttribute("username")%><br>
   <%
   //application scope에 저장된 count 라는 이름의 Integer 객체를 가져옴.
   String I = application.getAttribute("count").toString();
   //랩퍼 클래스인 Inter를 int형으로 만들어 1을 더함.
   int num=Integer.parseInt(I)+1;
   
   application.setAttribute("count", num);
   %>     
<%=application.getAttribute("count") %>
</body>
</html>


반응형