CH01 내장객체(09~15강)

Gm·2021년 9월 13일
0

JSP 인프런강의

목록 보기
1/4

1. Request 객체 실습1

* request 객체는 다음 다음 페이지로는 결과값을 넘겨주지 못한다.

(바로 다음페이지로만 form태그와 a태그를 통해 값을 넘겨줄 수 있음)

<RequestLogin.jsp>

<body>
	<center>
	<h2>로그인 페이지</h2>
		<form action="RequestLoginProc.jsp" method="post">
			<table width="400" border="1">
				<tr height="60">
					<td align="center" width="150">아이디</td>
					<td align="left" width="250">
						<input type="text" name="id">						
					</td>
				</tr>
				<tr height="60">
					<td align="center" width="150">비밀번호</td>
					<td align="left" width="250">
						<input type="password" name="pass">					
					</td>
				</tr>
				<tr height="60">
					<td colspan="2" align="center">
						<input type="submit" value="전송">
					</td>
				</tr>
			</table>						
		</form>	
	</center>
 </body>

==> 결과

<RequestLoginProc.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<!-- RequestLogin에서 넘어온 아이디와 비밀번호를 읽는 방법 2가지 -->
<%
	// 사용자의 정보가 저장되어있는 객체 request객체의 getParameter() 메서드를 이용해서 사용자의 정보를 추출
	String id = request.getParameter("id"); // 사용자의 id값을 읽어드려서 변수 id에 저장하시오
	String pass = request.getParameter("pass");
%>
	당신의 아이디는 <%=id %> 이고 비밀번호는 <%=pass %> 입니다.	
	<h2>	
	<a href="RequestForward.jsp">다음 페이지</a>
	</h2>		
</body>
</html>

==> 결과
당신의 아이디는 darkoz 이고 비밀번호는 1234 입니다.
다음 페이지

<RequestForward.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<!-- RequestLogin에서 넘어온 아이디와 비밀번호를 읽는 방법 2가지 -->
<%
	// 사용자의 정보가 저장되어있는 객체 request객체의 getParameter() 메서드를 이용해서 사용자의 정보를 추출
	String id = request.getParameter("id"); // 사용자의 id값을 읽어드려서 변수 id에 저장하시오
	String pass = request.getParameter("pass");
%>
	<h2>
	RequestForward 페이지 입니다. <br>
	당신의 아이디는 <%=id %> 이고 비밀번호는 <%=pass %> 입니다.	
	</h2>
</body>
</html>

==> 결과
RequestForward 페이지 입니다.
당신의 아이디는 null 이고 비밀번호는 null 입니다.

===> request 객체는 다음 다음페이지로는 결과값을 넘겨주지 못한다.

(바로 다음페이지로만 form태그와 a태그를 통해 값을 넘겨줄 수 있음)



1. Request 객체 실습2

==> 결과

<RequestJoin.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center>
		<h2>회원 가입</h2>
		<form action="RequestJoinProc.jsp" method="post">
			<table border="1" width="500">
				<tr height="50">
					<td width="150" align="center">아이디</td>
					<td width="350" align="center">
						<input type="text" name="id" size="40" placeholder="id를 넣으세요">
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center">비밀번호</td>
					<td width="350" align="center">
						<input type="password" name="pass1" size="40" value="0000">
					</td>					
				</tr>
				<tr height="50">
					<td width="150" align="center">비밀번호 확인</td>
					<td width="350" align="center">
						<input type="password" name="pass2" size="40">
					</td>					
				</tr>
				<tr height="50">
					<td width="150" align="center">이메일</td>
					<td width="350" align="center">
						<input type="email" name="email" size="40">
					</td>					
				</tr>
				<tr height="50">
					<td width="150" align="center">전화번호</td>
					<td width="350" align="center">
						<input type="tel" name="tel" size="40">
					</td>					
				</tr>
				<tr>
					<td width="150" align="center">당신의 관심분야</td>
					<td width="350" align="center">
						<input type="checkbox" name="hobby" value="캠핑"> 캠핑 &nbsp;&nbsp;
						<input type="checkbox" name="hobby" value="등산"> 등산 &nbsp;&nbsp;
						<input type="checkbox" name="hobby" value="영화"> 영화 &nbsp;&nbsp;
						<input type="checkbox" name="hobby" value="독서"> 독서 &nbsp;&nbsp;
					</td>					
				</tr>
				<tr height="50">
					<td width="150" align="center"> 당신의 직업은</td>
					<td width="350" align="center">
						<select name="job">
							<option value="교사">교사</option>
							<option value="변호사">변호사</option>
							<option value="의사">의사</option>
							<option value="기술사">기술사</option>							
						</select>
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center">당신의 연령은?</td>
					<td width="350" align="center">
						<input type="radio" name="age" value="10">10대 &nbsp;&nbsp;
						<input type="radio" name="age" value="20">20대 &nbsp;&nbsp;
						<input type="radio" name="age" value="30">30대 &nbsp;&nbsp;
						<input type="radio" name="age" value="40">40대 &nbsp;&nbsp;						
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center"> 하고싶은 말</td>
					<td width="350" align="center">
						<textarea rows="5" cols="40" name="info"></textarea>
					</td>
				</tr>
				<tr height="50">
					<td colspan="2" align="center"> 
						<input type="submit" value="회원 가입">
						<input type="reset" value="취소">
					</td>
				</tr>
			</table>
		</form>
	</center>
</body>
</html>

