세션 로그인, 로그아웃
sessionLogin.jsp
<%@page import="kr.or.ddit.memvo.MemberVO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Session Login</title>
</head>
<body>
<%
MemberVO memVo = (MemberVO)session.getAttribute("loginMember");//키값을 적어준것
if(memVo==null){ //세션이 없을때 ...(로그인이 안되었을때)
%>
<form action="<%=request.getContextPath() %>/sessionLogin.do"
method="get">
<table border="1">
<tr>
<td>ID : </td>
<td><input type = "text" name = "id" placeholder="ID 입력하세요"></td>
</tr>
<tr>
<td>PASS : </td>
<td><input type = "password" name = "pass" placeholder="PassWord 입력하세요"></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<input type="submit" value="Login"/>
</td>
</tr>
</table>
</form>
<%
}else{
%>
<h3><%=memVo.getMem_id() %>, <%=memVo.getMem_name() %>님 반갑습니다.</h3>
<a href="<%=request.getContextPath()%>/sessionLogout.do">로그아웃</a>
<%
}
%>
</body>
</html>
SessionLogin.java - servlet
package kr.or.ddit.basic.session;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import kr.or.ddit.memvo.MemberVO;
@WebServlet("/sessionLogin.do")
public class SessionLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//사용자가 입력한 값을 받아오기 위해
request.setCharacterEncoding("utf-8");
//사용자가 입력한 id와 password를 파라미터로 받는다.
String userid = request.getParameter("id");
String pass = request.getParameter("pass");
//실제 회원인지 아닌지 검사하기 위함
MemberDao dao = MemberDao.getInstance();
//입력받은 데이터를 MemberVO객체에 담는다.
MemberVO memVo = new MemberVO();
//파라미터로 받은 아이디와 비밀번호 받기
memVo.setMem_id(userid);
memVo.setMem_pass(pass);
//세션객체 만들기(서버가 가진 데이터)
HttpSession session = request.getSession();
//DB에서 id와 password를 이용하여 해당 회원 정보를 가져온다.
MemberVO returnMemvo = dao.getLoginMember(memVo);
if(returnMemvo==null) { //로그인 실패
response.sendRedirect(
request.getContextPath()
+"/session/sessionLogin.jsp");
}else {//로그인 성공
session.setAttribute("loginMember", returnMemvo);
response.sendRedirect(
request.getContextPath()
+"/session/sessionLogin.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
SessionLogout.java - servlet
package kr.or.ddit.basic.session;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class SessionLogout
*/
@WebServlet("/sessionLogout.do")
public class SessionLogout extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//로그아웃은 세션정보를 모두 삭제하면 된다.
HttpSession session = request.getSession();
session.invalidate(); //세션 삭제
response.sendRedirect(
request.getContextPath()
+"/session/sessionLogin.jsp"); //세션 삭제 후 로그인 페이지로 다시 보내기
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
MemberDao - service,controller안만들고 dao만 간단하게 만든것
package kr.or.ddit.basic.session;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import kr.or.ddit.memvo.MemberVO;
import kr.or.ddit.util.DBUtil3;
public class MemberDao {
private static MemberDao dao;
private MemberDao() {}
public static MemberDao getInstance() {
if(dao==null) dao = new MemberDao();
return dao;
}
//세션에 무엇을 저장할지에 따라 메서드가 달라짐
public MemberVO getLoginMember(MemberVO memVo) {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
MemberVO memberVo = null; // 로그인한 결과가 저장될 변수
try {
conn = DBUtil3.getConnection();
String sql = "select * from mymember "
+ "where mem_id=? and mem_pass=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, memVo.getMem_id());
pstmt.setString(2, memVo.getMem_pass());
rs = pstmt.executeQuery();
if(rs.next()) {
memberVo = new MemberVO();
memberVo.setMem_id(rs.getString("mem_id"));
memberVo.setMem_pass(rs.getString("mem_pass"));
memberVo.setMem_name(rs.getString("mem_name"));
memberVo.setMem_tel(rs.getString("mem_tel"));
memberVo.setMem_addr(rs.getString("mem_addr"));
}
} catch (SQLException e) {
memberVo = null;
e.printStackTrace();
}finally {
if(rs!=null)try {rs.close();}catch(SQLException e) {}
if(pstmt!=null)try {pstmt.close();}catch(SQLException e) {}
if(conn!=null)try {conn.close();}catch(SQLException e) {}
}
return memberVo;
}
}
Author And Source
이 문제에 관하여(세션 로그인, 로그아웃), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@susan9905/세션-로그인-로그아웃저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)