JSP attribute 예제
wrapper class 예제
<%@page import="java.text.DecimalFormat"%> <%@ 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> <% request.setAttribute("damage", new Integer(145)); request.setAttribute("rate", 1.56); Integer damage1 = (Integer)request.getAttribute("damage"); Double rate1 = (Double)request.getAttribute("rate"); int damage2 = ((Integer)request.getAttribute("damage")).intValue(); double rate2 = rate1.doubleValue(); DecimalFormat df = new DecimalFormat(".##"); %> damage1:<%= damage1 %><br/> rate1:<%= rate1 %><br/> damage2:<%= damage2 %><br/> rage2:<%= rate2 %> <hr/> <!-- Java 5 이상에서 가능한 문법 --> damage1 * rate1:<%=df.format(damage1*rate1) %><br/> damage2 * rate2:<%= damage1*rate2 %><br/> damage2 * rate1:<%= damage2*rate1 %><br/> damage2 * rate2:<%= damage2*rate2 %><br/> </body> </html>
Attribute 예제
<%@page import="java.util.Enumeration"%> <%@ 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> <% //application이 내장한 모든 속성 이름 얻어오기 Enumeration enums = application.getAttributeNames(); String name = null; Object value = null; int i = 1; while (enums.hasMoreElements()){ name = (String)enums.nextElement(); value = application.getAttribute(name);//각 속성 이름에 대한 값 가져오기 out.println("<b>application 속성" + i + "</b>:" + name + " = " +value + "<br>"); i++; } %> </body> </html>
count 예제
<%@ 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> <% Integer count = (Integer)application.getAttribute("count"); if(count == null){ application.setAttribute("count", 1); }else{ count = count + 1; application.setAttribute("count", count); } if(session.isNew()){ count = count + 1; application.setAttribute("count", count); //새로고침을 해도 값이 증가하지 않는다. } %> 방문자수: <%= application.getAttribute("count")%> </body> </html>
file에 count 저장 예제
<%@page import="java.io.IOException"%> <%@page import="java.io.FileWriter"%> <%@page import="jdk.jfr.events.FileWriteEvent"%> <%@page import="java.io.FileReader"%> <%@page import="java.io.PrintWriter"%> <%@page import="java.io.BufferedReader"%> <%@ 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> <% String path = "D:\\count.txt"; //String path = application.getRealPath(file); BufferedReader br = null; PrintWriter pr = null; String count = null; try{ br = new BufferedReader(new FileReader(path)); count = br.readLine().trim(); br.close(); if(session.isNew()){ pr = new PrintWriter(new FileWriter(path)); int n = Integer.parseInt(count); pr.println(n+1); pr.close(); } }catch(IOException e){ out.println("Error: "+ e.getMessage()); } %> 방문자수:<%= count %> </body> </html>











