728x90
반응형
간단하게 얼마나 많은 요청을 받았는지를 출력하는 jsp를 작성해 보면 다음과 같다.
Counter.java
package com.example;
public class Counter {
private static int count;
public static synchronized int getCount() {
count++;
return count;
}
}
package com.example;
public class Counter {
private static int count;
public static synchronized int getCount() {
count++;
return count;
}
}
BasicCounter.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.example.*" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.println(Counter.getCount());
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.example.*" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
out.println(Counter.getCount());
%>
</body>
</html>
Counter 클래스에서 static int 변수를 두어서 요청한 횟수를 저장할 변수를 만든다.
BasicCounter.jsp에서는 page 지시자를 사용하여 com.example.*을 import 한 후 Counter 객체를 사용해서 카운트를 표시한다.(page 지시자 끝에는 세미콜론이 없다.)
위 코드와 같이 표시하는 경우를 스크립틀릿 코드라 하며 표현식 코드로 변환해서 사용해도 무방하다.
표현식 코드는 다음과 같이 사용한다.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.example.*" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= Counter.getCount() %>
</body>
</html>
<%@ page import="com.example.*" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%= Counter.getCount() %>
</body>
</html>
표현식 코드 역시 지시자와 마찬가지로 세미콜론을 사용하지 않는다.
728x90
반응형
'Java > Servlet & JSP' 카테고리의 다른 글
JSP 초기화 하기 (0) | 2009.02.10 |
---|---|
스크립틀릿에서 변수 선언하기 (0) | 2009.02.04 |
간단한 Jsp 시작하기 (0) | 2009.02.04 |
리스너 예제 (2) | 2009.02.03 |
HttpSessionBindingListener와 HttpSessionAttributeListener (0) | 2009.02.02 |
Cookie 사용하기 (0) | 2009.02.02 |