ππππ
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>
<%@ 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>
<%@ 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>
<%@ 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>
decl02.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>
<%!
double a = 3.14159;
public double getDouble(){
return a;
}
%>
<%
out.println(getDouble());
%>
</body>
</html>
expr02.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();
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>
<%@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>
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>
getAttribute(name)
Object
νμ
μ΄λ―λ‘ μ¬μ©μ μ€μ ν λΉλ κ°μ²΄ νμ
μΌλ‘ νλ³ν(Casting)ν΄μΌν¨.getAttributeNames()
3.setAttribute(name, value)
setMaxInactiveInterval()
(κΈ°λ³Έ μ€μ 30λΆ(Serversμ web.xmlμ μ‘΄μ¬ν¨)
session.setAttribute(String name, Object value);
Object getAttribute (String name)Β
<%@ 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
κ° μ‘΄μ¬νμ§ μμμ)
<%@ 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()
λ©μλμ€λͺ
μ 보면 μΈμ
μ λ€μ΄μλ λͺ¨λ κ°μ²΄λ€μ κ°λ€μ κ°μ Έμ€κΈ° μν΄μ μ¬μ―μκ³ , μ΄κ±°νμΌλ‘ λ°νλλ€κ³ νλ€. κ·Έλμ μ΄κ±°ν κ°μ²΄μμ κ°κ° λ½μμ€κΈ° μν΄μλ hasMoreElements
μ nextElement
λ©μλλ₯Ό μ¬μ©ν΄μΌν¨
hasMoreElements
nextElement
<%@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>
<%@ 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>
<%@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>
κ²°κ³Όλ λλ€μΌλ‘λμ΄(μΈλ±μ€κ° μμ΄μ λλ€ μ²λ¦¬κ° λ¨)
<%@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>
νμ΄
<%@ 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>
<%@ 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>
<%@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>
<%@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>
<%@ 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() %>
μλ°λΉ :λ°μ΄ν°λ₯Ό νννλ κ²μ λͺ©μ μΌλ‘ νλ μλ° ν΄λμ€
μλ°λΉ ν΄λμ€λ λ°μ΄ν°λ₯Ό μ μ₯νλ νλ, λ°μ΄ν°λ₯Ό μ½μ΄μ¬ λ μ¬μ©λλ λ©μλ(getproperty),
κ°μ μ μ₯ν λ μ¬μ©λλ λ©μλλ‘ κ΅¬μ±(setproperty)
νλ² λ§λ€μ΄ λκ³ νμν λλ§λ€ κ°μ Έκ°μ μ¬μ©νλ€.
κΈ°λ³Έμ μΌλ‘ JAVAμ ꡬ쑰λ₯Ό κ°μ§κ³ μμ(νλ+λ©μλ)
μ¬κΈ°μ νλλ₯Ό νλ‘νΌν°?λΌκ³ λΆλ₯Έλ€.
<jsp:useBean class="hello.HelloBean" id="myBean" scope="page" />
<jsp:setProperty name="myBean" property="age" value="10" /> = <% myBean.setAge(10); %>
property="*"
λ λͺ¨λ μ 보λ₯Ό κ°μ²΄μ λκΉμ΄λ¦ : <jsp:getProperty property="name" name="myBean"/>
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>
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>