2022.08.17 JSP

sofiaΒ·2022λ…„ 8μ›” 21일
0

JAVA

λͺ©λ‘ 보기
18/27
post-thumbnail

πŸ“–πŸ“•πŸ“˜πŸ“—

πŸ“šμ—°μŠ΅ 문제 및 볡슡 문제

πŸ“–μ—°μŠ΅ 문제 1


1. cookie.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="cookie_process.jsp">
		<p>
			μ•„ 이 λ”” : <input type="text" name="id">
		</p>
		<p>
			λΉ„λ°€λ²ˆν˜Έ : <input type="text" name="passwd">
		</p>
		<p>
			<input type="submit" value="전솑">
		</p>
	</form>
</body>
</html>
  1. cookie_process.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	
	<% 
		String user_id = request.getParameter("id");
		String user_pw = request.getParameter("passwd");
	
		if(user_id.equals("admin") && user_pw.equals("admin1234")){
			Cookie cookie_id = new Cookie("userID",user_id);//μΏ ν‚€ 생성
			cookie_id.setMaxAge(60*60);//κΈ°κ°„ μ„€μ •
			cookie_id.setPath("/");//경둜
			response.addCookie(cookie_id);
			response.sendRedirect("welcome.jsp");
		}else {
			out.println("아이디와 λΉ„λ°€λ²ˆν˜Έλ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”.");
		}
	%>
	

</body>
</html>
  1. welcome.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<% 
		Cookie[] cookies = request.getCookies(); 
		
	
		for(int i =0; i<cookies.length; i++){
			if(cookies[i].getName().equals("userID")){
		

	%>
						<H4><%= cookies[i].getValue() %>λ‹˜ λ°˜κ°‘μŠ΅λ‹ˆλ‹€.</H4>
	
	<% 
			}
		}
	%>



	<a href="./cookie_out.jsp">λ‘œκ·Έμ•„μ›ƒ</a>
</body>
</html>
  1. cookie_out.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		Cookie[] cookies = request.getCookies();
		
		for(int i=0; i<cookies.length; i++){
			cookies[i].setMaxAge(0);//κΈ°κ°„ 만료
			cookies[i].setPath("/");
			response.addCookie(cookies[i]);//ν•΄λ‹Ή μΏ ν‚€ 객체	
		}
		response.sendRedirect("cookie.jsp");
	%>
</body>
</html>



πŸ“–λ³΅μŠ΅ 문제 1

decl02.jsp 파일 생성

  • μ„ μ–Έλ¬Έ νƒœκ·Έμ— μ‹€μˆ˜ λ³€μˆ˜ μ„ μ–Έν•˜μ—¬ '3.14159' μ €μž₯
  • μ‹€μˆ˜ λ³€μˆ˜ 값을 λ°˜ν™˜ν•˜λŠ” λ©”μ†Œλ“œ getDouble() μž‘μ„±
  • getDouble()λ©”μ†Œλ“œ ν˜ΈμΆœν•˜μ—¬ μ‹€μˆ˜ λ³€μˆ˜ κ°’ 좜λ ₯
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%!
		double a = 3.14159;
		public double getDouble(){
			return a;
		}
	%>
	
	<%
		out.println(getDouble());
	%>
</body>
</html>

πŸ“–λ³΅μŠ΅ 문제 2

expr02.jsp 파일 생성

  • ν‘œν˜„λ¬Έ νƒœκ·Έμ— java.util.Calendar 클래슀λ₯Ό μ΄μš©ν•˜μ—¬ 였늘 λΆ€ν„° ν•œλ‹¬ ν›„ λ‚ μ§œ 좜λ ₯
  1. λ‚΄κ°€ ν•œ 방법
<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%!
		public String calDate(){
		Calendar cal = Calendar.getInstance();
		String days ="";
		String month ="";
		String year ="";
		
		cal.add(Calendar.MONDAY,1);//ν•œλ‹¬ λ”ν•˜κΈ°
		cal.get(Calendar.DATE);//ν˜„μž¬ λ‚ μ§œ 가지고 였기
		
		
		days = String.valueOf(cal.get(Calendar.DATE));
		month = String.valueOf(cal.get(Calendar.MONTH)+1);
		//1월을 0으둜 ν‘œκΈ°ν•˜κΈ° λ•Œλ¬Έμ— +1을 ν•΄μ€˜μ•Όν•¨
		year = String.valueOf(cal.get(Calendar.YEAR));
	
		return year+" "+month+" "+days;
		}
	%>
	ν•œλ‹¬ = <%= calDate() %>
	
