본문 바로가기

JSP

로그인 화면이동( 화면이동방법 3가지 )

# 로그인( 모듈화 안된거. 그냥 막코딩. 중복 많음. 필요없는 코드 많음 )
main.jsp
============
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<style type="text/css">
table {
width: 500px;
height: 300px;
}

td {
border: 2px solid green;
}

.menutd {
width: 150px;
height: 230px;
}
</style>
<title>Insert title here</title>
</head>
<body>

<table>
     <tr><td colspan="2">
         <div align="center">TOP</div> <br>
         <div align="right">Guest님 즐거운 시간!!</div>
     </tr>
     <tr>
          <td class="menutd">총방문자수 : <br> 현재접속자수:<br>
          <a href="loginForm.jsp">로그인</a>
          <td class="contenttd">main페이지<br> 사이트를 이용하려면 로그인을 해주세요...
     </tr>
</table>

</body>
</html>


loginForm.jsp
==================
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<style type="text/css">
table {
width: 500px;
height: 300px;
}

td {
border: 2px solid green;
}

.menutd {
width: 150px;
height: 230px;
}
</style>
<title>Insert title here</title>
</head>
<body>

<table>
     <tr><td colspan="2">
          <div align="center">TOP</div> <br>
          <div align="right">Guest님 즐거운 시간!!</div>
     </tr>
     <tr>
          <td class="menutd">총방문자수 : <br> 현재접속자수:<br>
          <a href="loginForm.jsp">로그인</a>
          <td class="contenttd">

               <form action="loginProcess.jsp">
                    ID<input type="text" name="id"><br>
                    PW<input type="password" name="pw"><br>
                       <input type="button" value="취소?">
                       <input type="submit">
               </form>

     </tr>
</table>

</body>
</html>



loginProcess.jsp
===================
1)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");

if (id.equals(pw)) {
%>

<jsp:forward page="loginSuccess.jsp" />

<%
} else {
%>
<script>
history.back();
</script>
<%
}
%>

2)
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");

String p = "loginForm.jsp";
if (id.equals(pw)) {
p = "loginSuccess.jsp";
}
%>
<jsp:forward page="<%=p%>" />

2-1)깔끔히완성 
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");

String p = "loginForm.jsp";
if (id.equals(pw)) {
p = "loginSuccess.jsp";
} else
request.setAttribute("msg", "로그인실패");
%>
<jsp:forward page="<%=p%>" />
하고나서 loginForm.jsp에 다음과같이추가해줘야함
<%
if(request.getAttribute("msg")!=null){
      out.println((String)request.getAttribute("msg"));
}
%>


loginSuccess.jsp
================
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<style type="text/css">
table {
width: 500px;
height: 300px;
}

td {
border: 2px solid green;
}

.menutd {
width: 150px;
height: 230px;
}
</style>
<title>Insert title here</title>
</head>
<body>
<table>
<tr>
<td colspan="2">
<div align="center">TOP</div> <br>
<div align="right"><%=request.getParameter("id")%>님 즐거운 시간!!
</div>
</tr>
<tr>
<td class="menutd">총방문자수 : <br> 현재접속자수:<br>
                              <a href="loginForm.jsp">로그인</a>
<td class="contenttd">로그인성공~
</tr>
</table>
</body>
</html>











#화면이동처리3가지방법
(주소바뀜, 데이터전달X) - 단순 화면 이동할때 사용한다.
1.로케이션(js)         location.href="b.jsp"
2.리다이렉트(jsp)   response.sendRedirect("~~.jsp")     (주소바뀜,
                                                        request값이 유지안되므로 데이터전달x)
                                                       ( 1.로케이션이동과 같음 단순 화면이동임!! )


(주소안바뀜, 데이터전달 ) - 화면이동과 동시에 데이터를 전달 할 경우 사용한다.
3.포워드(html)         <jsp:forward page="~~.jsp" />          
  포워드(jsp/서블릿)   request.getRequsetDispatcher("~~.jsp").forward(request, response);
          


서블릿
=============
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// 첫번째방식
//response.sendRedirect("a.jsp");

// 두번째방식 - 이거는 <jsp:forward~>와 같은역활... url유지하고 request값유지한다
request.setAttribute("aa", "bb");         // request값유지되는지확인하기위해
request.getRequestDispatcher("a.jsp").forward(request, response);
                                                // request객체를 매개변수로 그대로 넘겨줌
                                             

}



# 하지만 위에거는 반복되는게 너무많다( 페이지 모듈화가 필요하다 )
   중복되는걸 모듈화해서 <jsp:include page="xxx.jsp"  />
main.jsp
=============
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%
     String contentPage = "welcome.jsp";
     String a = request.getParameter("cp");
     if (a != null) {
          contentPage = a;
     }
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<style type="text/css">
table {
width: 500px;
height: 300px;
}

td {
border: 2px solid green;
}

.menutd {
width: 150px;
height: 230px;
}
</style>
<title>Insert title here</title>
</head>
<body>
       <table>
              <tr>
                     <td colspan="2"><jsp:include page="top.jsp" />
              </tr>
              <tr>
                     <td class="menutd"><jsp:include page="menu.jsp" />
                     <td class="contenttd"><jsp:include page="<%=contentPage%>" />
                    // top과 menu는 가만히 있고 내용(content)만 바뀜 
              </tr>
       </table>
</body>
</html>


loginForm.jsp
==========
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>

       <form action="loginProcess.jsp">
              ID<input type="text" name="id"><br> PW<input
                     type="password" name="pw"><br> <input type="reset"
                     value="취소"> <input type="submit" value="전송"><br>
              <%
                     if (request.getAttribute("msg") != null) {
                           out.println(request.getAttribute("msg"));
                     }
              %>
       </form>





loginProcess.jsp
====================
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
       <%
              String id = request.getParameter("id");
              String pw = request.getParameter("pw");

              String p = "main.jsp?cp=loginForm.jsp";
              if (id.equals(pw)) {
                     p = "main.jsp?cp=loginSuccess.jsp";
              } else
                     request.setAttribute("msg", "로그인실패");
       %>
       <jsp:forward page="<%=p%>" />





loginSuccess.jsp
=======================
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
로그인성공


top.jsp
=============
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
     <div align="center">TOP</div>     <br>
     <div align="right">Guest님 즐거운 시간!!</div>

menu.jsp
==============
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
               총방문자수 : <br>
               현재접속자수:<br>
               <a href="main.jsp?cp=loginForm.jsp">로그인</a>

welcome.jsp
===============
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
     환영합니다~