<_jspService() >
- jsp ==> _jspService() 안쪽으로 들어가게 된다.
* _jspService()의 지역 변수 : 내장 객체
내부적으로 선언되어 있어 따로 선언하지 않아도 되는 객체
* out, request, response (... pagecontext, session, application... 등등)
- pageContext : 현재 JSP 페이지의 컨텍스트를 나타내는 객체
- request : 사용자의 요청을 객체화 시켜 놓은 것
- session : 웹 브라우저의 정보를 유지하기 위한 세션 정보를 저장하고 있는 객체
- application : 웹 application Context의 정보를 저장하고 있는 객체
* forward/redirect
- forward
RequestDispatcher rd = request.getRequestDispatcher("page2.jsp");
rd.forward(request, response);
=> 포워드는 request, response 객체를 가지고 이동하기 때문에 다른 페이지에서도 꺼낼 수 있음
- redirect
response.sendRedirect("page2.jsp");
=> 사용자가 자원을 요청하면 새로운 요청처럼 page2.jsp에 접근하도록
- [page1.jsp] : set하기
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page1.jsp</title>
</head>
<body>
<h2>page1.jsp 입니다.</h2>
<%
// jsp ==> _jspService() 안쪽으로 들어가게 된다.
// _jspService()의 지역 변수 : 내장 객체
// 내부적으로 선언되어 있어 따로 선언하지 않아도 되는 객체
// out, request, response
// pageContext : 현재 JSP 페이지의 컨텍스트를 나타내는 객체
// request : 사용자의 요청을 객체화 시켜 놓은 것
// session : 웹 브라우저의 정보를 유지하기 위한 세션 정보를 저장하고 있는 객체
// application : 웹 application Context의 정보를 저장하고 있는 객체
// 내장 객체 속성, 값 지정
pageContext.setAttribute("id1", "aaa");
request.setAttribute("id2", "bbb");
session.setAttribute("id3", "ccc");
application.setAttribute("id4", "ddd");
Object obj1 = pageContext.getAttribute("id1"); // 이름으로 값 꺼내기
String id1 = (String)obj1; // string으로 형변환
Object obj2 = request.getAttribute("id2");
String id2 = (String)obj2;
Object obj3 = session.getAttribute("id3");
String id3 = (String)obj3;
Object obj4 = application.getAttribute("id4");
String id4 = (String)obj4;
out.println("<h3> pageContext : " + id1 + "</h3>");
out.println("<h3> request : " + id2 + "</h3>");
out.println("<h3> session : " + id3 + "</h3>");
out.println("<h3> application : " + id4 + "</h3>");
// * forward
/* RequestDispatcher rd = request.getRequestDispatcher("page2.jsp");
rd.forward(request, response); // 포워드는 request, response 객체를 가지고 이동하기 때문에 다른 페이지에서도 꺼낼 수 있음!
*/
// * redirect
response.sendRedirect("page2.jsp"); // 사용자가 자원을 요청하면 새로운 요청처럼 page2.jsp에 접근하도록
%>
</body>
</html>
|
cs |
- [page2.jsp] 1에서 set한 값 가져오기
>> page1으로 실행! > page1 실행하면 page2의 값 포워딩해서 보여줌
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
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>page2.jsp</title>
</head>
<body>
<h2>현재 페이지는 page2.jsp 입니다.</h2>
<%
out.println("page2.jsp : " + request);
Object obj1 = pageContext.getAttribute("id1"); // 이름으로 값 꺼내기
String id1 = (String)obj1; // string으로 형변환
Object obj2 = request.getAttribute("id2");
String id2 = (String)obj2;
Object obj3 = session.getAttribute("id3");
String id3 = (String)obj3;
Object obj4 = application.getAttribute("id4");
String id4 = (String)obj4;
out.println("<h3> pageContext : " + id1 + "</h3>");
out.println("<h3> request : " + id2 + "</h3>");
out.println("<h3> session : " + id3 + "</h3>");
out.println("<h3> application : " + id4 + "</h3>");
%>
</body>
</html>
|
cs |
<로그인 / 회원가입 페이지 만들기>
- [login.jsp] : 유효성 검사를 위해 submit을 button으로.. 그리고 자바스크립트로 작성
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
|
<%@page import="vo.MemberVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login.jsp</title>
<!-- 유효성 검사 -->
<script type="text/javascript">
window.onload = function(){
var btn = document.getElementById("btn1");
btn.onclick=function(){
console.log("하하하");
var frm = document.frm;
// 입력값 유효성 검사
frm.action = "loginOk.jsp";
frm.method = "get";
frm.submit();
}
}
</script>
</head>
<body>
<%
// 로그인 되어있지 않을 때만 로그인창 뜨도록
Object obj = session.getAttribute("vo"); // 오브젝트가 있는지 가져옴
if( obj != null){ // 로그인 되어 있는 상태
MemberVO vo = (MemberVO)obj;
out.println("<h3><a href='logout.jsp'>" + vo.getName() + "님 환영합니다. </h3>");
} else {
%>
<form action="loginOk.jsp" name="frm" method="post">
<table>
<tr>
<th>ID</th>
<td><input type="text" name="id" id="id" /></td>
</tr>
<tr>
<th>PW</th>
<td><input type="password" name="pwd" id="" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="로그인" id="btn1" />
<a href="register.jsp"><input type="button" value="회원가입" id="btn2" /></a>
</td>
</tr>
</table>
</form>
<%
}
%>
</body>
</html>
|
cs |
- [loginOk.jsp] : memberdao, membervo로 db 연결
> 내장 객체, 세션 저장, 리다이렉트
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 import="vo.MemberVO"%>
<%@page import="dao.MemberDAO"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>LoginOk.jsp</title>
</head>
<body>
<%
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
/* out.println("<h3> ID : "+id+"</h3>");
out.println("<h3> PW : "+pwd+"</h3>"); */
// dao, vo 사용 => db연결
MemberDAO dao = new MemberDAO();
MemberVO vo = dao.getOne(id, pwd);
if(vo != null){
out.println("<h1>" + vo.getName() + "님 환영합니다. </h1>");
// session 내장 객체 저장
session.setAttribute("vo", vo); // vo를 세션에 저장
out.println("<a href='login.jsp'>로그인 페이지로 이동 </a>");
} else {
response.sendRedirect("login.jsp"); // 메시지 출력 않고 바로 redirect 되도록
/* out.println("<h1> 로그인 실패 </h1>"); */
}
%>
</body>
</html>
|
cs |
- [logout.jsp]
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
// 이 부분은 사용자에게 무언가 보여줄 내용이 없음
session.invalidate(); // 세션을 무효화
// 로그인 페이지로 리다이렉트
response.sendRedirect("login.jsp");
%>
|
cs |
- [register.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>register.jsp</title>
<style>
#div1{
position: relative;
left: 40%;
}
#div2{
position: relative;
left: 40%;
}
</style>
</head>
<body>
<div id="div1"><h1>가입신청서</h1></div>
<form action="registerOk.jsp">
<table>
<tr>
<td>ID: </td>
<td><input type="text" name="id"/></td>
</tr>
<tr>
<td>NAME: </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>주민등록번호: </td>
<td><input type="text" value="111111" name="number1"/> -</td>
<td><input type="text" name="number2"/></td>
</tr>
<tr>
<td>비밀번호: </td>
<td><input type="password" value="1111" name="pw" /></td>
</tr>
<tr>
<td>전화번호: </td>
<td><input type="text" value="010" name="phone1" /> -</td>
<td><input type="text" value="1234" name="phone2" /> -</td>
<td><input type="text" value="5678" name="phone3" /></td>
</tr>
<tr>
<td>주소: </td>
<td><input type="text" value="서울 종로구 XX동" name="addrs" /></td>
</tr>
<tr>
<td>EMAIL: </td>
<td><input type="text" name="email1" /> @</td>
<td><input type="text" name="email2"/></td>
<td>
<select name="email" name="">
<option value="직접입력">직접입력</option>
<option value="naver.com">naver.com</option>
<option value="daum.net">daum.net</option>
</select>
</td>
</tr>
<tr>
<td>성별: </td>
<td>
<input type="radio" name="gender" value="남" />남
<input type="radio" name="gender" value="여" />여
</td>
</tr>
<tr>
<td>취미: </td>
<td>
<input type="checkbox" name="motive" id="음악감상" />음악감상
<input type="checkbox" name="motive" id="독서" />독서
<input type="checkbox" name="motive" id="운동" />운동
</td>
</tr>
</table>
<div id="div2">
<input type="submit" value="가입하기" />
<input type="button" value="취소하기" />
</div>
</form>
</body>
</html>
|
cs |
- [registerOk.jsp] : 회원가입 정보 > 데이터베이스로 저장
+ timeout
+ memberDAO의 no를 시퀀스 넘버로 수정
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
|
<%@page import="org.eclipse.jdt.internal.compiler.batch.Main"%>
<%@page import="dao.MemberDAO"%>
<%@page import="vo.MemberVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>registerOk.jsp</title>
</head>
<body>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String motive = request.getParameter("motive");
MemberDAO dao = new MemberDAO();
MemberVO vo = new MemberVO();
// MemberVO vo = new MemberVO(88, id, pw, name, gender, motive);
vo.setId(id);
vo.setPw(pw);
vo.setGender(gender);
vo.setName(name);
dao.addOne(vo); // dao를 통해 데이터베이스에 저장
dao.close();
%>
<h2>짝짝짝 회원가입을 축하합니다.</h2>
<h2>3초 후에 자동으로 login.jsp로 이동합니다. (자바스크립트)</h2>
<script type="text/javascript"> // 자바스크립트 기본이라 보통 생략
window.setTimeout(function(){
location.href="login.jsp";
}, 3000);
</script>
</body>
</html>
|
cs |
- [test.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>
<html>
<head>
<meta charset="UTF-8">
<title>test.jsp</title>
</head>
<body>
<%
int a = 10;
out.println("<h1>" + a + "</h1>");
%>
<script type="text/javascript">
<%
for(int i = 0; i<100; i++) {
%>
console.log("나 집에 갈래...");
<%
}
%>
</script>
</body>
</html>
|
cs |
'web' 카테고리의 다른 글
[jsp] jsp 파일 불러오기 - include & action tag (0) | 2023.04.30 |
---|---|
[jsp] VO/DAO 실습(Emp/EmpDept) (0) | 2023.04.30 |
[JSP] 데이터베이스 파일(DEPT) 가져와서 출력<VO/DAO> (0) | 2023.04.30 |
[jsp] <로그인 페이지 만들기(db 연결!)> : member 테이블 사용 + lombok 다운로드 (0) | 2023.04.30 |
[jsp] jsp (jsp의 스크립트 요소/button/text/for문/request&response) (0) | 2023.04.23 |