자 오늘은 내가 만든 세션을 정리해보는 시간을 가져볼것이다.
일단 회원로그인폼 하나를 만든다.
MemberLoginForm.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>
<%
String id = "";
Cookie [] cookies = request.getCookies();
for(int i = 0; i<cookies.length;i++){
if(cookies[i].getName().equals("id")){
id = cookies[i].getValue();
break;//기능을 다 수행했으면 마침으로 break;를 처리한다.
}
}
%>
<center>
<h2>회원 로그인</h2>
<form action="MemberLoginProc.jsp" method="post">
<table width="300" border="1">
<tr height="100">
<td>아이디:</td>
<td><input name="id" type="text" value ="<%=id%>"></td>
</tr>
<tr height="100">
<td>비밀번호:</td>
<td><input name="password" type="password"></td>
</tr>
<tr height="50">
<td colspan="2" align="center"><input type="checkbox"
name="save" value="1"> 아이디 저장</td>
</tr>
<tr height="50">
<td align="center" colspan="2"><input type="submit"
name="login" value="로그인"> <a href="MemberJoin.jsp"> <input
type="button" name="join" value="회원가입 ">
</a> <a href="../Main/index.jsp"> <input type="button" name="home"
value="홈">
</a></td>
</tr>
</table>
</form>
</center>
</body>
</html>
자 이런식으로 form하나를 만든다.
MemberLoginProc.jsp
<%@page import="java.io.Console"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="db.MemberBean"%>
<%@page import="db.MemberDAO"%>
<%@ 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>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String password = request.getParameter("password");
String save = request.getParameter("save");
if(save!=null){
Cookie cookie = new Cookie("id",id);
cookie.setMaxAge(60*10);
response.addCookie(cookie);
System.out.print("쿠키로그인완료 ");
}
MemberDAO mdao = new MemberDAO();
MemberBean mbean = new MemberBean();
mbean = mdao.oneSelectMemberLogin(id,password);
HttpSession session1 = request.getSession();
if(mbean.getId()==null&&mbean.getId()==null){
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('잘못된 아이디 혹은 비밀번호입니다. ')");
script.println("history.go(-1)");
script.println("</script>");
}else if(mbean.getId().equals(id)&&mbean.getPass1().equals(password)){
response.sendRedirect("../Main/index.jsp");
session1.setAttribute("id", mbean.getId());
}
%>
</body>
</html>
위 코드를 잘보자..!
Header.jsp
<%@page import="db.MemberBean"%>
<%@ 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>
<style type="text/css">
#indexMainTitle {
text-decoration: none;
color: rgb(205, 24, 24);
font-size: 100px;
width: 600px;
}
</style>
<title>DRP</title>
</head>
<body>
<%
MemberBean mbean = new MemberBean();
HttpSession session1 = request.getSession();
String loginString = "";
String logoutString = "";
if (mbean.getId() == null) {
loginString = "로그인 ";
}
else if (mbean.getId() != null) {
logoutString = "로그아웃";
}
%>
<center>
<h1 class="HeaderTitle">
<a href="index.jsp" id="indexMainTitle">DropTheBeat</a>
</h1>
<table align="center">
<tr height="50">
<%
if(session1.getAttribute("id")==null){
%>
<td><button name="login" value="로그인" onclick="location.href='../Member/MemberLoginForm.jsp' ">로그인 </button><%
}
else if(session1.getAttribute("id")!=null){
%>
<%=session1.getAttribute("id") %>
<td><button name="logout" value="로그아웃" onclick="location.href='../Member/MemberLoginProc2.jsp' ">로그아웃 </button>
<%
}
%>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
위 코드를 잘 보자!
'웹 프로그래밍(풀스택-->java) > 웹프로그래밍(백엔드-->java)' 카테고리의 다른 글
33. Jsp 게시판 - 테이블 설정, 빈클래스,Dao클래스 설정 (0) | 2021.12.05 |
---|---|
32. Jsp 게시판 - 시스템구조 (0) | 2021.12.05 |
30. Jsp Cookies & Session 4 (0) | 2021.12.03 |
29. Jsp Cookie & Session3 (0) | 2021.12.02 |
28. Jsp Cookies & Session 2 (0) | 2021.12.02 |