2017-02-16 16 views
0

captcha 코드를 다시로드하려는 로그인 페이지를 만듭니다. 그러나 사용자가 입력 한 정보를 잃지 않으려합니다.사용자 입력을 잃지 않고 현재 페이지 새로 고침

파일 : 의 login.jsp

<body> 
<b>REGISTER</b> 
<p>Please fill up the form below.</p> 
<s:form action="register" method="post"> 
    <s:textfield label="Enter username" key="userId" maxlength="5" size="30" id="userId"/> 
    <s:password label="Enter password" key="userPsw" size="30" /> 
    <tr> 
     <td></td> 
     <td> 
      <img src="<c:url value='simple-captcha.png' />" /> 
      <br /> 
      <p onclick="something()">Refresh</p> 
     </td> 
    </tr> 
    <s:textfield label="Enter code" key="captchaResponse" size="30" /> 
    <s:submit value="Login" /> 
</s:form> 

파일 : 자바 스크립트 코드

난 내 보안 문자 코드하지만 난 돈을로드 할 "새로 고침"텍스트에
function something() 
{ 
    window.location.reload(true); 
} 

window.onload = function() 
{ 
    var a = sessionStorage.getItem('userId'); 
    if(a !== null) $('#inputName').val(name); 

    var b = sessionStorage.getItem('userPsw'); 
    if(b !== null) $('#userPsw').val(b); 
} 

window.onbeforeunload = function() { 
    sessionStorage.setItem("userId", $('#userId').val()); 
    sessionStorage.setItem("userPsw", $('#userPsw').val()); 
} 

내 데이터를 잃고 싶지 않습니다. How to reload current page without losing any form data?

여전히 쿼리하지 해결 :

나는이 링크를 참조하십시오.

+2

만 대신 전체 페이지, 보안 문자를 갱신하는 것은 좋은 생각이 될 수 있는가? 일부 iFrame 콘텐츠를 만들거나 생성 된 보안 문자를 예를 들어 ajax-stuff 또는 이와 유사한 것으로 교체하십시오. 키는 전체 페이지를 새로 고치지 않고 재생성해야하는 요소 만 - 단일 구성 요소에 대해 전체 페이지를 새로 고치는 것이 아닙니다 .--). – vegaasen

+0

@vegaasen 귀하의 조언에 감사드립니다. 하지만 나는 자바 스크립트와 jQuery에 익숙하지 않다. 그래서 만약 당신이 대답을 보내 주시기 바랍니다. –

+0

* 참고 사항 : 클라이언트 쪽을 새로 고친 후에 사용자 입력을 저장/재사용하면 안됩니다. 이 취약점에 대한 * Reflected XSS Attacks *에 대한 문이 열립니다. https://www.owasp.org/index.php/Cross-site_Scripting_(XSS) –

답변

2

다른 로직 및 구문을 사용한 후 나는 F5 또는 새로 고침 페이지를 누르면 사용자 입력을 저장하는 쉬운 방법을 발견했습니다.

파일 : 자바 스크립트 코드

<script> 
    window.onload = function() 
    { 
     var a = sessionStorage.getItem('userId'); 
     if(a !== null){ 
      //alert(a); 
      document.getElementById("userId").value = a; 
     } 
    } 

    window.onbeforeunload = function() { 
     sessionStorage.setItem("userId", $('#userId').val()); 
    } 
</script> 
+0

onbeforeunload 이벤트는 사용자가 사이트 페이지 사이를 이동할 때도 발생합니다 –