IT story/JSP

[Jsp 강의] L04JSP

jason719 2016. 10. 27. 15:52

L04JSP

index.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp에 대해서 알아보자</title>
</head>
<body>
    <h1>jsp 사용법</h1>
    <hr>
    <h3><a href="./L01DataType.jsp">jsp의 데이터 타입</a></h3>
    <h3><a href="./L02Import.jsp?key=jsp&key2=lesson">jsp에서 다른 class import 하는 방법</a></h3>
    <h3><a href="./L03GlobalV.jsp">jsp에서 전역변수 설정하기</a></h3>
    <h3><a href="./L04Method.jsp">jsp에서 함수 사용하는 법</a></h3>
    <h3><a href="./L05If.jsp?id=jspLesson&pwd=admin1234">jsp에서 if를 사용해서 Login 하기</a></h3>
</body>
</html>
cs

L01DataType.jsp으로 jsp의 데이터 타입에 대해 알아보자

예제1 L01DataType.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>기본 데이터 타입을 알아보자</title>
</head>
<body>
    <h1>기본 데이터 타입</h1>
    
<%
char a = 'a';
String b = "Hello jsp";
int c = 10;
long l = 22222222222222L;
float f = 2.5F;
double d = 255.22;
%>
<% out.print("<h3> char a:"+a+"</h3>"); %>
<h3>String b: <%=%></h3>
<%-- <%= %>는 out.print();의 괄호 내부  --%>
<h3>int c: <%=%></h3>
<h3>long l: <%=%></h3>
<h3>float f: <%=%></h3>
<h3>double d: <%=%></h3>
<h3>++c : <%=++%></h3>
<h3>--c : <%=--%></h3>
<h3>c/3 : <%=c/3 %></h3>
<h3>c%3 : <%=c%3 %></h3>
<h3>l+c : <%=l+%></h3>
<h3>d/c : <%=d/%></h3>
<h3>d/f : <%=d/%></h3>
<h3>d-0.22: <%=d-0.22 %></h3>
<h3>d*3 : <%=d*3 %></h3>
 
</body>
</html>
cs

L02Import.jsp으로 jsp에서 다른 class import 하는 방법에 대해 알아보자

예제2 L02Import.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
40
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Set"%>
<!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=UTF-8">
<title>jsp에서 다른 class import 하는 방법</title>
</head>
<body>
    <%    
        String key = request.getParameter("key");
        String key2 = request.getParameter("key2");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("one"1);
        map.put("two"2.0);
        map.put("three""3");
        map.put("four",    '4');
        map.put("key",key);
        map.put("key2", key2);
        //Set, Iterator로 map의 모든 자원을 출력하세요. or foreach로 출력
        out.print(map);
        
        Set<Map.Entry<String,Object>> entries = map.entrySet();
        Iterator<Map.Entry<String,Object>> it = entries.iterator();
        while(it.hasNext()){
            Map.Entry<String,Object> entry = it.next();
    %>
        <p><b><%=entry.getKey() %></b><%=entry.getValue() %></p>
    <%
        }
        for(Map.Entry<String, Object> entry : entries){
            out.print(entry.getKey()+"="+entry.getValue()+", ");
        }
    %>
</body>
</html>
cs

L03GlobalV.jsp으로 jsp에서 전역변수 설정에 대해 알아보자

L03GlovalV.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
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>전역변수와 servlet 생성자에 대해 알아보자!!</title>
</head>
<body>
<%!
//servlet은 페이지를 처음 한 번 요청시 생성자가 호출된다.
//->자기 자신을 생성자로 가지고 있다.
//main() -> 역할은 doGet()이 대신 한다.
//main() a()를 호출하려면 생성자를 생성하고 호출해야한다. new Class().a();
//doGet() 바로 호출 가능 a();
//자기 자신은 생성자 없이 호출 가능함.
//public L03GlobalV(){} -> JSP 생성자를 만들 수 없다. servlet은 가능
 
int a = 10;
public int a(){
    int a = 20;
    return a;
}
%>
<h1>자바처럼 전역변수와 메소드를 설정하고 불러오자</h1>
<%
//PrintWriter out = response.getWriter(); 에서 가져온다.
out.print("<h3>전역변수: "+this.a+"</h3>");
out.print("<h3>a().a: "+a()+"</h3>");
%>
</body>
</html>
cs

L04Method.jsp으로 jsp에서 함수 사용하는 법에 대해 알아보자

L04Method.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 language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>method를 이용해서 성적프로그램을 만들어보자!!</title>
</head>
<%!
public String gradeDemo(int score){
    String grade,plus;
    switch(score/10){
    case 10case 9: grade="A"break;
    case 8: grade="B"break;
    case 7: grade="C"break;
    case 6: grade="D"break;
    default : grade="F"break;
    }
    plus=(((score+5)/10)-(score/10)==1 || score==100)?"+":"";
    return grade+plus;
}
//삼항연산자로 +도 만들어보자!! 
%>
<body>
    <h1>성적 프로그램</h1>
    <h3>당신의 성적은 100점 : 학점은 : <%=gradeDemo(100%> </h3>
</body>
</html>
cs

L05If.jsp으로 jsp에서 if를 사용해서 Login 하기에 대해 알아보자

L05If.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
40
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>if문을 사용해서 login 하자</title>
</head>
<body>
<%!
String db_id = "jspLesson";
String db_pwd = "admin1234";
public boolean loginDemo(String id, String pwd){
    if(db_id.equals(id) && db_pwd.equals(pwd)){
        return true;
    }else{
        return false;
    }
}
%>
    <%
    String para_id = request.getParameter("id");
    String para_pw = request.getParameter("pwd");
    Boolean login = loginDemo(para_id,para_pw);
    %>
    <h1>파라미터로 받아온 id와 pwd로 로그인 성공하자.</h1>
    <hr>
    
    <%
        if(login){
    %>
        <h2 style="color: blue;">로그인 성공</h2>
    <%    }else{
    %>
        <h2 style="color: red;">로그인 실패</h2>
    <%    }
    
    %>
</body>
</html>
cs


'IT story > JSP' 카테고리의 다른 글

[Jsp 강의] L06Forward  (0) 2016.10.27
[Jsp 강의] L05Redirect  (0) 2016.10.27
[Jsp 강의] L03ServletMethod  (0) 2016.10.27
[Jsp 강의] L02HelloJsp  (0) 2016.10.27
[Jsp 강의] L01HelloServlet  (0) 2016.10.27