Simple JSP question

About Scriptlets

Page 1 of 1

7 Replies - 523 Views - Last Post: 29 November 2009 - 11:36 AM Rate Topic: -----

#1 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Simple JSP question

Posted 29 November 2009 - 03:49 AM

Hello there.
Lets say I have this piece of code

<html>
<body>
<h1> This is a counter example </h1>

<%! int i=0; %>

<%
	i++;
%>

Hello World! <%= "This JSP has been accessed "+i+" times" %>
</body>
</html>



Every time I reload the page i goes ++.
So, why that happens? Is it something like cached memory that saves i and when I run it remembers the previous values?
Just confused between that and sessions.I thought you used session to acomplish that.
Any explanation will get me out of this confusing mess :crazy:

Is This A Good Question/Topic? 0
  • +

Replies To: Simple JSP question

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Simple JSP question

Posted 29 November 2009 - 07:39 AM

Why do you have an exclamation mark there?

<%! int i=0; %>



It's been a while since I've worked with JSP, but if I t recall correctly, it should set i to zero each iteration, thus each page refresh would display i as 1.

Why did you separate 'i' and it's increment in two different scriplets?
Was This Post Helpful? 0
  • +
  • -

#3 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Simple JSP question

Posted 29 November 2009 - 08:07 AM

I just got a book in jsp and it says that:

When you want to declare variables or classes use these tags : <%! %>
When you want to write scriptlets use these : <% %>
And when you want to print an expression include these : <%= %>

Thats for the tags.Now for the iteration I thought the same but when I run it every time I reloaded the page i was getting ++.
Really cant explain it.If someone can that would be delightful :)
Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Simple JSP question

Posted 29 November 2009 - 08:32 AM

I know why I thought the tag was weird:

<%! int serverInstanceVariable = 1;%>
 
 <% int localStackBasedVariable = 1; %>




Using the first causes it to be a server variable, which will persist until you kill your connection. Use a regular scriplet and you'll get what you expect. :)


Use <%! when you want to load a persistent class/object/library/etc....

Most of the time for that you'll do something like this though:

<%@ page session="false" import="java.util.Iterator"%>
<jsp:useBean id="department" scope="request" class="domain.Department"/>
<jsp:useBean id="employee" scope="request" class="domain.Employee"/>
<jsp:useBean id="hiring" scope="request" class="domain.Hiring"/>

<%-- bla bla bla --%>

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> 

<%--but i digress--%>


This post has been edited by KYA: 29 November 2009 - 08:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Simple JSP question

Posted 29 November 2009 - 08:44 AM

Nice explanation about the tags, thanks!
But still didnt got why the i becomes bigger and not 0 every time. :P
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Simple JSP question

Posted 29 November 2009 - 08:46 AM

The first time you run it 'i' is initialized as a server side variable. Thus, every refresh it is persistent and is incremented. It isn't recreated each time. Only when you "leave" and come back.
Was This Post Helpful? 0
  • +
  • -

#7 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • Posts: 330
  • Joined: 13-December 07

Re: Simple JSP question

Posted 29 November 2009 - 09:14 AM

View PostKYA, on 29 Nov, 2009 - 07:46 AM, said:

The first time you run it 'i' is initialized as a server side variable. Thus, every refresh it is persistent and is incremented. It isn't recreated each time. Only when you "leave" and come back.


Oh I get it.Could you specifie me one last thing.
When the connection is closed?I mean when is it going to recreate?Is it some time period?
Or when I close my browser?
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3089
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: Simple JSP question

Posted 29 November 2009 - 11:36 AM

Not sure off the top of my head. I'd have to re setup Tomcat for a local test. Why don't you experiment a bit and find the answer for yourself?
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1