[2022-08-29] TIL - 22일차

2022. 8. 29. 14:47Golfzon tech/TIL

😁 Today I Learned

  • JSTL
  • Session
  • LocalStorage
  • SessionStorage

  • // session 은 브라우저가 닫히면 그때서야 없어진다. Request 랑 다르다. 리퀘스트는 그 순간에만 데이터가 유지되지만 세션은 브라우저가 끝날때까지 유지된다.
  • RequestDispatcher 에다가 맵핑해서 하는게 setAttribute 이다.  Request.setAttribute 라고 작성.
  • getAttribute로만 받아야된다.
  • {param.num} 도 request scope 가 생략되어있다.
  • Was 는 우리의 톰캣이다.
  • Jsp 에서 request.setA— 을 써버리면 그 페이지에서만 리퀘스트가 유지되는거 뿐이니 컨트롤러에 쓰는것이다.
  • 값을 저장할 떄는 setAttribute, 읽을 때는 getAttribute 
  • setAttribute 왼쪽의 이름을 유니크하게 줘야한다.
  • Application 은. app_ 무엇무엇으로 이름을 짓고.   Session 은 session_ 무엇무엇으로 이름을 짓는다.
  • JSTL 에서 core 라이브러리는 많이 쓰인다.  

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page session="false"%>
<!-- 이 페이지는 세션 안쓰겠다.  -->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="css/menu.css">
<script>
	sessionStorage.user_id = "tester";
	console.log("sessionStorage : ", sessionStorage.user_id);
	localStorage.user_name = "lee";
	console.log("localStorage : ", localStorage.user_name);
</script>
</head>
<body>
	<h3>Home</h3>
	<jsp:include page="top_menu.jsp"></jsp:include>
	${user_id} ${pageContext.request.scheme}
	${pageContext.request.serverName} ${pageContext.request.serverPort}
	${pageContext.request.contextPath}
	<%=request.getContextPath()%>
	<%=application.getAttribute("player_name")%>

</body>
</html>

 

  • ${} 형태로 사용한다.

 

login.jsp

 

<%@ 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>
<link rel="stylesheet" href="css/menu.css">
</head>
<body>
	<h3>로그인</h3>
	<jsp:include page="top_menu.jsp"></jsp:include>
	<form action="loginOk.do" method="post">
		<table>
			<tr>
				<td><label for="id">ID:</label></td>
				<td><input type="text" placeholder="ID" id="id" name="id"
					value="admin"></td>
			</tr>
			<tr>
				<td><label for="pw">PW:</label></td>
				<td><input type="password" placeholder="PW" id="pw" name="pw"
					value="hi1234"></td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" value="로그인"></td>
			</tr>
		</table>
	</form>

</body>
</html>

 

top_menu.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div id="nav">
	<ul>
		<li><a href="index.do">Home</a></li>
		<li><a href="b_insert.do">글쓰기</a></li>
		<li><a href="b_selectAll.do">글목록</a></li>
	</ul>
</div>
<br>
<br>
<hr>
<h3>${user_id}</h3>
<c:choose>
	<c:when test="${user_id == null}">
		<a href="login.do">로그인</a>
		<a href="join.do">회원가입</a>
	</c:when>
	<c:otherwise>
		<a href="logout.do">로그아웃</a>
	</c:otherwise>
</c:choose>

 

  • <c:choose> 안에 when 으로 조건을 넣어 로그인이 된 상태와 되지 않은 상태를 구별했다.

 

728x90

'Golfzon tech > TIL' 카테고리의 다른 글

[2022-08-31] TIL - 24일차  (0) 2022.08.31
[2022-08-30] TIL - 23일차  (0) 2022.08.30
[2022-08-26] TIL - 21일차  (0) 2022.08.26
[2022-08-25] TIL - 20일차  (0) 2022.08.26
[2022-08-24] TIL - 19일차  (2) 2022.08.24