JSP
[JSP]JSP 페이지 내장객체
bangle0621
2020. 12. 24. 00:27
JSP 웹 쇼핑몰 프로그래밍 기본 과정(JSP WEB Programming) - 인프런
JSP 기초 부터 차량 구매 쇼핑몰 프로젝트까지 완성할 수 있는 강의로, 학습을 마치면 바로 실전 프로젝트를 수행할 수 있게 됩니다. (본 강의는 질의응답을 제공하지 않는 강의입니다.) 초급 웹
www.inflearn.com
해당 강의로 공부중인 내용입니다. 문제가 되면 비공개로 전환하겠습니다.
자주 사용되어지는 객체. 별도의 import없이도 사용 가능하다
내장 객체 | 리턴 타입 | 설명 |
request | javax.servlet.http.HttpServletRequest | 웹 브라우저의 요청 정보를 저장하고 있는 객체 로그인 페이지에 정보를 넣어주면 DB의 데이터와 비교 요청.이러한 일처리 |
response | javax.servlet.http.HttpServletResponse | 웹 브라우저 요청에 대한 응답 정보를 저장하고 있는 객체 |
out | javax.servlet.jsp.jsp.jspWriter | JSP페이지에서 생성된 결과를 브라우저레 출력할 때 사용하는 객체 |
session | javax.servlet.http.HttpSession | 웹 브라우저의 정부를 유지하기 위해 정보를 저장하고 있는 객체 (쿠키와 세션 파트에서 다룬다.) |
application | javax.servlet.ServletContext | 웹 어플리케이션 전체에 공유하는 정보를 저장하고 있는 객체(사용자에게는 보여주면 안된다!) |
pageContext | javax.servlet.jsp.PageContext | 다른 페이지에 대한 정보를 불러가져 올때 |
page | java.lang.Object | JSP 페이지를 구현한 자바 클래스 객체 |
config | javax.servlet.ServletConfig | JSP 페이지에 대한 설정 정보를 저장하고 있는 객체 |
exception | java.lang.Throwable | JSP 페이지서 예외가 발생한 경우에 사용되는 객체 |
request
RequestLogin.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 안에 있는 데이터만 다음으로 넘어간다.
get - 정보가 노출된다.
post - 정보를 숨겨서 넘긴다.-->
<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">
<!-- submit 을 누르면 action 에서 지정된 곳으로 넘어간다 (문법) -->
<input type="submit" value="전송">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
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에서 넘어온 아이디와 패스워드를 읽어들임 -->
<%
//가져올 땐 get. 부여할 땐 set. 사용자의 정보가 저장되어있는 객체가 request. getParameter() 사용자의 정보를 추출
//getParameter 가 String타입으로 return 되어서 String 타입으로 가져온 것.
//request 의 범위는 해당페이지까지만 존재한다. 다음 페이지로 가게 되면 전송되지 않는다 (세션 기능과 관련이 있다.)
String id = request.getParameter("id");//사용자의 id 값을 읽어들여서 변수 id 에 저장해라.
String pass = request.getParameter("pass");
%>
<h2>
당신의 아이디는 <%=id %>이고 패스워드는 <%=pass %>입니다.
</h2>
</body>
</html>
회원가입창 작성
<%@ 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 width = "500" border ="1">
<tr height = "50">
<td width="150" align="center">아이디</td>
<td width = "350" align="center"> <input type="text" name="id" size="40">
</td></tr>
<tr height = "50">
<td width="150" align="center">패스워드</td>
<td width = "350" align="center"> <input type="password" name="pass1" size="40">
</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" placeholder="">
</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 height = "50">
<td width="150" align="center">당신의 관심분야</td>
<td width="350" align="center">
<!-- 배열로 넘어와야 하기때문에 name 을 동일하게 작성해주어야 한다. -->
<input type="checkbox" name="hobby" value="캠핑">캠핑
<input type="checkbox" name="hobby" value="등산">등산
<input type="checkbox" name="hobby" value="독서">독서
<input type="checkbox" name="hobby" value="영화">영화
</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대
<input type="radio" name="age" value="20">20대
<input type="radio" name="age" value="30">30대
<input type="radio" name="age" value="40">40대
</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 align="center" colspan="2">
<input type="submit" value="회원가입">
<input type="reset" value="취소">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
회원가입 정보
<%@ 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>
<center>
<h2>회원 정보 보기</h2>
<%
//post 방식으로 데이터가 넘어올 떄 한글이 깨질 수 있기세
request.setCharacterEncoding("UTF-8");
//각종 사용자로부터 넘어온 데이터를 저장해 줌.
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");
//배열 []열 타입으로 리턴
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"> <%= pass2 %>
</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문을 돌려서 하나씩 값을 받아야한다.-->
<%
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"> <%= job %>
</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>
위의 오류가 났을때 JSP의 기본 인코딩을 euc-kr 로 업데이트 해줬다 (기존에는 ISO -8859-1? 로 되어있었다)
그래도 오류는 계속 떠서 그냥 Save as UTF-8 눌러줬다....
페이지 post로 전송했을때 한글깨짐 현상이 일어나서 전부 다 UTF-8로 변경해주었다.
IPv4 주소 옆에 내 IP주소를 확인할 수 있다.
IP주소:8090/JSP1/RequestJoin.jsp
를 입력해도 해당 페이지를 볼 수 있다.(핸드폰은 와이파이 환경에서만)
response
addCookie(세션시간에 따로 다룸)
sendRedirect
페이지를 이동시키는 메소드
로그인 페이지에서 submit 눌렀을 시 밑의 페이지로 간다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<body>
<h2>로그인 처리 페이지</h2>
<%
request.setCharacterEncoding("UTF-8");
//임의적으로 id 와 pass 를 설정
String dbid = "aaaa";
String dbpass ="1234";
//사용자로부터 넘어온 데이터를 입력 받아줌
String id = request.getParameter("id");
String pass = request.getParameter("pass");
//db 데이터와 넘어온 데이터를 비교한다.
if(dbid.equals(id)&&dbpass.equals(pass)){
//아이디와 패스워드가 일치하니까 메인페이지를 보여주어야 한다. 데이터를 받아서 넘기고 싶으면 ?데이터 명. 파라미터에 담아서 넘기지 않으면 다음페이지까지 정보가 안넘어간다.
response.sendRedirect("ResponseMain.jsp?id="+id);
}else{
%>
<script>
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("UTF-8");
%>
<h2><%=request.getParameter("id") %>님 반갑습니다.</h2>
</body>
</html>
OUT
<%@ 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 %> 이 화면에 출력
<p>
<%
out.println(name + "이 화면에 출력");
%>
</body>
</html>
Session
<%@ 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 = "shin";
//세션을 이용하여 데이터를 유지시키고 싶다
session.setAttribute("name1", name);
//세션 유지시간
session.setMaxInactiveInterval(10);//10초간 로그인 유지
%>
<a href="SessionName.jsp">세션네임페이지로 이동</a>
</body>
</html>
<%@ 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 name1=(String)session.getAttribute("name1");
%>
<%=name1 %>님 반갑습니다.
</body>
</html>