</body>
</html>
  1. κ°•μ‚¬λ‹˜μ˜ 방법(ν•œλ²ˆμ— returnν•˜μ—¬ κ²°κ³Όλ₯Ό μ–»μŒ)
<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%!
		public String calDate(){
			Calendar cal = Calendar.getInstance();
			return cal.get(Calendar.YEAR)+"-"
					+ String.valueOf(cal.get(Calendar.MONTH)+2) +"-"
					+ cal.get(Calendar.DATE);
		}
	%>
	λ‹€μŒ 달 = <%= calDate() %>
	
</body>
</html>

πŸ“–λ³΅μŠ΅ 문제 3


h01.jsp νŒŒμΌμ€ μƒλž΅

i01.jsp

<%@page import="java.util.Calendar"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%!
		public String calDate(){
			Calendar cal = Calendar.getInstance();
			return cal.get(Calendar.YEAR)+"-"
					+ String.valueOf(cal.get(Calendar.MONTH)+2) +"-"
					+ cal.get(Calendar.DATE);
		}
	%>
	λ‹€μŒ 달 = <%= calDate() %>
	
</body>
</html>

πŸ“š μ„Έμ…˜(Session) (PPT Chapter 10)

πŸ“˜μƒνƒœ μœ μ§€λ₯Ό μœ„ν•œ μ„Έμ…˜(Session) 객체

  • μ„Έμ…˜μ€ μ„œλ²„μƒμ— μ‘΄μž¬ν•˜λŠ” 객체
  • μ„Έμ…˜μ€ μ›Ή λΈŒλΌμš°μ € ν•˜λ‚˜λ‹Ή ν•˜λ‚˜μ”©λ§Œ λ°œκΈ‰
  • μ„Έμ…˜μ€ μ›Ή λΈŒλΌμš°μ €λ₯Ό λ‹«κΈ°μ „κΉŒμ§€ νŽ˜μ΄μ§€ μ΄λ™ν•˜λ”λΌλ„ μ‚¬μš©μžμ˜ 정보λ₯Ό μžƒμ§€μ•Šκ³  μ„œλ²„μ— 보관
  • 쿠킀보닀 λ³΄μ•ˆμ— 강함
  • μœ μ§€μ‹œκ°„(μ§€μ†μ‹œκ°„)이 μ‘΄μž¬ν•¨

πŸ“˜μ„Έμ…˜(Session) 객체가 μ œκ³΅ν•˜λŠ” λ©”μ†Œλ“œ

  1. getAttribute(name)
  • name에 λŒ€ν•œ μ„ΈνŒ…λœ 값을 λ°˜ν™˜ν•¨
  • λ¦¬ν„΄νƒ€μž…μ΄ Objectνƒ€μž…μ΄λ―€λ‘œ μ‚¬μš©μ‹œ μ‹€μ œ ν• λ‹Ήλœ 객체 νƒ€μž…μœΌλ‘œ ν˜•λ³€ν™˜(Casting)해야함.
  1. getAttributeNames()
  • μ„Έμ…˜μ˜ λͺ¨λ“  κ°μ²΄λ“€μ˜ 이름을 μ—΄κ±°ν˜•μœΌλ‘œ λ°˜ν™˜
  • Enumeration μ‚¬μš©

3.setAttribute(name, value)

  • μ„Έμ…˜μ˜ 이름과 κ°’ μ„€μ •
  1. setMaxInactiveInterval()
  • μ§€μ†μ‹œκ°„μ„ μ„€μ •(κΈ°λ³Έ 30λΆ„μœΌλ‘œ λ˜μ–΄μžˆμŒ)


