The moose likes JSP and the fly likes using submit button in jsp to set a boolean value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "using submit button in jsp to set a boolean value" Watch "using submit button in jsp to set a boolean value" New topic
Author

using submit button in jsp to set a boolean value

Joseph Rudman
Greenhorn

Joined: Mar 26, 2006
Posts: 13
Hello,

any help would be much appreciated.

I have put the following submit button in jsp:
<TR>
<TD ALIGN=CENTER><INPUT TYPE="SUBMIT" NAME="next" VALUE="Next"></TD>
</TR>

I have then put the following java code in:
<%
boolean next = (request.getParameter("next") != null);
session.setAttribute("next", next);
boolean previous = (request.getParameter("previous") != null);
session.setAttribute("previous", previous);
int row = 1;
session.setAttribute("row", row);
%>

now I expected when user hit next button and jsp was forwarded to another jsp that when I did the following:

//get session attributes for next/previous action from previous jsp
boolean next = (Boolean) session.getAttribute("next");
boolean previous = (Boolean) session.getAttribute("previous");
int row = (Integer) session.getAttribute("row");

that the boolean next would be set to true, but it is set to false and I do not understand why?
Can somebody help please?

Cheers Joe.
M Anderson
Greenhorn

Joined: Mar 17, 2006
Posts: 25
I'm thinking that when you go after the 'next' value from the session, that it's not actually set, and, since the default value for the primitive type boolean is false, you're reading that value as false (even thouse it's null in the session. You're code is declaring the next variable as a primitive boolean:



It might be better to declare some hidden field in the form and set it with javascript in an onclick handler, like this (your jsp code, modified):


Then, in the jsp look for this:



Or something like that. When you get a chance, check out jstl and the EL. It'll clean up your jsp code considerably. For example, in my jsp code above, it could look like this:



Anyway, you get the idea.
[ April 14, 2006: Message edited by: M Anderson ]

Mike -- SCJP, SCWCD
Joseph Rudman
Greenhorn

Joined: Mar 26, 2006
Posts: 13
I do not currently know java script and the application I'm creating is not supposed to use javascript - so I wondered if anyone new a way to get desired result without using javascript.

Cheers Joe.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

Originally posted by Joseph Rudman:

I have put the following submit button in jsp:

...

I have then put the following java code in:



Are these both in the same JSP page, or is the java code in the page that is the target of the form submission?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
M Anderson
Greenhorn

Joined: Mar 17, 2006
Posts: 25
Also, is it required that you use buttons? You could use links instead, like this:



Of course, pagehandler.jsp could be any 'handler' like a servlet or whatever you want.

There's another tag out there called the dipsplay tag.

Take a look at http://displaytag.sf.net. It can display your data, as long as it's in a collection of objects that adhere to the JavaBeans rules and provide paging, etc. Otherwise, you'll need to write your own paging code, which can be a challenge in and of itself. You'll probably want to only allow a next link if there are more pages, or a previous if they're pages before these. Also, you'll probably want to display a message like, "viewing page 1 of 10" or whatever... or "viewing items 1 - 10 of 50". The Display tag can handle all of this for you, plus offer the user the ability to sort by columns and some other features.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

No Javascript or HTML goop is necessary. I just think you may have some misunderstandings about how JSP works and when things get executed.

More info is needed as to the structure of your code as per my previous question. Your initial post is not clear about where the code fragments you included reside.
Joseph Rudman
Greenhorn

Joined: Mar 26, 2006
Posts: 13
Original message from Bear Bibeault

The submit button and java code is all included in same jsp.

Cheers Joe
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I'm not sure if this is causing your problem but
you're mixing the primative boolean type with the object wrapper Boolean.
boolean next = (Boolean) session.getAttribute("next");

In JDK1.5.0 and up, the new Auto Boxing feature may straighten this out for you but I wouldn't count on current JSP engines to be able to handle this.

I'm surprized that the following two lines compiled:
boolean next = (request.getParameter("next") != null);
session.setAttribute("next", next);
[ April 15, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61658
    
  67

If you've put the Java code:



on the same page as the form with the "next" parameter, you've put the cart before the horse.

This Java will execute on the server in order to format the HTML page containing the form that will be sent to the user. In other words, it will execute long before the user has any chance to click any buttons.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: using submit button in jsp to set a boolean value