Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to avoid show buttons

 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if it is not good way please tell me a good way?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4511
272
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using Boolean.TRUE, and simplifying the conditional logic to just <c:when test="${total0}">.
 
Samanthi perera
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4511
272
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4511
272
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 4511
272
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i run this is jps it print both falseee and otherwiseeeeeeeee


if i use this tooo same

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4511
272
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic