Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Whats the problem in this code ?

Reply
Thread Tools

Whats the problem in this code ?

 
 
Gaurav
Guest
Posts: n/a
 
      05-01-2005
MY CODE IS

<% if (Status.equals("true") ) {

int Answer=9;
%>
<script language = "JAVASCRIPT">
window.alert("Your record has been added ");
var choice = window.confirm("Do you want to add the Instance of
this particular Resource ? ");

if (choice == true) {
window.alert("You are going to add a new Instance");
<% Answer=1; %>
window.alert("answer = " + <%= Answer %>);

} else {
window.alert("Thanx for NOT entering the record");
<% Answer=89; %>

}

window.alert("answer = " + <%= Answer %>);
</script>

%>


NOW THE PROBLEM IS

I am always gettin the "Answer" as 89, Irrespective what I choose in
the confirm button.

if i choose "True" to my confirm button, First my "Answer" is printed
as 1 , then it also executes the JSP tag in the "else" clause and makes
my "Answer" as 89.

Kindly help, what is the error ? Is it because I am embeeding JSP in
Jscript ?

regards
gaurav

 
Reply With Quote
 
 
 
 
Peter MacMillan
Guest
Posts: n/a
 
      05-01-2005
Gaurav wrote:
> MY CODE IS

[snip - incomplete and not compilable jsp]
> I am always gettin the "Answer" as 89, Irrespective what I choose in
> the confirm button.
>


Javascript does not affect the runtime of the JSP. That is, an if
statement has no bearing on the JSP because it (the javascript) is run
on the client - not the server.

If you cut out the HTML (which, at most, is only sent as-is to the
browser), you get:

Answer=1;
Answer=89;

So it's no wonder that, in the end:

> if i choose "True" to my confirm button, First my "Answer" is printed
> as 1 , then it also executes the JSP tag in the "else" clause and makes
> my "Answer" as 89.
>


No. The server executes both because there is no server-side logic to
prevent it. The JSP has finished running long before the browser shows
this to you, however (the server generates the HTML from the JSP which
is sent to the browser as HTML. There is no magic glue that ties the
browser to the JSP).

What you should do is create a page (not some screwey jscript alert)
that says: "the record has been added." and then have a button with a
label "Create new instance". That button would post to another jsp (or
you could write it so that this jsp will handle the click - either way)
and that jsp would handle the semantics of creating a new instance
(whatever that is).


> Kindly help, what is the error ? Is it because I am embeeding JSP in
> Jscript ?


Well, yes. Since you can't *actually* "embed" jsp in jscript.

>
> regards
> gaurav
>



--
Peter MacMillan
e-mail/msn:
 
Reply With Quote
 
 
 
 
Peter MacMillan
Guest
Posts: n/a
 
      05-01-2005
Just in case I wasn't clear - which I'm usually not - here's a better
idea of what I meant:

<%
int answer = 42;

// you didn't define where "Status" came from
// so I assumed you meant a query parameter
String status = request.getParameter("status");

if ((status != null) && (status.equals("true"))) {
answer = 9;
}
%>
<script language = "JAVASCRIPT">
var choice = window.confirm("Do you want to add the Instance of this
particular Resource ? ");

// we can pass the value of the JSP answer to the html file.
// remember that it's one way only and that they are not the
// same variable.
var answer = <%= answer %>;

if (choice) {
window.alert("choice == true");
answer = 1;
} else {
window.alert("choice == false");
answer = 2;
}

window.alert("answer = " + answer);
// at this point you could call a jsp with the new answer
// eg. window.location = "otherPage.jsp?answer=" + answer;
</script>


--
Peter MacMillan
e-mail/msn:
 
Reply With Quote
 
Gaurav
Guest
Posts: n/a
 
      05-01-2005
thanx peter ..

Can you tell me, what method is good ? whether to generate HTML pages
dynamically , or statically ? which option is most suited ?

Gaurav

 
Reply With Quote
 
Peter MacMillan
Guest
Posts: n/a
 
      05-01-2005
Gaurav wrote:
> thanx peter ..
>
> Can you tell me, what method is good ? whether to generate HTML pages
> dynamically , or statically ? which option is most suited ?
>
> Gaurav
>


It depends on what you're doing. If it's just some client-side effect
then you can get away with the code I posted above (in reply to my
post). However, if you're going to be doing something server-side in
reaction to the user's response (eg. conditionally create a new database
record) then you have to somehow send the users response to the server
(you can use javascript to make it "magical" to the user, but I prefer
making it clear that something is going on).

--
Peter MacMillan
e-mail/msn:
 
Reply With Quote
 
Gaurav
Guest
Posts: n/a
 
      05-01-2005
thanx man .. I am now not using JSCRIPT . Instead showing the status of
the addition on the JSP page and then asking the user to choose from
the two choice, that wheather he want to enter the Instance of that
Record. If he choose that, he would be redirected to a new JSP page ,
and if does not, he would be logged out !!

 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-02-2005

"Gaurav" <> wrote in message
news: oups.com...
> MY CODE IS
>
> <% if (Status.equals("true") ) {
>
> int Answer=9;
> %>
> <script language = "JAVASCRIPT">
> window.alert("Your record has been added ");
> var choice = window.confirm("Do you want to add the Instance of
> this particular Resource ? ");
>
> if (choice == true) {
> window.alert("You are going to add a new Instance");
> <% Answer=1; %>
> window.alert("answer = " + <%= Answer %>);
>
> } else {
> window.alert("Thanx for NOT entering the record");
> <% Answer=89; %>
>
> }
>
> window.alert("answer = " + <%= Answer %>);
> </script>
>
> %>
>
>
> NOW THE PROBLEM IS
>
> I am always gettin the "Answer" as 89, Irrespective what I choose in
> the confirm button.
>
> if i choose "True" to my confirm button, First my "Answer" is printed
> as 1 , then it also executes the JSP tag in the "else" clause and makes
> my "Answer" as 89.
>
> Kindly help, what is the error ? Is it because I am embeeding JSP in
> Jscript ?
>
> regards
> gaurav
>


There are a number of problems.
First and foremost, the use of JSP scriptlets implies a design flaw.
Second, and more subjective (or more correctly, less obvious), the use of
JSP implies a design flaw.
I will only speculate that your stated problem is the result of confusion of
what happens when a JSP is translated and then compiled on the application
server versus what a web browser might be doing with client side scripting

--
Tony Morris

JTiger Unit Test Framework for J2SE 1.5
http://www.jtiger.org/
Java Q&A (FAQ, Trivia)
http://qa.jtiger.org/
http://xdweb.net/~dibblego/


 
Reply With Quote
 
Harald
Guest
Posts: n/a
 
      05-02-2005
"Tony Morris" <> writes:
> Second, and more subjective (or more correctly, less obvious), the use of
> JSP implies a design flaw.


Ripping this statement out of context may lead to a wrong
interpretation. But do you suggest that JSP is generally a bad idea or
was this just meant for the context of the OPs problem? If it is the
former, I wonder what your reasons are.

I am taking the risk of igniting a flame war here, but since I myself
think that the syntax mix used in JSP is ugly and compilation after
deployment is evil, I am interested to see whether I am the only one
feeling a bit uneasy about JSP.

Harald.

--
---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-02-2005

"Harald" <> wrote in message
news:87fyx5tqo1.fsf_-_@gmx.de...
> "Tony Morris" <> writes:
> > Second, and more subjective (or more correctly, less obvious), the use

of
> > JSP implies a design flaw.

>
> Ripping this statement out of context may lead to a wrong
> interpretation. But do you suggest that JSP is generally a bad idea or
> was this just meant for the context of the OPs problem? If it is the
> former, I wonder what your reasons are.


I was interpreting the original statement as face value i.e. "Whats the
problem in this code ?"

> I am taking the risk of igniting a flame war here, but since I myself
> think that the syntax mix used in JSP is ugly and compilation after
> deployment is evil, I am interested to see whether I am the only one
> feeling a bit uneasy about JSP.


Agreed that it is potentially a war.
JSP scriptlets imply a design flaw simply because business logic and
presentation have been combined.
There are, of course, those who will argue that "oh it's a only a little bit
of business logic", or "it's not *really* business logic [but it is - you've
just changed your interpretation]". I argue that if you are going to do the
job (separate the two), do it properly, purely, correctly and 100%.
JSP itself also implies the mixing of business logic and presentation
(despite first appearances).
I take the same stance as the Velocity developers.
http://jakarta.apache.org/velocity/

--
Tony Morris

JTiger Unit Test Framework for J2SE 1.5
http://www.jtiger.org/
Java Q&A (FAQ, Trivia)
http://qa.jtiger.org/
http://xdweb.net/~dibblego/


 
Reply With Quote
 
Ross Bamford
Guest
Posts: n/a
 
      05-03-2005
On Tue, 2005-05-03 at 07:37 +1000, Tony Morris wrote:
> "Harald" <> wrote in message
> news:87fyx5tqo1.fsf_-_@gmx.de...
> > "Tony Morris" <> writes:
> > > Second, and more subjective (or more correctly, less obvious), the use

> of
> > > JSP implies a design flaw.

> >
> > Ripping this statement out of context may lead to a wrong
> > interpretation. But do you suggest that JSP is generally a bad idea or
> > was this just meant for the context of the OPs problem? If it is the
> > former, I wonder what your reasons are.


> > I am taking the risk of igniting a flame war here, but since I myself
> > think that the syntax mix used in JSP is ugly and compilation after
> > deployment is evil, I am interested to see whether I am the only one
> > feeling a bit uneasy about JSP.

>
> Agreed that it is potentially a war.
> JSP scriptlets imply a design flaw simply because business logic and
> presentation have been combined.
> There are, of course, those who will argue that "oh it's a only a little bit
> of business logic", or "it's not *really* business logic [but it is - you've
> just changed your interpretation]". I argue that if you are going to do the
> job (separate the two), do it properly, purely, correctly and 100%.
> JSP itself also implies the mixing of business logic and presentation
> (despite first appearances).


My main project atm is a web framework / CMS (open source), and when I
first started on the project the battles with JSP were epic. The whole
system is just wonky to the nth degree (IM*H*O), for all the above
reasons and many more.

In the end I started looking at alternatives, looked into Velocity, and
that was that. The benefits in terms of functionality are incredible,
and the flexibility of being able to completely integrate has allowed me
use VTL (Velocity Code) for more than simple presentation.

And to top it all off, the designers who'll use the system don't take
one look and say "No way" - they say "Oh, I see".

> I take the same stance as the Velocity developers.
> http://jakarta.apache.org/velocity/
>


Try it.

Ross
--
[Ross A. Bamford] [ross AT the.website.domain]
Roscopeco Open Tech ++ Open Source + Java + Apache + CMF
http://www.roscopec0.f9.co.uk/ + in


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
whats use of ":" in java in the following code ? Madni Java 4 06-12-2006 01:18 PM
Whats wrong with this code.... is0871@gmail.com Java 10 04-11-2005 03:22 AM
whats wrong with this simple code? Robert Smith C++ 2 04-28-2004 01:01 AM
Whats wrong with this crc32 code? Matthew Wilson C Programming 1 02-17-2004 04:40 AM
Whats wrong in this code?? Tinus HTML 2 09-03-2003 10:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57