JSP/Servlet-Servlet 로그인 구현
16 Feb 2021 | JSPJSP/Servlet - Servlet 로그인 구현
DB를 연동하여 로그인 기능을 구현해보자
// 로그인 GUI
import java.io.*
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
try {
HttpSession session = request.getSession(false);
// 저장된 세션 여부에 따라 다른 화면 출력
if(session != null) {
String sessionId = session.getId();
System.out.println("세션 아이디: "+sessionId);
String user = (String) session.getAttribute("user");
// 로그인 후 화면
out.println("<html>");
out.println("<body>");
out.println("<table border=1 width=300>");
out.println("<tr>");
out.println("<td width=300 align=center>"+user+"님이 로그인했습니다.");
out.println("</tr>");
out.println("<tr>");
out.println("<td align=center>");
out.println("<a href='#'>회원정보</a>");
out.println("<a href='Logout'>로그아웃</a>");
out.println("</td>");
out.println("</tr>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
} else {
// 로그인 화면
out.println("<html>");
out.println("<body>");
out.print("<form method=post action=LoginCheck>");
out.println("<table border=1 width=300>");
out.println("<tr>");
out.println("<th width=100>아이디</th>");
out.println("<td width=200> <input type=text name=id></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<th width=100>비번</th>");
out.println("<td width=200> <input type=password name=pwd></td>");
out.println("</tr>");
out.println("<tr>");
out.println("<td align=center colspan=2>");
out.println("<input type=button value=회원가입>");
out.println("<input type=submit value=로그인>");
out.println("</td>");
out.println("</tr>");
out.println("</form>");
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
} finally {
out.close();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
// DB연동 로그인 확인 서블릿
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginCheck extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8"); // 한글 처리
String id = request.getParameter("id"); // 사용자 입력 id
String pwd = request.getParameter("pwd"); // 사용자 입력 pwd
// db에서 사용자 정보 조회
StringBuffer sql = new StringBuffer();
sql.append("select * from login");
Connection con = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/visit_db?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false","root","123456");
pstm = con.prepareStatement(sql.toString());
rs = pstm.executeQuery();
while(rs.next()) {
String dbId = rs.getString("id");
String dbPwd = rs.getString("pass");
if(dbId.equals(id) && dbPwd.equals(pwd)) {
HttpSession session = request.getSession();
// 클라이언트 정보를 HttpSession에 저장
session.setAttribute("user", id);
}
}
} catch(SQLException e) {
e.printStackTrace();
} catch(ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {if(pstm != null)pstm.close();}catch(SQLException e) {};
try {if(con != null)pstm.close();}catch(SQLException e) {};
}
response.sendRedirect("Login");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
// 로그아웃 서블릿
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
public class Logout extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(false);
if(session != null) {
// 세션 무효화
session.invalidate();
}
// 로그인 화면으로 화면 이동
response.sendRedirect("Login");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
실행화면
참고 자료
KG 아이티뱅크 강의 자료