File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to avoid show buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to avoid show buttons" Watch "how to avoid show buttons" New topic
Author

how to avoid show buttons

Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
this is my jsp

this is my calculate.java servlet


there are many buttons and they have several values.
when i press button it decreases the total value.
when total is 0 the thank you message is appears.
when total is 0 i need to avoid rthe showing buttons.
how to do this?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
In your Servlet you could set an attribute (request or session attribute) when total=0;

Then you get it on your jsp. If it's null the buttons are visible, if not they aren't.
In the same way. If that attribute you set in the Servlet is null you don't show the message. If it is not, you do show it.


In the Servlet:


The following is the jsp code:




Bye,
Nicola
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(With the caveat that scriptlets aren't the cleanest way to do this--avoid them, including the bit from lines 19-28. No reason to do that in the JSP, and good reasons not to.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
if it is not good way please tell me a good way?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2891
    
  19

Scriptlets are java code fragments. So, where do YOU think you should place them on?
You may use standard actions or JSTLs on the JSP, but not scriptlets.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
My Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Yes, right.

Assuming you have the same request attribute total0 set in your servlet



With the core library the test in your jsp could be written as follows:

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Consider using Boolean.TRUE, and simplifying the conditional logic to just <c:when test="${total0}">.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
in my servlet it get servlet using below code


then each request for clicking button it makes a new session
how to solve this issue?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2891
    
  19

Samanthi perera wrote:each request for clicking button it makes a new session

With all due respect, have you every seen the Servlet specification or the documentations of those classes? Which part of the documentation made you believe that getSession() always creates a new session?
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
I DON'T REMEMBER WHERE.But i heard that if we use
HttpSession session = request.getSession();
it makes a new session for each request.
if we use HttpSession session = request.getSession(false it gets the old session).
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
i change my servlet.java


now my jsp is like this


but it always print
Thank you very much ccccccccccccccccccccccccccc.
it can't be happen.because it is in c:choose part.how it happens?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2891
    
  19

Samanthi perera wrote:I DON'T REMEMBER WHERE.

Please KeepItDown.

Samanthi perera wrote:But i heard that if we use....

Do you thing listening to someone and believing whatever he say is better than going through the specifications or documentations to understand the truth?

Samanthi perera wrote:
HttpSession session = request.getSession();
it makes a new session for each request.
if we use HttpSession session = request.getSession(false it gets the old session)

Please go through the documentation of HttpServletRequest and read it.
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2891
    
  19

Samanthi perera wrote:but it always print
Thank you very much ccccccccccccccccccccccccccc.
it can't be happen.because it is in c:choose part.how it happens?

Is their any taglib declaration for the JSTL core in your JSP?
I think you should have a look at the JSTL Tutorial.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
if i run this is jps it print both falseee and otherwiseeeeeeeee


if i use this tooo same

Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 2891
    
  19

Devaka Cooray wrote:Is their any taglib declaration for the JSTL core in your JSP?
I think you should have a look at the JSTL Tutorial.
Samanthi perera
Ranch Hand

Joined: Jan 08, 2010
Posts: 510
it is ok now.
i have added
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> to the jsp and add jstl jar file.
not it is working.
when it comes to 0 value it prints thank you.
but after that if i click back button in the browser it shows the buttons and when i click it shows valus as minus valus.
how to avoid this?
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
It's ok.

If you set an attribute in a HttpServletRequest object it happens exactly as you are saying.
Once the request is gone, the attribute is gone as well.

Now, if you want, please try to put the attribute total0 in your session instead of in your request.

I mean:

1) Delete the statement:



2) add this one



It works as you expected it should.
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
And sorry i would like to add this too:

if you set an attribute in your session, it will stay there during your session life.
So, if your total value is not zero anymore and then you want to show your buttons again during the same session, remember to remove the attribute from your session.

For example, in your servlet you would do like that:



and your buttons will appear again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to avoid show buttons
 
Similar Threads
exception
exception while running code
resultset is not working properly
error message
how to getSession in simple java class?