📄 JSP

[JSP] Day39 - Session / Application / Out 내장객체 활용 웹 페이지

harveydent 2023. 6. 29. 21:55
728x90

내장객체 활용 웹 페이지

게시판 웹 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.ArrayList"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<%
		if (session.getAttribute("id") == null) {
	%>
	<form action="b.jsp" method="post">
		<input type="text" name="mid" placeholder="아이디" required="required">
		<input type="password" name="mpw" placeholder="비밀번호" required="required">
		<input type="submit" value="로그인">
	</form>	
	<%
		} else {
	%>
	<form action="d.jsp">
		<input type="submit" value="로그아웃">
	</form>	
	<%
		}
	%>
	<%
		if (session.getAttribute("id") != null) {
	%>
	<hr>
	<form action="c.jsp">
		<input type="text" name="content" placeholder="내용을 입력해주세요.">
		<input type="submit" value="글작성">
	</form>	
	<%
		}
	%>
	<hr>
	<ul>
		<%
			ArrayList<String> list = (ArrayList<String>)application.getAttribute("list");
			if (list == null) {
				out.println("등록된 글이 없습니다.");
			} else {
				for (String v : list) {
					out.println("<li>" + v + "</li>");
				}			
			}
		%>
	</ul>
</body>
</html>

a.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:useBean id="mDAO" class="model.MemberDAO"/>
<jsp:useBean id="mVO" class="model.MemberVO"/>
<jsp:setProperty property="*" name="mVO"/>

<%
	mVO = mDAO.selectOne(mVO);

	if (mVO == null) {
		out.println("<script>alert('로그인 실패...'); history.go(-1);</script>");
	} else {
		session.setAttribute("id", mVO.getMid());
//		out.println("<script>location.href = 'a.jsp';</script>");
		response.sendRedirect("a.jsp");
	}
%>

b.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.ArrayList"%>
<%
	String id = (String)session.getAttribute("id");
	String content = request.getParameter("content");
	ArrayList<String> list = (ArrayList<String>)application.getAttribute("list");
	
	System.out.println("글정보: " + content);
	
	if (request.getParameter("content").equals("")) { // 현재 내용이 공백인지
		out.println("<script>alert('내용을 입력해주세요.');</script>");
	} else {
		if (list == null) {
			list = new ArrayList<String>();
			application.setAttribute("list", list);
		}
		list.add(0, id + ": " + content);	
	}
	
//	response.sendRedirect("a.jsp");
//	out.println("<script>history.go(-1);</script>");
	out.println("<script>location.href = 'a.jsp';</script>");
%>

c.jsp

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	session.removeAttribute("id");

	response.sendRedirect("a.jsp");
%>

d.jsp

 

방문자 수 확인 웹 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

<%
	application.setAttribute("memberName", "홍길동");

	application.setAttribute("cnt", 0);
%>

<a href="test02.jsp">방문자수 확인하기</a>

</body>
</html>

test1.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>
</head>
<body>

<%
	Integer cnt = (Integer)application.getAttribute("cnt");
	cnt++;
	application.setAttribute("cnt", cnt);
%>

오늘의 방문자수는 <%= cnt %> 명입니다.

</body>
</html>

test2.jsp

GitHub

https://github.com/Qkrwnsgus0522/JSP

 

GitHub - Qkrwnsgus0522/JSP

Contribute to Qkrwnsgus0522/JSP development by creating an account on GitHub.

github.com

 

728x90