Cohe

JSP mini project : mypage 실습3 - 로그인 본문

개발 언어/JSP

JSP mini project : mypage 실습3 - 로그인

코헤0121 2024. 4. 15. 18:08
728x90

로그인

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Welcome to MyWorld</title>

    <!-- Bootstrap Core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="../css/business-casual.css" rel="stylesheet">

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">
</head>
<body>
    <%@include file="../include/header.jsp" %>
    <section>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    <form name = "regform" class="form-horizontal" action="user_login_ok.jsp" method="post">
                        <h2 class="text-center">로그인 페이지</h2>
                        <hr>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">ID</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="id" placeholder="아이디">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-3 control-label">PW</label>
                            <div class="col-sm-9">
                                <input type="password" class="form-control" name="pw">
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-offset-3 col-sm-9">
                                <input type="submit" class="btn btn-default" onclick="" value="로그인">
                                <button type="button" class="btn btn-info" onclick="location.href='user_join.jsp'">회원가입</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>

            <br>

    <%@include file="../include/footer.jsp" %>
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>

login_ok.jsp


<%@page import="com.myweb.user.model.UserVO"%>
<%@page import="com.myweb.user.model.UserDAO"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%
    request.setCharacterEncoding("utf-8");
    String id = request.getParameter("id");
    String pw = request.getParameter("pw");

    //dao 생성
    UserDAO dao = UserDAO.getInstance();

    int result = dao.login(id, pw);
    if(result == 1){//중복 시 경고 띄우고 회원 가입 창으로 돌아가기
        //로그인 성공시 회원정보 얻어오는 작업
        UserVO vo = dao.getInfo(id);
        String name = vo.getName();


        //아이디와 이름을 세션에 저장
        session.setAttribute("user_id", id);
        session.setAttribute("user_name", name);

        //mypage.jsp로 이동

        response.sendRedirect("user_mypage.jsp");
        }
    else{// 중복이 없는 경우 회원가입 처리 진행
        out.write("<script>alert(\"로그인 실패했습니다.\");history.go(-1);</script>");
    }
%>

mypage.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Welcome to MyWorld</title>

    <!-- Bootstrap Core CSS -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="../css/business-casual.css" rel="stylesheet">

    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">


    <%

        String id = (String)session.getAttribute("user_id");
        String name = (String)session.getAttribute("user_name");

        if(id ==null){
            response.sendRedirect("user_login.jsp");
        }
    %>
</head>
<body>
    <%@include file="../include/header.jsp" %>
    <section>
    <div align="center">
        <h2>MyPage</h2>
        <hr>

        <%=id %>(<%=name %>)님의 회원정보를 관리합니다.
        <hr>
        <button type="button" class="btn btn-primary" 
        onclick="location.href='user_change_pw.jsp'">비밀번호 변경</button>
        <button type="button" class="btn btn-info" 
        onclick="location.href='user_update.jsp'">회원 정보 수정</button>
        <button type="button" class="btn btn-primary" onclick="location.href='user_delete_check.jsp'">회원 탈퇴</button>

    </div>


    </section>
    <%@include file="../include/footer.jsp" %>
    <!-- jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>