간단하게 사용하는 코드를 저장하고, 자세한 설명은 다른 게시물에 추가하도록 하자.

 

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<%@ 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>
 
<%-- 
 
    내장 객체 : 동적으로 할당(생성)하지 않고 (언제든지 바로) 사용할 수 있는 객체
    
    out: web으로 출력
    request
    response
    session
    pageContext
    application
         :
--%>
 
<h3>out(web에 출력)</h3>
 
<%
    String title = "hello jsp"
    out.println("<p>" + "hello jsp" + "</p>");
    out.println("<p>" + title + "</p>"); // = servlet java(html)
%>
 
<p><%=title %></p>
 
<h3>request</h3>
<%
// request(요청) : parameter 값을 취득, session을 접근, Object를 전송, encoding 설정 
// HttpServletRequest
// encoding 설정
request.setCharacterEncoding("utf-8");
 
// parameter 값을 취득
// 값 한개 취득(getParameter)
String name = request.getParameter("name");
// url에 "?name=홍길동"을 추가하면,
%>
<p><%=name %></p> <!-- 홍길동 -->
 
<%
// 값 여러개 취득(getParameterValues)
    String[] hobby = request.getParameterValues("hobby");
    // url에 ?hobby=book&hobby=game&hobby=running 추가
    for(int i = 0;i < hobby.length; i++){
    %>
        <p><%=hobby[i] %></p>
    <%    
    }
%>
 
<%
// session에 접근
session = request.getSession(); // 세션 생성 및 얻기
session.setAttribute("visited""1"); // 세션 이름: visited, 세션 값 1 저장
String str = (String)session.getAttribute("visited");
// request.getSession().getAttribute("세션명")의 리턴 타입이 Object이므로 캐스트 연산을 해야한다.
out.println("<h1>" + str + "</h1>"); // 1
 
%>
 
 
 
 
</body>
</html>
cs

 

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
package sample02;
 
import java.io.Serializable;
 
public class Human implements Serializable{
    String name;
    int age;
    
    public Human() {
    }
 
    public Human(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
 
    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;
    }
    
    
    
}
 
cs

 

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 import="sample02.Human"%>
<%@ 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>
 
<h3>response</h3>
<%
// response: 이동
// response.sendRedirect("이동할 jsp 페이지"): 페이지 이동
// String name = "Tom";
// response.sendRedirect("defaultCopy.jsp?name=" + name + "&age=" + 15);
%>
 
<h3>pageContext</h3>
<
String name = "성춘향";
int age = 16;
 
// 마치 짐을 싸듯이...!
Human human = new Human(name, age);
// human이라는 참조변수가 가리키는 객체를 lady라는 이름으로 담아 보낼 준비
request.setAttribute("lady", human);
 
pageContext.forward("defaultCopy.jsp");
 
 
 
%>
 
</body>
</html>
cs

 

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 import="sample02.Human"%>
<%@ 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>
 
<h1>defaultCopy.jsp</h1>
 
<%--
    String name = request.getParameter("name");
    int age = Integer.parseInt(request.getParameter("age"));
--%>
<%-- 
<p><%=name %></p> 
<p><%=age %></p> 
 --%>
 
<%
    Human human = (Human)request.getAttribute("lady");
%>
<p>이름:<%=human.getName() %></p>
<p>나이:<%=human.getAge() %></p>
 
</body>
</html>
cs

'Java > JSP, Servlet' 카테고리의 다른 글

JSP와 Servlet 사이에 데이터 주고 받기(json)  (0) 2023.02.08
Servlet 정리  (0) 2023.02.06

+ Recent posts