<RequestJoinProc.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center>
	<h2>회원 정보 보기</h2>	
	<% 
	// post 방식으로 데이터가 넘어올때 한글이 깨질 수 있기에
	request.setCharacterEncoding("EUC-KR"); 	
	// 각종 사용자로부터 넘어온 데이터를 저장해줌
	String id = request.getParameter("id");
	String pass1 = request.getParameter("pass1");
	String pass2 = request.getParameter("pass2");
	String email = request.getParameter("email");
	String tel = request.getParameter("tel");	
	// []배열 타입으로 리턴
	//getParameterValues() 메서드는 순서없이 모든 데이터(중복선택 된 데이터)가 저장
	String[] hobby = request.getParameterValues("hobby"); 
	String job = request.getParameter("job");
	String age = request.getParameter("age");
	String info = request.getParameter("info");		
	if(!pass1.equals(pass2)){
%>
	<script type="text/javascript">
		alert("비밀번호가 틀립니다."); // 경고창
		history.go(-1);
	</script>
<%
	}
	%>	
	<table width="500" border="1">
		<tr height="50">
					<td width="150" align="center">아이디</td>
					<td width="350" align="center">
						<%=id %>
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center">비밀번호</td>
					<td width="350" align="center">
						<%=pass1 %>
					</td>					
				</tr>				
				<tr height="50">
					<td width="150" align="center">이메일</td>
					<td width="350" align="center">
						<%=email%>
					</td>					
				</tr>
				<tr height="50">
					<td width="150" align="center">전화번호</td>
					<td width="350" align="center">
						<%=tel%>
					</td>					
				</tr>
				<tr height="50">
					<td width="150" align="center">관심분야</td>
					<td width="350" align="center">
					<%
						for(int i=0; i<hobby.length; i++){
							out.write(hobby[i]+" ");
						}
					%>
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center">직업은</td>
					<td width="350" align="center">
						<%=tel%>
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center">나이대</td>
					<td width="350" align="center">
						<%=age%>
					</td>
				</tr>
				<tr height="50">
					<td width="150" align="center">하고싶은 말</td>
					<td width="350" align="center">
						<%=info%>
					</td>
				</tr>
	</table>
	</center>
</body>
</html>



2. Rsponse 객체 실습1

  • response.sendRedirect 메서드 사용시
    • 현재 페이지를 화면에 표시하지 않고 다음 페이지로 넘어감!!

      ==>결과

      <ResponseSendRedirect.jsp>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<center>
	<h2>로그인 페이지</h2>
		<form action="ResponseLoginProc.jsp" method="post">
			<table width="400" border="1">
				<tr height="60">
					<td align="center" width="150">아이디</td>
					<td align="left" width="250">
						<input type="text" name="id">						
					</td>
				</tr>
				<tr height="60">
					<td align="center" width="150">비밀번호</td>
					<td align="left" width="250">
						<input type="password" name="pass">						
					</td>
				</tr>
				<tr height="60">
					<td colspan="2" align="center">
						<input type="submit" value="전송">
					</td>
				</tr>
			</table>						
		</form>	
	</center>
</body>
</html>

<ResponseLoginProc.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>로그인 처리 페이지</title>
</head>
<body>
	<h2>로그인 처리 페이지</h2>	
	<%
		request.setCharacterEncoding("euc-kr");	
		//임의적으로 id와 pass를 설정
		String dbid = "aaaa";
		String dbpass = "1234";		
		// 사용자로부터 넘어온 데이터를 입력 받아줌
		String id = request.getParameter("id");
		String pass = request.getParameter("pass");		
		if(dbid.equals(id) && dbpass.equals(pass)){
			// 아이디와 패스워드가 일치하면 페이지를 보여줌
			// response.sendRedirect 메서드 사용시 현재 페이지를 화면에 표시하지 않고 다음 페이지로 넘어감!!
			response.sendRedirect("ResponseMain.jsp?id="+id);
		}else{
	%>
		<script type="text/javascript">
			alert("아이디와 비밀번호를 다시 확인하세요");
			history.go(-1);
		</script>
	<%
		}
	%>	
</body>
</html>

<ResponseMain.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<%
		request.setCharacterEncoding("euc-kr");
		String id = request.getParameter("id");
	%>	
	<h2>
		<%=id %> 님 반갑습니다.
	</h2>
</body>
</html>

3. Session 객체 실습

  • 세션을 이용하여 데이터를 유지


    ==> 결과

    <SessionTest.jsp>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h2>세션 연습</h2>
<%
	String name = "kim";	
	// 세션 : 세션을 이용하여 데이터를 유지
	session.setAttribute("sname", name);
	//세션 유지시간
	session.setMaxInactiveInterval(10); //10초간 세션을 유지
%>
	<a href="SessionName.jsp?name=<%=name%>">세션네임 페이지로 이동</a>
</body>
</html>

<SessionName.jsp>

<%@ page language="java" contentType="text/html; charset=EUC-KR"
   pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
	<h2>세션 네임 페이지 입니다.</h2>
	<%
		String sname = (String)session.getAttribute("sname");
	%>	
	<%=request.getParameter("name")%>
	<br><br>
	<%=sname %>님 session 값입니다.	
</body>
</html>



4. Out 내장객체 실습

  • jsp 스트립트 출력 방법 2가지
    • <%= %>
    • out.println(name+"이 화면2에 출력");

      ==> 결과

      <OutTest.jsp>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
	String name = "알리미 어플";
%>
스크립트로 표현시 <%=name %>이 화면에 출력 <br><br>
<%
	out.println(name+"이 화면2에 출력");		
%>
</body>
</html>

0개의 댓글

관련 채용 정보