Web programming

(22.11.15)Web ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ์›น ํ”„๋กœ๊ทธ๋ž˜๋ฐ, html ํƒœ๊ทธ ์‹ค์Šต

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 11. 15.

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์— ํ‘œ์‹œ๋˜์—ฌ ๋„˜์–ด ๊ฐ„๋‹ค.

๋Œ“๊ธ€