Bernice
Bernice

Reputation: 2592

JSP simple program

I am trying to do a JSP program where there is a number and a button. Upon clicking the button, the number above increments. I need to use sessions in this program.

This is the code I did:

<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>

The result is that every time I run the program, the number increments.. however I do not want this to happen.. I want the number to increment upon clicking the button :/ What am I doing wrong?

Thanks for any help. Would be very much appreciated!

Upvotes: 0

Views: 899

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109613

A schematic JSP, as it could have been done.

Here I assume that the page is named "counter.jsp" and that the AddCount class resides in a package "mypkg".

JSP encodings can be set in the HTML header lines, before the first HTML browser text.

For ISO-8859-1 you may actually use encoding Windows-1252, with extra chars, like special comma-like quotes. Even MacOS browsers will accept these.

Here I check whether the button was clicked, as whether the form parameter "somefield" is present. (There are other possibilities.)

session="true" is crucial here.

<%@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>

Upvotes: 1

Related Questions