login.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>
<h2>로그인</h2>
<!-- 로그인 처리는 예외적인 사항으로 post 요청을 하자 -->
<form action="welcome2.jsp" method="POST">
<label for="">username : </label>
<input type="text" id="username" name="username" >
<label for="">password : </label>
<input type="password" id="password" name="password" >
<button type="submit">로그인</button>
</form>
</body>
</html>
welcome2.jsp
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>환영해요~</title>
</head>
<body>
<%
// http://localhost:8080/jsp/welcome2.jsp
// session 객체를 사용하여 사용자 정보가 여부를 확인하자
String username = (String)session.getAttribute("username");
Integer visitCount = (Integer) application.getAttribute("visitCount");
if(username == null || username.trim().isEmpty()){
// request 객체에서 사용자 정보를 추출 하자
username = request.getParameter("username");
if(username != null && !username.trim().isEmpty()) {
// 세션 객체를 사용해서 사용자 정보를 저장(속성과 값을)
session.setAttribute("username", username);
if(visitCount == null){
visitCount = 1;
} else {
visitCount++;
}
} else {
// 사용자가 정상적인 데이터를 보내지 않았다면
response.sendRedirect("login.jsp");
return;
}
}
// application 내장 객체를 사용하여 방문 횟수 증가
application.setAttribute("visitCount", visitCount);
Date now = new Date();
%>
<h2>환영 합니다, <%= username %></h2>
<p>현재 시간 : <%= now %> </p>
<p>방문 횟수 : <%= visitCount %> </p>
</body>
</html>
쿠키 확인
'JAVA > JSP 프로그래밍' 카테고리의 다른 글
JSP 게시판 만들기(로그인, 회원가입, 게시글 리스트) (0) | 2024.07.15 |
---|---|
[JSP] 간단한 게시판 만들어 보기 (1) | 2024.07.08 |
[JSP] 폼 처리와 요청 방식 (1) | 2024.07.05 |
[JSP] JSP 내장 객체란 뭘까? (0) | 2024.07.05 |
[JSP] JSP 주석과 지시자 (0) | 2024.07.05 |