반응형
UserController
package frontControllerPJT.controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import frontControllerPJT.util.DBConn;
import frontControllerPJT.vo.UserVO;
public class UserController {
public UserController(HttpServletRequest request
, HttpServletResponse response
, String[] comments) throws ServletException, IOException {
if(comments[comments.length-1].equals("login.do")) {
if(request.getMethod().equals("GET")) {
login(request,response);
}else if (request.getMethod().equals("POST")) {
loginOk(request,response);
}
}else if(comments[comments.length-1].equals("logout.do")) {
logout(request,response);
}else if(comments[comments.length-1].equals("mypage.do")) {
mypage(request,response);
}
}
public void login(HttpServletRequest request
, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/user/login.jsp").forward(request, response);
}
public void loginOk(HttpServletRequest request
, HttpServletResponse response) {
String uid = request.getParameter("uid");
String upw = request.getParameter("upw");
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
ResultSet rs = null; //조회 결과를 담음
//try 영역
try{
conn = DBConn.conn();
String sql = "select * from user where uid=? and upw=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, uid);
psmt.setString(2, upw);
rs = psmt.executeQuery();
if(rs.next()){
UserVO loginUser = new UserVO();
loginUser.setUno(rs.getString("uno"));
loginUser.setUname(rs.getString("uname"));
loginUser.setUauthor(rs.getString("uauthor"));
loginUser.setUid(uid);
//로그인 정보 session에 저장
HttpSession session = request.getSession();
session.setAttribute("loginUser", loginUser);
response.sendRedirect(request.getContextPath());
}else {
//로그인 실패할 경우
response.sendRedirect(request.getContextPath()+"/login.do");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
public void logout(HttpServletRequest request
, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect(request.getContextPath());
}
public void mypage(HttpServletRequest request
, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
UserVO loginUser = (UserVO)session.getAttribute("loginUser");
String uno = loginUser.getUno();
Connection conn = null; //DB 연결
PreparedStatement psmt = null; //SQL 등록 및 실행. 보안이 더 좋음!
ResultSet rs = null; //조회 결과를 담음
//try 영역
try{
conn = DBConn.conn();
String sql = "select * from user where uno=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1, uno);
rs = psmt.executeQuery();
if(rs.next()){
UserVO user = new UserVO();
user.setUno(rs.getString("uno"));
user.setUid(rs.getString("uid"));
user.setUname(rs.getString("uname"));
user.setUpw(rs.getString("upw"));
user.setUphone(rs.getString("uphone"));
user.setUemail(rs.getString("uemail"));
user.setUstate(rs.getString("ustate"));
user.setUauthor(rs.getString("uauthor"));
user.setRdate(rs.getString("rdate"));
request.setAttribute("user",user);
request.getRequestDispatcher("/WEB-INF/user/mypage.jsp").forward(request, response);
}else {
//회원조회 실패할 경우
response.sendRedirect(request.getContextPath()+"/login.do");
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
DBConn.close(rs, psmt, conn);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
login
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>로그인</h2><hr>
<form action="login.do" method="post">
<table>
<tr>
<td align="right">아이디 : </td>
<td><input type="text" name="uid" size="30"></td>
</tr>
<tr>
<td align="right">패스워드 : </td>
<td><input type="password" name="upw" size="30"></td>
</tr>
</table>
<button>로그인</button>
</form>
</body>
</html>
mypage
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp" %>
<%
//request의 attribute 값은 object타입이므로 강제 형변환 필요
UserVO user = (UserVO)request.getAttribute("user");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>mypage</title>
</head>
<body>
<br>
<button onclick="location.href='<%= request.getContextPath()%>'">메인페이지로 돌아가기</button>
<h2>마이페이지</h2><hr>
회원번호 : <span><%= user.getUno() %> </span><br>
회원아이디 : <span> <%= user.getUid() %></span><br>
회원비밀번호 : <span> <%= user.getUpw() %></span><br>
회원이름 : <span><%= user.getUname() %> </span><br>
회원연락처 : <span><%= user.getUphone() %> </span><br>
회원이메일 : <span><%= user.getUemail() %> </span><br>
회원가입일 : <span><%= user.getRdate() %> </span><br>
회원상태 : <span><%= user.getUstate() %> </span><br>
회원권한 :
<span>
<%
String uauthor = "";
switch(user.getUauthor()){
case "U" : uauthor = "일반회원"; break;
case "V" : uauthor = "VIP회원"; break;
case "A" : uauthor = "관리자"; break;
}
%><%= uauthor %>
</span><br>
</body>
</html>
header
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="frontControllerPJT.vo.*" %>
<%
String author= "";
String loginNo= "";
UserVO loginUser = null;
if(session.getAttribute("loginUser") != null){
loginUser = (UserVO)session.getAttribute("loginUser");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>FrontController</title>
</head>
<style>
td{
text-align:center;
}
input[type="text"],input[type="password"], textarea{
width:400px;
border-radius:5px;
}
input[type="text"],input[type="password"]{
height:30px;
}
button{
background-color:white;
color:royalblue;
font-weight:bold;
border: 1px solid royalblue;
border-radius:5px;
cursor:pointer;
width:170px;
height:30px;
}
#sBtn{
width:100px;
}
</style>
<body>
<!-- head 영역 -->
<header>
<div class="title_inner">
<div class="login">
<%
if(loginUser != null){
%>
<strong><%= loginUser.getUid() %></strong>님 환영합니다.
<a href="<%= request.getContextPath() %>/user/logout.do"><button id="sBtn">로그아웃</button></a>
<a href="<%= request.getContextPath() %>/user/mypage.do"><button id="sBtn">내 정보 확인</button></a>
<%
}else{
%>
<a href="<%= request.getContextPath() %>/user/login.do"><button id="sBtn">로그인</button></a>
<%
}
%>
</div>
</div>
</header>
index
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="frontControllerPJT.vo.*" %>
<%@ include file="/WEB-INF/include/header.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Front Controller</title>
</head>
<body>
<h2>프론트컨트롤러 프로젝트 연습하기</h2><hr>
<a href="<%= request.getContextPath() %>/sample/main.do">sample 메인페이지로 이동</a>
<br>
<a href="<%= request.getContextPath() %>/sample/board1List.do">
sample 게시판1 페이지로 이동
</a><br>
<a href="<%= request.getContextPath() %>/sample/board2List.do">
sample 게시판2 페이지로 이동
</a><br>
<!--
sample 게시판1 페이지로 이동 클릭시 WEB-INF/sample/list1.jsp를 출력
sample 게시판2 페이지로 이동 클릭시 WEB-INF/sample/list2.jsp를 출력
-->
<br>
<a href="<%= request.getContextPath() %>/board/main.do">board 메인페이지로 이동</a>
<br>
<%-- <%
if(loginUser != null){
%>
[<strong><%= loginUser.getUid() %></strong>]님 환영합니다.
<a href="<%= request.getContextPath() %>/user/logout.do">로그아웃</a>
<a href="<%= request.getContextPath() %>/user/mypage.do">내 정보 확인</a>
<%
}else{
%>
<a href="<%= request.getContextPath() %>/user/login.do">로그인</a>
<%
}
%> --%>
<br>
<a href="<%= request.getContextPath() %>/free/list.do">자유게시판으로 이동하기</a>
</body>
</html>
반응형
'Java' 카테고리의 다른 글
[JSP] EL & JSTL (0) | 2024.11.26 |
---|---|
[Servlet] FrontController 게시글 수정하기 삭제하기 (0) | 2024.10.25 |
[Servlet] FrontController 게시글 상세 조회 (0) | 2024.10.24 |
[Servlet] FrontController 글 목록 조회 (0) | 2024.10.20 |
[Servlet] Front Controller (0) | 2024.10.19 |