JSP
<%@page import="kr.or.ddit.vo.MemberVO"%>
<%@ 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>
<%
MemberVO memVo = (MemberVO) session.getAttribute("mem");
System.out.println(memVo);
if(memVo==null){
%>
<form action="<%=request.getContextPath()%>/sessionLoginDb.do" method="post">
<table border="1" style="margin : 1px auto">
<tr>
<td>ID : </td>
<td><input name="id" placeholder="아이디를 입력하세요"></td>
</tr>
<tr>
<td>PASSWORD : </td>
<td><input name="pw" type="password" placeholder="비밀번호를 입력하세요"></td>
</tr>
<tr>
<td colspan="2" style="text-align : center"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
<%
} else{
%>
<div style="text-align : center">
<h3><%=memVo.getMem_name() %>님 반갑습니다</h3>
<br>
<a href="<%=request.getContextPath()%>/sessionLogoutDb.do">로그아웃</a>
</div>
<%
}
%>
</body>
</html>
Login 서블릿
package kr.or.ddit.session.login.controller;
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;
import kr.or.ddit.session.login.service.MemberServiceImpl;
import kr.or.ddit.vo.MemberVO;
@WebServlet("/sessionLoginDb.do")
public class SessionLoginDb extends HttpServlet {
private static final long serialVersionUID = 1L;
private static MemberServiceImpl service = MemberServiceImpl.getInstance();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 클라이언트가 보내온 id와 password를 받는다
// form action으로 보내온 경우 parameter값은 input의 name과 동일해야한다
String id = request.getParameter("id");
String pw = request.getParameter("pw");
MemberVO memVo = new MemberVO();
memVo.setMem_id(id);
memVo.setMem_pass(pw);
MemberVO vo = service.login(memVo);
// 로그인 성공시
if(vo!=null) {
HttpSession session = request.getSession();
session.setAttribute("mem", vo);
}
response.sendRedirect(request.getContextPath()+"/session/sessionLoginDB.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Logout 서블릿
package kr.or.ddit.session.login.controller;
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;
@WebServlet("/sessionLogoutDb.do")
public class SessionLogoutDb 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/sessionLoginDB.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
그 외
mapper
<?xml version="1.0" encoding="UTF-8"?>
<!-- 이 문서는 myBatis에서 처리할 SQL문을 작성하는 문서입니다 -->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org/DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- <mapper>태그의 namespace속성에 설정된 값은 Java소스에서 실행할 SQL문을 호출할 때
같이 사용되는 이름이다 -->
<mapper namespace="mem">
<select id="login" resultType="memVo" parameterType="memVo">
select * from mymember where mem_id=#{mem_id} and mem_pass=#{mem_pass}
</select>
</mapper>
dao
package kr.or.ddit.session.login.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import kr.or.ddit.vo.MemberVO;
import kr.or.ddit.util.MyBatisUtil;
public class MemberMybatisDaoImpl implements IMemberDao{
private static MemberMybatisDaoImpl dao;
private MemberMybatisDaoImpl() {
// TODO Auto-generated constructor stub
}
public static MemberMybatisDaoImpl getInstance() {
if(dao==null) {
dao = new MemberMybatisDaoImpl();
}
return dao;
}
@Override
public MemberVO login(MemberVO memVo) {
SqlSession session = null;
MemberVO vo = new MemberVO();
try {
session = MyBatisUtil.getSqlSession();
vo = session.selectOne("mem.login", memVo);
} catch (Exception e) {
// TODO: handle exception
if(session!=null)try {session.close();}catch (Exception e2) {
// TODO: handle exception
}
}
return vo;
}
}
log4j
사용시 오류를 찾기위해 사용
먼저 lib에 jar파일 추가
↓
res폴더에 (패키지X)
log4j.properties추가
↓
mybatis-config.xml에
setting값 추가
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
'JAVA > HIGH JAVA' 카테고리의 다른 글
[JAVA] 파일 저장/다운 (0) | 2024.06.05 |
---|---|
[JAVA] Json(ajax) + 동기방식 (0) | 2024.06.04 |
[JAVA] 세션(HttpSession) (1) | 2024.06.03 |
5/31 Homework - 로그인(쿠키로 id기억) (0) | 2024.05.31 |
[JAVA] 쿠키(Cookie) (0) | 2024.05.30 |