TIL | 07.20의 기록...
jsp로 넘어가니까 제법 복잡해지는걸..?
Enumeration<String> nameList = request.getParameterNames();
request.getRemoteAddr()
request.getCharacterEncoding()
request.getContentType()
request.getMethod()
request.getProtocol()
request.getRequestURI()
->경로와 파일명request.getContextPath()
request.getServerName()
-> 도메인request.getServerPort()
request.getServletContext().getRealPath("/")
-> 파일의 물리적 주소다른 페이지로 이동하는 객체
//클라이언트 페이지(login.jsp)
---html영역----------
<h1>로그인</h1>
<form method="post" action="loginOk.jsp">
아이디 : <input type="text" name="userid" id="userid"><br>
비밀번호 : <input type="password" name="userpwd" id="userpwd"><br>
<input type="submit" value="로그인">
</form>
//서버 페이지(loginOk.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String userid = request.getParameter("userid");
String userpwd = request.getParameter("userpwd");
if(userid.equals("gildong")&& userpwd.equals("1234")){//로그인 성공
//다른 페이지로 이동
response.sendRedirect(request.getContextPath()+"/index.jsp");
}else{//로그인실패
//response.sendRedirect("/webJSP/jsp02_response/login.jsp");
%>
<script>
history.back();//js의 history객체 : 로그인페이지로 돌아가기
// history.back():이전페이지로이동/history.forward():다음페이지로/history.go(-1):전페이지로
</script>
<%
}
%>
쿠키 : 클라이언의 컴퓨터에 저장된 데이터
<%
//클라이언트 컴퓨터의 쿠키 정보를 서버로 가져오기
Cookie[] coo = request.getCookies();
for(int idx=0;idx<coo.length;idx++){
%>
<li><%=coo[idx].getName() %> : <%=coo[idx].getValue() %></li>
<% }%>
//jsp에서 쿠키 저장하기
//Cookie클래스를 객체로 만들어 response를 이용하여 클라이언트 컴퓨터에 정보를 보내기
Cookie cookie = new Cookie("변수","값");
//소멸시점설정
cookie.setMaxAge(정수); //초단위
//클라이언트에게 전송
response.addCookie(cookie);
<script>
//js에서 쿠키 저장하기
document.cookie="변수=값";
</script>
session은 cookie와 달리 서버에 저장되며, 모든 페이지에서 접근 가능함.
<%
session.setAttribute("logId", "gildong");
session.setAttribute("logStatus", "Y");
session.setAttribute("logName", "홍길동");
%>
//로그아웃시 세션에 있는 모든 기록을 제거하고 홈페이지로 이동하기
//세션객체를 삭제하면 기록이 제거되고 새로운 세션이 할당됨
session.invalidate();
response.sendRedirect("홈페이지파일");
//홈페이지 파일(index.jsp)
<% if(session.getAttribute("logStatus")!=null && session.getAttribute("logStatus").equals("Y")){//로그인된 경우 %>
<%=session.getAttribute("logName") %><a href="/webJSP/jsp04_session/sessionLogout.jsp">로그아웃</a>
<% }else{//로그인 안된경우 %>
<a href="<%=request.getContextPath()%>/jsp02_response/login.jsp">로그인</a>
<% } %>
//클라이언트 페이지(에러가 발생할 페이지)
<!-- 지시부에 작성 -->
<%@ page errorPage="errorPage.jsp" %>
//에러 발생하면 실행될 페이지
<!-- 지시부에 작성 -->
<%@ page isErrorPage="true" %>
------html영역-------
에러메세지 :
<%
out.print(exception.getMessage());
%>
<a href="/webJSP/index.jsp"><img src="../img/oops.png"></a>
페이지에서 header, footer, 배너등과 같이 항상 같은 내용이 표시되는 부분은 매번 같은 내용을 작성하기보다 <jsp:include>
를 이용하여 다른 jsp파일이나 html파일을 가져와 그 페이지에 그대로 적용할 수 있다.
jsp 파일 붙여넣기 : jsp의 include는 변수를 호환하지 않는다.
//상단바(top.jsp)
<%
String name ="세종대왕";
%>
<!-- main과 중복되는 소스들 제거하기
<style>
#top{height:50px; background:pink}
</style>
main의 css에 작성가능-->
<div id="top">
<%=name %>
</div>
//하단바(bottom.jsp)
<%
int num = 1234;
%>
<!--
<style>
#bottom{height:50px; background:#ddd}
</style>
-->
<div id="bottom">
<%=num %>
</div>
//메인페이지
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
.container{
width:80%; margin:0 auto;
}
.container>img{width:100%;}
#top{height:50px; background:pink}
#bottom{height:50px; background:#ddd}
</style>
</head>
<body>
<!-- top.jsp include -->
<jsp:include page="top.jsp"></jsp:include>
<div class="container">
<img src="../img/06.jpg">
<%
//jsp의 include는 변수를 호환하지 않는다.
//out.print(name); //사용불가
%>
</div>
<!-- bottom.jsp include -->
<jsp:include page="bottom.jsp"></jsp:include>
</body>
</html>
jspf 파일 붙여넣기 : include한 파일의 변수 사용 가능하다.
//상단바(header.jspf)
<%
String username = "홍길동";
%>
<!-- 중복코드 삭제 -->
<div id="header">
<%=username %>
</div>
//하단바(footer.jspf)
<!-- 중복코드 삭제-->
<div id="footer">
<%=username %>
</div>
//메인페이지
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
#main{
width:80%;
margin:0 auto;
}
#main>img{
width:100%;
}
#header{height:50px; background:lightblue}
#footer{height:50px; background:orange}
</style>
</head>
<body>
<%@ include file="header.jspf" %>
<div id="main">
<%=username %><br>
<img src="../img/07.jpg">
</div>
<%@ include file="footer.jspf" %>
</body>
</html>