javaBook.jsp๊ฐ ํ๋ฉด์ ํ์๋์ด ์์๋ 'Hello Java'๋ฅผ ํด๋ฆญํ๋ฉด ์๋ฒ์ธก(shopControl.jsp)์ ๊ทธ ๋ฌธ์์ด์ด ์ ์ฅ๋๊ณ pythonBook.jsp๊ฐ ํ๋ฉด์ ํ์ ๋์ด ์์ ๋ 'Easy Python'์ ํด๋ฆญํ๋ฉด ์๋ฒ์ธก(shopControl.jsp)์ ๊ทธ ๋ฌธ์์ด์ด ์ ์ฅ๋๋๋กํ๋ค. ์์ 2๊ฐ ํ์ด์ง๋ฅผ ์ ์ํ ๋ ์น๋ธ๋ผ์ฐ์ ์ฃผ์์ฐฝ์ ์ด์ฉํ๋ผ
์ฃผ์์ฐฝ์ showCart.jsp๋ฅผ ์ ๋ ฅํ๋ฉด ์์์ ์ ์ฅํ 2๊ฐ์ ๋ฌธ์์ด์ด ๋ชจ๋ ํ์ ๋๋๋ก ํ๋ค.
** ํ๊ฐ์ ๋ธ๋ผ์ฐ์ ์ํ๋ฅผ ์ ์ฅํ ์ ์๋ ์์ญ(scope) ๊ฐ์ฒด๋ session์ด๋ฉฐ, jsp์์๋ ์ด๋์์๋ ์ง session์ด๋ผ๋ ์ฐธ์กฐ๋ณ์๋ก ์ ๊ณต๋๋ค.
javaBook.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Java Book </title>
</head>
<body>
<a href="shopControl.jsp?book=Hello java"> Hello Java </a>
</body>
</html>
|
cs |
pythonBook.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Python Book</title>
</head>
<body>
<a href="shopControl.jsp?book=Easy Python"> Easy Python </a>
</body>
</html>
|
cs |
shopControl.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<%@page import="java.util.*"%>
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String book = request.getParameter("book");
List<String> cart = (List<String>)session.getAttribute("cart");
if(cart==null){
cart = new ArrayList<String>();
session.setAttribute("cart", cart);
}
boolean added = cart.add(book);
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> </title>
</head>
<body>
<%= added ?"์ฅ๋ฐ๊ตฌ๋์ ์ ์ฅํ์ต๋๋ค.":"์ฅ๋ฐ๊ตฌ๋ ์ ์ฅ ์คํจ" %>
</body>
</html>
|
cs |
showCart.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<%@page import="java.util.*"%>
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
List<String> cart = (List<String>)session.getAttribute("cart");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> ์ฅ๋ฐ๊ตฌ๋ ๋ด์ฉ๋ณด๊ธฐ</title>
</head>
<body>
<h3>์ฅ๋ฐ๊ตฌ๋ ๋ด์ฉ</h3>
<%
for(int i=0;i<cart.size();i++){%>
<%= i+1%>. <%=cart.get(i)%>
<% }%>
</body>
</html>
|
cs |
์คํ ๊ฒฐ๊ณผ:
ํ์ฌ๋์ด ์ฌ๋ฌ ํ์ด์ง๋ฅผ ์ง๋๋ค๋๋ฉด์ ์ฐ์ถ๋๋ ์ ๋ณด๋ฅผ ์ ์ฅํ๊ธฐ ์ํด์ session ์์ญ ๊ฐ์ฒด๊ฐ ํ์ํ๋ค.
์ฅ๋ฐ๊ตฌ๋ ๋น์ฐ๊ธฐ ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
showCart.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<%@page import="java.util.*"%>
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
List<String> cart = (List<String>)session.getAttribute("cart");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> ์ฅ๋ฐ๊ตฌ๋ ๋ด์ฉ๋ณด๊ธฐ</title>
</head>
<body>
<h3>์ฅ๋ฐ๊ตฌ๋ ๋ด์ฉ</h3>
<%
if(cart!=null){
for(int i=0;i<cart.size();i++){%>
<%= i+1%>. <%=cart.get(i)%>
<% }
}else{%>
<%= "์ฅ๋ฐ๊ตฌ๋๊ฐ ๋น์ด์ ธ ์์ต๋๋ค."%>
<%}
%>
<p>
[<a href="cartEmpty.jsp"> ์ฅ๋ฐ๊ตฌ๋ ๋น์ฐ๊ธฐ </a>]
</body>
</html>
|
cs |
cartEmpty.jsp
1
2
3
4
5
6
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
session.invalidate(); //์ธ์
์ ๋ค์ด์๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ์ง์ด๋ค.
response.sendRedirect("showCart.jsp");
%>
|
cs |
์คํ ๊ฒฐ๊ณผ:
response.sendRedirect("showCart.jsp"); ์๋ต์ ๋ฐ๋ก ๋ค๋ฅธํ์ด์ง(showCart.jsp)๋ก ๋๋ ค๋ฒ๋ ธ๋ค.
์์ฃผ ๊ฐ๋จํ ๋ก๊ทธ์ธ์ฐฝ์ ๋ง๋ค์ด์ ๋ก๊ทธ์ธ์ด ์ฑ๊ณตํ๋ฉด ์ฑ๊ณตํ ์ฌ๋๋ค๋ง main.jsp์ ์ ์ ๊ฐ๋ฅํ๊ฒ ํ๊ณ ,
๋ก๊ทธ์ธ ์คํจํ ์ฌ๋ํ๊ฑฐ๋ ๋ก๊ทธ์ธ ํ์ง ์์ ์ฌ๋์ด url์ ์ณ์ main.jsp์ ๋ค์ด๊ฐ๋ คํด๋ ๋ชป๋ค์ด๊ฐ๊ฒ๋ ๋ง๋ค์ด๋ผ.
login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> ๋ก๊ทธ์ธ ํ๊ธฐ </title>
</head>
<body>
<h3>๋ก๊ทธ์ธ</h3>
<form action="login_proc.jsp" method="post">
<input type="hidden" name="cmd" value="login">
<div><label>์์ด๋</label>
<input type="text" name="uid" value="smith">
</div>
<div><label>์ ํธ</label>
<input type="password" name="pwd" value="1111">
</div>
<div><button type="submit">๋ก๊ทธ์ธ</button></div>
<div><button type="reset">์ทจ์</button></div>
</form>
</body>
</html>
|
cs |
login_proc.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String uid = request.getParameter("uid");
String pwd = request.getParameter("pwd");
boolean pass = false;
if(uid.equals("smith")&&pwd.equals("1111")){
session.setAttribute("login", true);
pass= true;
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๋ก๊ทธ์ธ ๊ฒฐ๊ณผ</title>
<script>
var pass = <%= pass%>;
var msg = pass?"๋ก๊ทธ์ธ ์ฑ๊ณต":"๋ก๊ทธ์ธ ์คํจ";
alert(msg);
if(!pass){
location.href = "login.jsp";
}else{
location.href = "main.jsp";
}
</script>
</head>
<body>
</body>
</html>
|
cs |
main.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
Object obj = session.getAttribute("login");
String msg = null;
boolean pass = true;
if(obj==null){
pass = false;
msg = "ํ์์ผ๋ก ๋ก๊ทธ์ธํด์ผ ์ ์ํ ์ ์์ต๋๋ค.";
//response.sendRedirect("login.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>๋ฉ์ธ ์ปจํ
์ธ ํ์ด์ง </title>
<style type="text/css">
main {width:300px; height:200px; margin:0 auto; background-color:purple;}
</style>
<script type="text/javascript">
var pass = <%=pass%>;
var msg = '<%=msg%>';
if(!pass){
alert(msg);
location.href = "login.jsp";
}
</script>
</head>
<body>
<h1>
ํ์๋ ํ์ํฉ๋๋ค.
[<a href="logout.jsp"> ๋ก๊ทธ์์ </a>]
</h1>
<main>
</main>
</body>
</html>
|
cs |
logout.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
session.removeAttribute("login");
boolean logout = true;
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> </title>
<script type="text/javascript">
var logout = <%=logout%>;
if(logout){
alert("์ ์์ ์ผ๋ก ๋ก๊ทธ์์๋์ต๋๋ค.");
location.href = "login.jsp";
}
</script>
</head>
<body>
</body>
</html>
|
cs |
์คํ ๊ฒฐ๊ณผ :
๋ก๊ทธ์ธ ์ฑ๊ณตํ ๊ฒฝ์ฐ
๋ก๊ทธ์ธ ์คํจ ๊ฒฝ์ฐ
๋ก๊ทธ์ธํ์ง ์๊ณ main.jsp์ ์ ์ํ๋ ค๊ณ ํ ๊ฒฝ์ฐ
๋ก๊ทธ์์
<form>ํ๊ทธ <input type= >์ข ๋ฅ
text, password, radio, checkbox, reset, submit, image, range, date, number, file, hidden
rangeTest.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Range Test </title>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
crossorigin="anonymous"></script>
<script type="text/javascript">
function valuecheck(){
var v = $('#slider').val();
console.log('slider value'+v);
$('#sliderout').val(v);
}
</script>
</head>
<body>
<form>
<input id="slider" name="slider" type="range"
max="100" min="0" step="1" value="50"
oninput="valuecheck();">
<output id="sliderout"></output>
<p>
<button type="button" onclick="valuecheck();">ํ์ธ</button>
</form>
</body>
</html>
|
cs |
์คํ ๊ฒฐ๊ณผ:
input์ด ์๋ ํ์ ๋ค
<select>, <textarea> ๋ฑ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<%@ page contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Range Test </title>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"
integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ="
crossorigin="anonymous"></script>
<script type="text/javascript">
function valuecheck(){
var v = $('#slider').val();
console.log('slider value'+v);
$('#sliderout').val(v);
}
</script>
</head>
<body>
<form>
<input id="slider" name="slider" type="range"
max="100" min="0" step="1" value="50"
oninput="valuecheck();">
<output id="sliderout"></output>
<p>
<input type="date" name="expire">
<p>
<input type="number" name="count" max="100" min="0" step="1" value="13">
<p>
<input type="color" name="color">
<p>
<textarea name="desc" rows="5" cols="20" placeholder="์
๋ ฅํด์ฃผ์ธ์"></textarea>
<p>
<button type="submit">ํ์ธ</button>
</form>
</body>
</html>
|
cs |
์คํ ๊ฒฐ๊ณผ:
๊ฐ์ ์ ๋ ฅ ํด์ฃผ๊ธฐ
ํ์ธ ๋ฒํผ ๋๋ฅด๋ฉด ์ค์ ํ ๊ฐ๋ค์ด url์ ํ์๋์ฌ ๋์ด ๊ฐ๋ค.
๋๊ธ