2012-12-18 3 views
0

숫자와 버튼이있는 곳에 JSP 프로그램을 만들려고합니다. 버튼을 클릭하면 위의 숫자가 증가합니다. 이 프로그램에서 세션을 사용해야합니다.JSP 간단한 프로그램

이것은 내가했던 코드는 다음과 같습니다

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title> Welcome </title> 
</head> 

<body> 

<% 
    // check if there already is a "Counter" attrib in session 
    AddCount addCount = null; 
    int test = 0; 
    String s; 

    try { 
     s = session.getAttribute("Counter").toString(); 

    } catch (NullPointerException e){ 
     s = null; 
    } 

    if (s == null){ 
     // if Counter doesn't exist create a new one 

     addCount = new AddCount(); 
     session.setAttribute("Counter", addCount); 

    } else { 
     // else if it already exists, increment it 
     addCount = (AddCount) session.getAttribute("Counter"); 
     test = addCount.getCounter(); 
     addCount.setCounter(test); 
     addCount.addCounter(); // increment counter 
     session.setAttribute("Counter", addCount); 
    } 

%> 

<%! public void displayNum(){ %> 
     <p> Count: <%= test %> </p> 
<%! } %> 

<input TYPE="button" ONCLICK="displayNum()" value="Add 1" /> 

</body> 
</html> 

결과는 내가 프로그램을 실행할 때마다이 숫자의 증가가 .. 그러나 내가 증가시킬 수를 원하는 .. 이런 일이하고 싶지 않은 것입니다 버튼을 클릭하면 :/내가 뭘 잘못하고 있니?

도움 주셔서 감사합니다. 대단히 감사하겠습니다!

+0

JSP와 HTML/JS에 관해 어디에서 배웠습니까? JS와 JS를 혼동하고있다. (oldschool 90의 HTML 스타일과 대문자로 된 속성 이름을 사용하고 oldschool 00의 JSP * scriptlets *를 사용한다.) 주제에 대한 적절한 자습서를 읽고 계십니까? 이 웹 사이트의 – BalusC

+0

: http://www.jsptut.com/Sessions.jsp :/더 좋은 것이 있습니까? 나는 정말로 혼란 스럽다. JSP인지 JS인지는 모르겠다. :/어디서부터 시작해야할지 모르겠다./ – Bernice

+0

여기서 JS를 사용하고 있는가? :/ – Bernice

답변

1

도식 JSP로 할 수 있습니다.

여기서 "counter.jsp"라는 페이지가 있고 AddCount 클래스가 "mypkg"패키지에 있다고 가정합니다.

첫 번째 HTML 브라우저 텍스트 앞에 HTML 헤더 행에 JSP 인코딩을 설정할 수 있습니다.

ISO-8859-1의 경우 실제로 쉼표와 같은 따옴표 같은 추가 문자가 포함 된 인코딩 Windows-1252를 사용할 수 있습니다. MacOS 브라우저조차도이를 수용 할 것입니다.

여기서 "somefield"라는 양식 매개 변수가 있는지 여부와 같이 버튼이 클릭되었는지 확인합니다. (다른 가능성이 있습니다.)

session = "true"이 중요합니다.

<%@page contentType="text/html; charset=Windows-1252" 
     pageEncoding="Windows-1252" 
     session="true" 
     import="java.util.Map, java.util.HashMap, mypkg.AddCount" %> 
<% 
    // Check if there already is a "Counter" attrib in session 
    AddCount addCount = (AddCount)session.getAttribute("Counter"); 
    if (addCount == null) { 
     // If Counter doesn't exist create a new one 
     addCount = new AddCount(); 
     session.setAttribute("Counter", addCount); 
    } 

    // Inspect GET/POST parameters: 
    String somefield = request.getParameter("somefield"); 
    if (field != null) { 
     // Form was submitted: 

     addCount.addCounter(); // increment counter 
    } 

    int count = addCount.getCounter(); 
%> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
     <title>Welcome - counter.jsp</title> 
    </head> 

    <body> 
     <p> Count: <%= count %></p> 
     <form action="counter.jsp" method="post"> 
      <input type="hidden" name="somefield" value="x" /> 
      <input type="submit" value="Add 1" /> 
     </form> 
    </body> 
</html> 
+0

답장을 보내 주셔서 감사합니다. 정말 도움이되었습니다. :) – Bernice