index.jsp 수정


페이지 상단에 jstl 사용을 위한 taglib를 추가해줍니다.

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

세션을 체크하여 로그인된 상태에서는 로그아웃이 나오게 하고,

로그인이 필요한 상태에서는 로그인이 나오게 코드를 작성합니다 아래 코드는 jstl문법으로 작성된 코드입니다.

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

<title>Index</title>
</head>
<body>
<table border="1" align="center" width="500" height="200">
	<tr>
		<td colspan="4" height="100" align="center">Main page</td>
	</tr>
	<tr>
	<c:if test="${empty sessionScope.user_id}">
		<td colspan="2" align="center" width="250"><a href="Login">로그인</a></td>
	</c:if>
	
	<c:if test="${!empty sessionScope.user_id}">
		<td colspan="2" align="center" width="250"><a href="Logout">로그아웃</a></td>
	</c:if>
		<td colspan="2" align="center" width="250"><a href="Register">회원가입</a></td>
	</tr>
</table>
Login ID:
<c:out value="${sessionScope.user_id }" default="null"/>
</body>
</html>

AppController 수정


AppController에 로그아웃을 위한 메소드 Logout을 새로 만들어줍니다.

	@ResponseBody
	@RequestMapping("/Logout")
	public String Logout(HttpSession session) {
		session.invalidate();
		String str = "<script>";
		str+="alert('Logout success');";
		str+="window.location.href = '/app';";
		str+="</script>";
		return str;
	}

+ Recent posts