<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>글 목록</h1>
<c:set var="name" value="${user }" />
<h2>${name }</h2>
<c:if test="${name eq null }">
<script type="text/javascript">
alert("로그인이 필요합니다.");
location.href="login.jsp"
</script>
</c:if>{
<table border="1">
<tr>
	<th bgcolor="orange" width="150">번호</th>
	<th bgcolor="orange" width="150">제목</th>
	<th bgcolor="orange" width="150">작성자</th>
	<th bgcolor="orange" width="150">등록일</th>
	<th bgcolor="orange" width="150">조회수</th>
</tr>
<c:if test="${name ne null }">
<c:forEach items="${boardList }" var="board">
<tr>
	<td>${board.seq }</td>
	<td><a href="getBoard.do?seq=${board.seq }">${board.title }</a></td>
	<td>${board.writer }</td>
	<td>${board.regDate }</td>
	<td>${board.cnt }</td>
</tr>
</c:forEach>
</c:if>
</table>
</body>
</html>

 

게시판을 불러올 때 user 세션이 없을 경우 로그인을 하도록 처리하려고한다.

여가서 문제가 있었던 부분은 위에 name 변수에 user 세션을 넣을 때 주의할 점은 ""이다.

따옴표가 있어야 value값 저장이 되니 이 점 참고하도록하자 작동 원리는 아래와 같다.

 

로그인 화면에서 위와 같이 로그인을 진행한다.

로그인을 하면 위와 같이 변수 name에 user세션에 대한 정보가 저장된다.

위의 전체 코드에서 보면 알 수 있듯이 user 세션이 있다면 위와 같이 게시판 정보가 보이도록 처리가 되어 있으며, user 세션이 있으므로 위의 게시판 정보가 정상 출력이 된다.

이후 로그아웃을 진행한 후 바로 list를 출력한다.

 

출력화면 위와 같이 아까 글 목록 화면 아래에 아무런 정보가 없는 것을 확인할 수 있다.

이유는 user session 값이 null 값이므로 아무런 정보가 노출되지 않는 것이다.

이후 확인 버튼을 클릭하면 작성한 코드와 같이 로그인 페이지로 이동한다.

위의 사진을 보면 작성된 script대로 로그인 페이지로 이동한 결과를 확인할 수 있다.

+ Recent posts