티스토리 뷰

FRONT-END/JSP

[JSP] SENDREDIRECT( )

진심스테이크 2018. 4. 13. 01:00

 

sendRedirect( ) : 페이지 이동 방법 중 하나

 

1. GET

- 내가 친 내용이 보임

- 보안성 없음

 

2. POST

- 내가 친 내용이 보이지 않음 -> null로 떠야 정상

 

 


 

 

 

로그인 예제

 

login.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>login</title>
</head>
<body>
    <form action="result_get.jsp" method="get">
        <table align="center">
            <tr>
                <td colspan="2" align="center">GET</td>
            </tr>
            <tr>
                <td bgcolor="lightgrey" align="center">아이디</td>
                <td><input type="text" name="id" /></td>
            </tr>
            <tr>
                <td bgcolor="lightgrey" align="center">비밀번호</td>
                <td><input type="password" name="pw" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="로그인" /></td>
            </tr>
        </table>
    </form>
    <form action="result_post.jsp" method="post">
        <table align="center">
            <tr>
                <td colspan="2" align="center">POST</td>
            </tr>
            <tr>
                <td bgcolor="lightgrey" align="center">아이디</td>
                <td><input type="text" name="id" /></td>
            </tr>
            <tr>
                <td bgcolor="lightgrey" align="center">비밀번호</td>
                <td><input type="password" name="pw" /></td>
            </tr>
            <tr>
                <td colspan="2" align="center"><input type="submit" value="로그인" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

 

1. GET

 

result_get.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>get</title>
</head>
<body>
    <%
        request.setCharacterEncoding("UTF-8");
        String id = (String) request.getParameter("id");
        String pw = (String) request.getParameter("pw");
        request.getSession().setAttribute("id", id);
        request.getSession().setAttribute("pw", pw);
    %>
    <%
        response.sendRedirect("end_get.jsp");
    %>

</body>
</html>

 

 

end_get.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%
    request.setCharacterEncoding("euc-kr");
%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id = (String) request.getSession().getAttribute("id");
String pw = (String) request.getSession().getAttribute("pw");
%>
    <table align="center">
        <tr>
            <td colspan="2" align="center">SHOW</td>
        </tr>
        <tr>
            <td bgcolor="lightgrey" align="center">아이디</td>
            <td><%=id %></td>
        </tr>
        <tr>
            <td bgcolor="lightgrey" align="center">비밀번호</td>
            <td><%=pw %></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="로그인" /></td>
        </tr>
    </table>
</body>
</html>

 

 

2.  POST

 

result_post.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <%!String name;%>
    <%
        request.setCharacterEncoding("UTF-8");
        request.getSession().setAttribute("id2", (Object) request.getParameter("id"));
        request.getSession().setAttribute("pw1", (Object) request.getParameter("pw"));
    %>
    <%
        response.sendRedirect("end_post.jsp");
    %>
</body>
</html>

 

 

end_post.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
    <%
    request.setCharacterEncoding("euc-kr");
%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
    <table align="center">
        <tr>
            <td colspan="2" align="center">SHOW</td>
        </tr>
        <tr>
            <td bgcolor="lightgrey" align="center">아이디</td>
            <td><%=request.getParameter("id")%></td>
        </tr>
        <tr>
            <td bgcolor="lightgrey" align="center">비밀번호</td>
            <td><%=request.getParameter("pw")%></td>
        </tr>
        <tr>
            <td colspan="2" align="center"><input type="submit" value="로그인" /></td>
        </tr>
    </table>
</body>
</html>

 

 


 실행 결과

 

1. GET

 

 

 

2.POST

 

 

'FRONT-END > JSP' 카테고리의 다른 글

[JSP] ACTION TAG - 액션 태그  (0) 2018.04.16
[JSP] 간단한 장바구니  (0) 2018.04.13
[JSP] ATTRIBUTE & SCOPE - 속성과 영역  (0) 2018.04.12
[JSP] 오류 예외처리  (0) 2018.04.12
[JSP] 한글 처리  (0) 2018.04.12
댓글