본문 바로가기

JSP/URL Rewriting

Session Tracking ( URL Rewriting )

HttpServletResponse.encodeURL(String url)
/* JSP의 HttpSession 객체는 쿠키를 이용하여 클라이언트를 식별하기 때문에 쿠키가 허용되지 않는 웹브라우저에서는 일반적인 방법으로는 세션을 사용할 수 없게 된다. 이런 경우에는 URL Rewriting 방법으로 이용자에게 전달되는 URL에 Session ID를 부가하여 이용자가 해당 링크를 클릭할 때 부가된 Session ID가 다시 서버로 전달되도록 할 수 있다.
response.encodeURL(String url) 메소드는 입력된 URL에 Session ID를 부가하여 링크를 재구성하는 기능을 한다. 그리고 이 메소드는 웹브라우저가 쿠키를 허용하지 않는 경우에만 Session ID를 추가하고 쿠키를 허용하는 웹브라우저에는 일반 URL으로 전달한다.
IE 8 의 쿠키 차단 설정: 도구 > 인터넷옵션 > 개인정보 > 고급 > 자동으로 쿠키처리 안함 > 차단 > 확인
*/

public java.lang.String encodeURL(java.lang.String url)
Encodes the specified URL by including the session ID in it, or, if encoding
is not needed, returns the URL unchanged. The implementation of this method
includes the logic to determine whether the session ID needs to be encoded in
the URL. For example, if the browser supports cookies, or session tracking is
turned off, URL encoding is unnecessary.

For robust session tracking, all URLs emitted by a servlet should be run
through this method. Otherwise, URL rewriting cannot be used with browsers
which do not support cookies.

Parameters:
url - the url to be encoded.
Returns:
the encoded URL if encoding is needed; the unchanged URL otherwise.


urlRewrite01.jsp

<%@ page contentType="text/html;charset=KSC5601"%>
<html>
<head><title>URL Rewrite 01</title>
</head>
<body><p><center>
<%
 Integer num = new Integer(100);
 session.setAttribute("num",num);
 String url = response.encodeURL("urlRewrite02.jsp");
%>

<a href='<%=url%>'>urlRewrite02.jsp</a>
/* 위와 같은 방법으로 작성된 URL이 웹브라우저 전달되었을 때의 형태는 다음과 같다
http://localhost:8888/WebApp/cart/urlRewrite02.jsp;jsessionid=E44DD52AF4BDAFA9711419E4F8C2DD89
*/

</center>
</body>
</html>


urlRewrite02.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"%>
<html>
<head>
<title>URL Rewrite 02</title>
</head>
<body><p><center>

<% Integer i= (Integer )session.getAttribute("num"); %>

Num value in session is <%= i.intValue() %>

</center>
</body>
</html>