Whats the problem in this code ?

G

Gaurav

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
 
P

Peter MacMillan

Gaurav said:
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.
 
P

Peter MacMillan

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>
 
G

Gaurav

thanx peter ..

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

Gaurav
 
P

Peter MacMillan

Gaurav said:
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).
 
G

Gaurav

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 !!
 
T

Tony Morris

Gaurav said:
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/
 
H

Harald

Tony Morris said:
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.
 
T

Tony Morris

Harald said:
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/
 
R

Ross Bamford

Harald said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top