임의의 코드를 생성하고 텍스트박스로 입력받아서 비교하는 프로그램인데요

비교가 한박자 늦게 이루어집니다.. 예를 들어 첫 번째 코드가 1, 두번째 코드가 2로 가정하면

화면에 표시된 코드가 1일 때 사용자가 1을 입력하고 비교 버튼을 누르면 1과 1을 비교하는게 아니라 2와 1을 비교한다는 거죠.

맨 처음에는 아예 빈칸하고 1을 비교하는 걸로 보입니다. 그 뒤로 쭉쭉 하나씩 밀리는걸 보니..

어떻게 해결해야 할까요? 8시간쨰 고민하는데 도저히 생각이 안나네요.,.. ㅠㅠ

-----------------------------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\">
<jsp:useBean id=\"spam\" scope=\"page\" class=\"jspbook.BoardProject.SpamTest\"/>
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=EUC-KR\">
<title>Insert title here</title>
</head>
<body>
<center>
<%
if(spam.getCnt() == 0)
 spam.Mkcode();
%>
<form name=form1 method=\"post\">
<h2> Fill textbox with spam protection code!</h2>
<jsp:getProperty property=\"gencode\" name=\"spam\"/>
<input type = \"text\" name = \"code\">
<input type = \"submit\" value=\"check!\">
<input type = \"reset\" value=\"rst\">
<br>
</form>
<%
if(request.getMethod().equals(\"POST\") )
{
%>
<jsp:setProperty property=\"*\" name=\"spam\"/>
<jsp:getProperty property=\"result\" name=\"spam\"/>
<% } %>
<br>
generated : <jsp:getProperty property=\"gencode\" name=\"spam\"/>
<br>
input : <jsp:getProperty property=\"inputcode\" name=\"spam\"/>
</center>
</body>
</html>

------------------------------------------------------------------------java 파일
public class SpamTest {
 private String gencode = \"\";
 private String inputcode = \"\";
 private int res;
 private int cnt = 0;
 public void Mkcode(){
  cnt = 1;
  gencode = \"\";
  int index = 5 + (int)(Math.random() * 3);
  for(int i=0; i<index; i++)
  {
   char tempchr = new Character( (char) (33 + (int)(Math.random() * 93) ) );
   this.gencode += Character.toString(tempchr);
  }
  if(gencode.equals(inputcode) )
  { res = 1; }
  else res = 0;
 }
 public void setCode(String code){ inputcode = code; }
 public String getInputcode(){  return inputcode; }
 public String getGencode(){   return gencode;  }
 public int getCnt(){    return cnt;   }
 public void setCnt(int cnt){  this.cnt = cnt;  }
 public String getResult(){
  if( res==1 ) { cnt=0; return \"Good!\"; }
  else { return \"Bad!\";}
 }
}