(κΈ°λ³Έ μ„€μ • 30λΆ„(Servers의 web.xml에 μ‘΄μž¬ν•¨)

πŸ“˜session에 κ°’ μ €μž₯ν•˜κΈ°/ μ–»μ–΄μ˜€κΈ°

  1. session.setAttribute(String name, Object value);

    • String ls_test = "μ„Έμ…˜μ— μ €μž₯된 데이터가 λ°”λ‘œ μ ‘λ‹ˆλ‹€. ^o^";
    • session.setAttribute("Testing", ls_test);
      β†’ Testing : μ„Έμ…˜μ΄λ¦„ , ls_test : μ„Έμ…˜ κ°’
  2. Object getAttribute (String name)Β 

    • Object lobj_getdata = session.getAttribute("Testing");
    • String ls_getdata = (String)lobj_getdata;

예제1. getAttribute.jsp 생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
 	<center>[ μ„Έμ…˜ 값을 μ–»μ–΄μ˜€λŠ” 예제 ]</center>
		<hr>
			μ–»μ–΄μ˜¨ μ„Έμ…˜κ°’μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.
		<hr>
	<% 
		Object obj_getdata = session.getAttribute("Testing");
		String s_getdata = (String)obj_getdata;
	
		int i_getdata = (Integer) session.getAttribute("MyData"); //μΊμŠ€νŒ…
		
		out.println("μ„€μ •λœ μ„Έμ…˜κ°’ [1] =>"+s_getdata+"<br><br>");
		out.println("μ„€μ •λœ μ„Έμ…˜κ°’ [2] =>"+i_getdata+"<br><br>");
	%>
	
</body>
</html>


였λ₯˜ λ°œμƒ (Testing,MyDataκ°€ μ‘΄μž¬ν•˜μ§€ μ•Šμ•„μ„œ)

예제2. setAttribute.jsp 생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
	String s_test = "μ„Έμ…˜μ— μ €μž₯된 데이터가 λ°”λ‘œ μ ‘λ‹ˆλ‹€. ^γ…‡^";
	session.setAttribute("Testing", s_test);	
	session.setAttribute("MyData", 10);
%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center><h3>[ μ„Έμ…˜ 값을 μ„€μ •ν•˜λŠ” 예제 ]</h3></center>
	<hr>
		μ„Έμ…˜κ°’μ„ μ„€μ •ν•˜λŠ” νŽ˜μ΄μ§€ μž…λ‹ˆλ‹€.
	<hr>

</body>
</html>

getAttribute.jsp λ‹€μ‹œ μ‹€ν–‰

μ„Έμ…˜κ°’μ΄ 반영됨

. getAttributeNames λ©”μ†Œλ“œμ™€ Enumeration μΈν„°νŽ˜μ΄μŠ€

μœ„μ—μ„œ getAttributeNames() λ©”μ†Œλ“œμ„€λͺ…을 보면 μ„Έμ…˜μ— λ“€μ–΄μžˆλŠ” λͺ¨λ“  κ°μ²΄λ“€μ˜ 값듀을 κ°€μ Έμ˜€κΈ° μœ„ν•΄μ„œ μ‚¬μš―μ•„κ³ , μ—΄κ±°ν˜•μœΌλ‘œ λ°˜ν™˜λœλ‹€κ³  ν•œλ‹€. κ·Έλž˜μ„œ μ—΄κ±°ν˜• κ°μ²΄μ—μ„œ 각각 λ½‘μ•„μ˜€κΈ° μœ„ν•΄μ„œλŠ” hasMoreElements와 nextElement λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•΄μ•Όν•¨

  1. hasMoreElements
    : μœ„μ˜ μ—΄κ±°ν˜• κ°μ²΄μ—μ„œ λ½‘μ•„μ˜¬ 데이터가 μžˆλŠ”μ§€ κ²€μ‚¬ν•˜μ—¬ T/F 리턴
  2. nextElement
    : μœ„μ˜ λ©”μ†Œλ“œλ‘œ 검사 후에 λ‹€μŒ 데이터가 μ‘΄μž¬ν•œλ‹€λŠ” μ „μž¬ν•˜μ— 데이터λ₯Ό ν•˜λ‚˜μ”© λ½‘μ•„μ˜΄.
  • nextElement λ©”μ†Œλ“œλ„ getAttribute λ©”μ†Œλ“œμ²˜λŸΌ Object ν˜•μœΌλ‘œ 값을 λ°˜ν™˜ν•΄ μ£Όλ―€λ‘œ 이λ₯Ό `ν˜•λ³€ν™˜(casting)`ν•΄μ•Ό 함

예제3. getAttributeNames.jsp 생성

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center>[ μ„Έμ…˜ 값을 μ–»μ–΄μ˜€λŠ” 예제 - 2 ]</center>
		<hr>
			getAttributeNames() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ„Έμ…˜κ°’μ„ μ–»μ–΄μ˜€λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€.
		<hr>
		
	<%
		String s_validata ="ν˜„μž¬ νŽ˜μ΄μ§€λŠ” λ¬Όλ‘  νŽ˜μ΄μ§€μ—μ„œ μ„€μ •λœ μ„Έμ…˜ 값듀도 λ‚˜νƒ€λƒ…λ‹ˆλ‹€.";
		session.setAttribute("Validate", s_validata);
		
		String s_name="";
		String s_value="";
		
		Enumeration enum_app = session.getAttributeNames();
		//getAttributeNames()은 μ„Έμ…˜ 이름듀을 닀가지고 μ˜€λŠ” λ©”μ†Œλ“œ. μ—΄κ±°ν˜•μœΌλ‘œ 가지고 옴
		//EnumerationλŠ” 반볡자둜  Enumμž„
		
		int i =0;
		
		while(enum_app.hasMoreElements()){ //μ—΄κ±°ν˜• 좜λ ₯ν•˜κΈ° μœ„ν•΄μ„œ 반볡문 μ‚¬μš©
			i++;
			s_name = enum_app.nextElement().toString();//toStringμ‚¬μš©
			s_value = session.getAttribute(s_name).toString();//toStringμ‚¬μš©
			// μ—΄κ±°ν˜•Enum을 λ¬Έμžμ—΄λ‘œ λ°”κΎΈκΈ° μœ„ν•΄μ„œ toStringμ‚¬μš©
			//Enumeration은 λ‹€μŒ λ‚΄μš©μ΄ μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” hasMoreElements() λ©”μ†Œλ“œμ™€
			//κ·Έ 값을 κ°€μ Έμ˜€λŠ” nextElement() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©

			
			out.println("<br> μ–»μ–΄μ˜¨ μ„Έμ…˜ 이름 ["+i+"] : "+s_name+"<br>");
			out.println("<br> μ–»μ–΄μ˜¨ μ„Έμ…˜ κ°’ ["+i+"] : "+s_value+"<hr>");
		}
		
	%>
</body>
</html>

예제4. infoSession.jsp 생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center><h3>[ μ„Έμ…˜ 정보λ₯Ό μ–»μ–΄μ˜€λŠ” λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•œ 예제 ]</h3></center>
	<hr>
	
	<%
		String s_id = session.getId();
		//아이디λ₯Ό λ³€μˆ˜λ‘œ λ°›μŒ
		long last_time = session.getLastAccessedTime();
		long Creationlast_time = session.getCreationTime();
		long time_used = (last_time - Creationlast_time) /60000;
		// λΆ„ 계산
		int inactive =  session.getMaxInactiveInterval() / 60;
		// λΆ„ 계산
		boolean b_new = session.isNew();
	%>	
	
	[1] μ„Έμ…˜ IDλŠ”  [ <%= s_id %> ] μž…λ‹ˆλ‹€. <br><hr>	
	[2] 당신이 μ›Ή μ‚¬μ΄νŠΈμ— λ¨Έλ¬Έ μ‹œκ°„μ€   [ <%= time_used %> ] λΆ„μž…λ‹ˆλ‹€. <br><hr>	
	[3] μ„Έμ…˜μ˜ 유효 μ‹œκ°„μ€  [ <%= inactive %> ] λΆ„μž…λ‹ˆλ‹€. <br><hr>	
	[4] μ„Έμ…˜μ΄ μƒˆλ‘œ λ§Œλ“€μ–΄ μ‘Œλ‚˜μš”? <br><br>
	<%
		if(b_new){
			out.println("예 !! μƒˆλ‘œμš΄ μ„Έμ…˜μ„ λ§Œλ“€μ—ˆμŠ΅λ‹ˆλ‹€.");
		}else{
			out.println("μ•„λ‹ˆμ˜€ !! μƒˆλ‘œμš΄ μ„Έμ…˜μ„ λ§Œλ“€μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.");
		}
	%>	
</body>
</html>

예제5. removeAttribute.jsp 생성

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center><h3>[ μ„€μ •λœ μ„Έμ…˜ 값을 μ‚­μ œν•˜λŠ” 예제 ]</h3></center>
	<hr>
	removeAttribute() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ„Έμ…˜κ°’μ„ μ‚­μ œν•˜λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€.
	<%
		String s_name;
		String s_value = "μ €λŠ” μ„Έμ…˜μ— μ €μž₯된 첫 번째 κ°’μ΄μ—μš”. ^γ…‡^";
		session.setAttribute("s_name1", s_value);
		
		s_value = "μ €λŠ” μ„Έμ…˜μ— μ €μž₯된 두 번째 κ°’μ΄μ—μš”. ^γ…‡^";
		session.setAttribute("s_name2", s_value);
		
		s_value = "μ €λŠ” μ„Έμ…˜μ— μ €μž₯된 μ„Έ 번째 κ°’μ΄μ—μš”. ^γ…‡^";
		session.setAttribute("s_name3", s_value);
	
		out.print("<hr><h3>----- μ„Έμ…˜κ°’μ„ μ‚­μ œν•˜κΈ° μ „ -----</h3>");
		Enumeration enum_app = session.getAttributeNames();
		int i =0 ;
		
		while(enum_app.hasMoreElements()){
			i++;
			s_name =  enum_app.nextElement().toString();
			s_value =  session.getAttribute(s_name).toString();
			out.print("<hr>μ–»μ–΄μ˜¨ μ„Έμ…˜ 이름 ["+i+"] : "+s_name);
			out.print("<br>μ–»μ–΄μ˜¨ μ„Έμ…˜ κ°’ ["+i+"] : "+s_value);
		}
		
		
		
		session.removeAttribute("s_name2");//μ„Έμ…˜κ°’ μ‚­μ œ

		
		
		out.print("<hr><h3>----- μ„Έμ…˜κ°’μ„ μ‚­μ œν•œ ν›„  -----</h3>");
		enum_app = session.getAttributeNames();
		i =0 ;
		
		while(enum_app.hasMoreElements()){
			i++;
			s_name =  enum_app.nextElement().toString();
			s_value =  session.getAttribute(s_name).toString();
			out.print("<hr>μ–»μ–΄μ˜¨ μ„Έμ…˜ 이름 ["+i+"] : "+s_name);
			out.print("<br>μ–»μ–΄μ˜¨ μ„Έμ…˜ κ°’ ["+i+"] : "+s_value);
		}
	%>
</body>
</html>


κ²°κ³ΌλŠ” λžœλ€μœΌλ‘œλ‚˜μ˜΄(μΈλ±μŠ€κ°€ μ—†μ–΄μ„œ 랜덀 μ²˜λ¦¬κ°€ 됨)

예제 6. invalidate.jsp 생성

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center><h3>[ μ„€μ •λœ μ„Έμ…˜ 값을 μ‚­μ œν•˜λŠ” 예제  ]</h3></center>
	<hr>
	invalidate() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ μ„Έμ…˜κ°’μ„ μ‚­μ œν•˜λŠ” μ˜ˆμ œμž…λ‹ˆλ‹€.
	
	<%
		String s_name;
		String s_value = "μ €λŠ” μ„Έμ…˜μ— μ €μž₯된 첫 번째 κ°’μ΄μ—μš”. ^γ…‡^";
		session.setAttribute("s_name1", s_value);
		
		s_value = "μ €λŠ” μ„Έμ…˜μ— μ €μž₯된 두 번째 κ°’μ΄μ—μš”. ^γ…‡^";
		session.setAttribute("s_name2", s_value);
		
		s_value = "μ €λŠ” μ„Έμ…˜μ— μ €μž₯된 μ„Έ 번째 κ°’μ΄μ—μš”. ^γ…‡^";
		session.setAttribute("s_name3", s_value);
	
		out.print("<hr><h3>----- μ„Έμ…˜κ°’μ„ μ‚­μ œν•˜κΈ° μ „ -----</h3>");
		Enumeration enum_app = session.getAttributeNames();
		int i =0 ;
		
		while(enum_app.hasMoreElements()){
			i++;
			s_name =  enum_app.nextElement().toString();
			s_value =  session.getAttribute(s_name).toString();
			out.print("<hr>μ–»μ–΄μ˜¨ μ„Έμ…˜ 이름 ["+i+"] : "+s_name);
			out.print("<br>μ–»μ–΄μ˜¨ μ„Έμ…˜ κ°’ ["+i+"] : "+s_value);
		}
		
		session.invalidate();
		//μ„Έμ…˜ λͺ½λ•… μ‚­μ œ
	
		out.print("<hr><h3>----- μ„Έμ…˜κ°’μ„ μ‚­μ œν•œ ν›„  -----</h3>");
		
		if(request.isRequestedSessionIdValid()){
			out.print("μ„Έμ…˜ 아이디가 μœ νš¨ν•©λ‹ˆλ‹€.");
		}else{
			out.print("μ„Έμ…˜ 아이디가 μœ νš¨ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.");
		}
	
	
	%>
	
</body>
</html>

interval.jsp 생성// μ„Έμ…˜ 유효 μ‹œκ°„ 좜λ ₯

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h4>------ μ„Έμ…˜ 유효 μ‹œκ°„ λ³€κ²½ μ „ ------</h4>
	
	<%
		int time = session.getMaxInactiveInterval() /60;
		out.print("μ„Έμ…˜ 유효 μ‹œκ°„ : "+time+"λΆ„ <br>");
	%>
	
	
	<h4>------ μ„Έμ…˜ 유효 μ‹œκ°„ λ³€κ²½ ν›„ ------</h4>
	
	<%
		session.setMaxInactiveInterval(60*60);
		
		time = session.getMaxInactiveInterval()/60 ;
		out.print("μ„Έμ…˜ 유효 μ‹œκ°„ : "+time+"λΆ„ <br>");
	%>

</body>
</html>

μ„Έμ…˜μƒμ„± κ΄€λ ¨ 둜그인 문제


풀이

  1. session.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form action="session_process.jsp" method="post">
		<p>μ•„ 이 λ”” : <input type="text" name="id"></p>
		<p>λΉ„λ°€λ²ˆν˜Έ : <input type="text" name="passwd"></p>
		<p><input type="submit" value="전솑"></p>
	</form>
</body>
</html>
  1. seesion_process.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		String user_id = request.getParameter("id");
		String user_pw = request.getParameter("passwd");
		
		if(user_id.equals("admin") && user_pw.equals("admin1234")){
			
			session.setAttribute("userID", user_id);
			response.sendRedirect("welcome.jsp");
		}else {
				out.println("아이디와 λΉ„λ°€λ²ˆν˜Έλ₯Ό ν™•μΈν•΄μ£Όμ„Έμš”.");
		}
	
	%>
</body>
</html>
  1. welcome.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		if(session.getAttribute("userID") == null){
			response.sendRedirect("session_out.jsp");
		}
	%>
	
	
	<h4><%=session.getAttribute("userID") %>λ‹˜ λ°˜κ°‘μŠ΅λ‹ˆλ‹€.</h4>
	<a href="session_out.jsp">λ‘œκ·Έμ•„μ›ƒ</a>
	
</body>
</html>
  1. session_out.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<% 
		session.invalidate();
		response.sendRedirect("session.jsp");
	%>
</body>
</html>

πŸ“š μ˜ˆμ™Έμ²˜λ¦¬ (PPT Chapter 11)

<%@ page errorPage="errorPage_error.jsp"%>
예러 λ°œμƒμ‹œ errorPage_error.jspνŽ˜μ΄μ§€λ‘œ 이동

<%@ page isErrorPage="true" %>
μœ„ νŽ˜μ΄μ§€λŠ” μ—λŸ¬νŽ˜μ΄μ§€λ₯Ό μ•Œλ €μ€Œ
μ΄λ•Œ νŽ˜μ΄μ§€λ₯Ό μ‹€ν–‰ν•˜κΈ° μœ„ν•΄μ„œλŠ”
<%response.setStatus(HttpServletResponse.SC_OK);%> λ₯Ό μž‘μ„±ν•΄μ•Όν•¨

예제

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	name νŒŒλΌλ―Έν„° : 
	<%= request.getParameter("name").toUpperCase() %>
</body>
</html>
```![](https://velog.velcdn.com/images/sofia_777/post/5b52678b-063b-409b-80d3-a86791d99a3b/image.png)

```java
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page errorPage="errorPage_error.jsp" %>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	name νŒŒλΌλ―Έν„° : 
	<%= request.getParameter("name").toUpperCase() %>
</body>
</html>

errorPage_error.jsp 생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	였λ₯˜κ°€ λ°œμƒν•˜μ˜€μŠ΅λ‹ˆλ‹€.
</body>
</html>

μ˜ˆμ™ΈνŽ˜μ΄μ§€λ₯Ό μ΄μš©ν•œ μ˜ˆμ™Έμ²˜λ¦¬ 예제

infoForm.html생성

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="viewInfo.jsp">
		이 름 : <input type="text" name="name" size="20"><br>
		λ‚˜ 이 : <input type="text" name="age" value="20"><br>
		<hr>
		* P.S : λ‚˜μ΄λŠ” 숫자만 μž…λ ₯ν•΄μ•Όν•©λ‹ˆλ‹€.
		<hr>
		<input type="submit" value="전솑">
	</form>
</body>
</html>

viewinfo.jsp생성

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
		String s_name;
		int age;
	%>
	<%
		//response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		
		
		s_name = request.getParameter("name");
		age =Integer.parseInt(request.getParameter("age"));
	%>
	
	<h3>νšŒμ› 정보 좜λ ₯</h3>
	λ‹Ήμ‹ μ˜ 이름은  <%= s_name %>μž…λ‹ˆλ‹€. <br>
	λ‹Ήμ‹ μ˜ λ‚˜μ΄λŠ”  <%= age %>μ‚΄μž…λ‹ˆλ‹€. <br>
</body>
</html>

λ§Œμ•½μ— 이름에 홍길동

λ‚˜μ΄μ— 슀물 μž…λ ₯


viewInfo,jsp μˆ˜μ •

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page errorPage = "error02.jsp" %>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%!
		String s_name;
		int age;
	%>
	<%
		//response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		
		
		s_name = request.getParameter("name");
		age =Integer.parseInt(request.getParameter("age"));
	%>
	
	<h3>νšŒμ› 정보 좜λ ₯</h3>
	λ‹Ήμ‹ μ˜ 이름은  <%= s_name %>μž…λ‹ˆλ‹€. <br>
	λ‹Ήμ‹ μ˜ λ‚˜μ΄λŠ”  <%= age %>μ‚΄μž…λ‹ˆλ‹€. <br>
</body>
</html>

<%@ page errorPage = "error02.jsp" %> μΆ”κ°€

error02.jsp 생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
μ—λŸ¬ λ°œμƒ

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page isErrorPage="true" %>

μ—λŸ¬ λ°œμƒ
<br>
<%= exception.getMessage() %>

πŸ“š μžλ°”λΉˆμ„ μ΄μš©ν•œ JSP 둜직 λͺ¨λ“ˆν™”(PPT Chapter 12)

  • μžλ°”λΉˆ :데이터λ₯Ό ν‘œν˜„ν•˜λŠ” 것을 λͺ©μ μœΌλ‘œ ν•˜λŠ” μžλ°” 클래슀

  • μžλ°”λΉˆ ν΄λž˜μŠ€λŠ” 데이터λ₯Ό μ €μž₯ν•˜λŠ” ν•„λ“œ, 데이터λ₯Ό μ½μ–΄μ˜¬ λ•Œ μ‚¬μš©λ˜λŠ” λ©”μ†Œλ“œ(getproperty),
    값을 μ €μž₯ν• λ•Œ μ‚¬μš©λ˜λŠ” λ©”μ†Œλ“œλ‘œ ꡬ성(setproperty)

  • ν•œλ²ˆ λ§Œλ“€μ–΄ 놓고 ν•„μš”ν• λ•Œλ§ˆλ‹€ κ°€μ Έκ°€μ„œ μ‚¬μš©ν•œλ‹€.

  • 기본적으둜 JAVA의 ꡬ쑰λ₯Ό 가지고 있음(ν•„λ“œ+λ©”μ†Œλ“œ)
    μ—¬κΈ°μ„œ ν•„λ“œλ₯Ό ν”„λ‘œνΌν‹°?라고 λΆ€λ₯Έλ‹€.

μžλ°”λΉˆ μ•‘μ…˜ νƒœκ·Έ

  • jsp:useBean : 빈 객체 생성
    <jsp:useBean class="hello.HelloBean" id="myBean" scope="page" />
    μ—¬κΈ°μ„œ pageλŠ” μ ‘κ·Ό μ§€μ •μž
    - μ ‘κ·Ό μ§€μ •μžμ˜ μ’…λ₯˜
    page
    request
    session
    Application
  • jsp:setProperty : κ°’ μ„ΈνŒ…
    <jsp:setProperty name="myBean" property="age" value="10" /> = <% myBean.setAge(10); %>
    property="*"λŠ” λͺ¨λ“  정보λ₯Ό 객체에 λ„˜κΉ€
  • Jsp:getProperty : 값가지고 였기
    이름 : <jsp:getProperty property="name" name="myBean"/>

κ΄€λ ¨ 예제1

helloBean.jsp 파일 생성

package hello;

public class HelloBean {
	private String name ="홍길동";
	private int age = 20;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

helloBean.jsp 파일 생성(μ‹€ν–‰)

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean class="hello.HelloBean" id="myBean" scope="page"></jsp:useBean>
<hr>
*. 빈 생성 ν›„ μ €μž₯된 κ°’ 좜λ ₯ν•˜κΈ°<br><br>
이름 : <%= myBean.getName() %><br>
λ‚˜μ΄ : <%= myBean.getAge() %>
<hr>
*. 값을 λ³€κ²½ ν•œ ν›„ 좜λ ₯ν•˜κΈ°<br><br>
<% 
	myBean.setName("μ „μˆ˜λΉˆ");
	myBean.setAge(10);
%>

이름 : <%= myBean.getName() %><br>
λ‚˜μ΄ : <%= myBean.getAge() %>

(getter)λΆ€λΆ„

<%= myBean.getName() %>
 <%= myBean.getAge() %>

(setter)λΆ€λΆ„

myBean.setName("μ „μˆ˜λΉˆ");
myBean.setAge(10);

usingBeanProperty.jsp생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean class = "hello.HelloBean" id="myBean"/>
<hr>
#. λΉˆμƒμ„± ν›„ μ €μž₯된 κ°’ 좜λ ₯ν•˜κΈ°<br><br>
이름 : <jsp:getProperty property="name" name="myBean"/><br>
λ‚˜μ΄ : <jsp:getProperty property="age" name="myBean"/>
<hr>
#. 값을 λ³€κ²½ν•œ ν›„ 좜λ ₯ν•˜κΈ°<br><br>
<jsp:setProperty property="name" name="myBean" value ="μ „μˆ˜λΉˆ"/>
<jsp:setProperty property="age" name="myBean" value ="10"/>

이름: <jsp:getProperty property="name" name="myBean"/><br>
λ‚˜μ΄: <jsp:getProperty property="age" name="myBean"/><br>

κ΄€λ ¨μ˜ˆμ œ 2 νšŒμ› 정보λ₯Ό μ €μž₯ν•˜λŠ” 빈 λ§Œλ“€κΈ°

magic νŒ¨ν‚€μ§€ μ•ˆμ— MemberBean.java 생성

package magic;

public class MemberBean {
	private String id;
	private String pw;
	private String name;
	private int mclass;
	private String phone1;
	private String phone2;
	private String phone3;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPw() {
		return pw;
	}
	public void setPw(String pw) {
		this.pw = pw;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getMclass() {
		return mclass;
	}
	public void setMclass(int mclass) {
		this.mclass = mclass;
	}
	public String getPhone1() {
		return phone1;
	}
	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}
	public String getPhone2() {
		return phone2;
	}
	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}
	public String getPhone3() {
		return phone3;
	}
	public void setPhone3(String phone3) {
		this.phone3 = phone3;
	}
}

addForm.jsp 생성

<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<form method="post" action="usingBeanAddForm.jsp">
			μ•„ 이 λ”” : <input type="text" name="id" size="20"><br>
			λΉ„λ°€λ²ˆν˜Έ : <input type="password" name="pw" size="20"><br>
			νšŒμ›μ΄λ¦„ : <input type="text" name="name" size="20"><br>
			νšŒμ›κ΅¬λΆ„ : <input type="radio" name="mclass" value="1">μΌλ°˜νšŒμ›
			        <input type="radio" name="mclass" value="2">κ΅μˆ˜λ‹˜<br>
			μ „ν™”λ²ˆν˜Έ : <select name="phone1">
						<option value = "010">010</option>
						<option value = "011">011</option>
						<option value = "016">016</option>
						<option value = "017" selected>017</option>
						<option value = "018">018</option>
						<option value = "019">019</option>
					</select>
					-
			        <input type="text" name ="phone2" size="4" maxlength="4">
			        -
			        <input type="text" name ="phone3" size="4" maxlength="4"><br>
			        <input type="submit" value="전솑">
	
	</form>
</body>
</html>

usingBeanAddForm.jsp생성

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<jsp:useBean class="magic.MemberBean" id="memBean"/>
<jsp:setProperty property="*" name="memBean" /><%--ν•œλ°©μ— λͺ¨λ“  값을 λ„£μŒ --%>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	당신이 μž…λ ₯ν•œ μ •λ³΄μž…λ‹ˆλ‹€.
	<hr> μ•„ 이 λ””:
	<jsp:getProperty property="id" name="memBean"/><%--κ°’ 가지고 였기 --%>
	<br> 이름:
	<jsp:getProperty property="name" name="memBean"/><%--κ°’ 가지고 였기 --%>
	<br> νšŒμ›κ΅¬λΆ„ : 
	<%
		if(memBean.getMclass() ==1){
			out.print("μΌλ°˜νšŒμ›");
		}else{
			out.print("κ΅μˆ˜λ‹˜");			
		}
	%>
	<br> μ „ν™”λ²ˆν˜Έ : 
	<jsp:getProperty property="phone1" name="memBean"/>-
	<jsp:getProperty property="phone2" name="memBean"/>-
	<jsp:getProperty property="phone3" name="memBean"/>
</body>
</html>

레퍼런슀

0개의 λŒ“κΈ€