MyClass.java contain a method public String getName().
The problem is with the expression name.
First time when the index.jsp is called, everything is fine. But when the hyperlink on A, B or C is pressed, the expression "name " is evaluated to null.WHY ?
And again if the index.jsp is refreshed before any hyperlinks clicked, the expression "name" is evaluated to Bibhudutta.
I m confused. please help.
With regards to your linking issue, links in a JSP should be made using server-relative addressing that starts with the context path. See the JspFaq for more information.
But, what happens when you click various hyperlinks? Does it throw any error? DId you check console? If it is calling the JSP atall (I will assume it does) , it will find request parameter param and set its value. It WON'T be null. It should evaluate else condition. but at none of the places value of variable 'name' is set except initialization which is null. so you see null as output.
If this is just a toy programe,then it might be OK to have java code in jsp. but when you want to do real coding, as pointed out , scriplets should not be used. Java code belongs to java files. JSP is templating technology and should be used for display purpose only. Use JSTL/EL as necessary.
Actually, I would say even for toy programs use MVC architecture only, because this will create bad coding habits. Code right , right from the beginning :)
Saurabh Pillai wrote:If this is just a toy programe,then it might be OK to have java code in jsp.
I'll disagree. Toy programs and prototypes have a way of growing into something real. And once bad code is established, it's really hard to excise.
Besides, even for toy programs, it's best to always practice good habits. That way, bad habits never have a chance of taking hold.
Bibhudutta Pradhan wrote:Yes. Of course this is just a toy programe. I m just experimenting in my home.
The code is running without any exception.
The problem is in the conditional.
1) the parameter "param" is null initially. Therefore the first condition (and first time) work fine:
2) when you press a hyperlink "param" value is : a,b or c
therefore "param" is not null, is to say, enter in the second condition:
this will print "called for a,b or c" (depend of link)
will be NULL, because dont call "MyClass.java" and dont fill the variable "name"
I hope you understand. Sometime is difficult to see our own mistakes! and we must to ask to other persons. That happen to me sometimes.
I recommend you, learn JSTL and the same time then you will see the advantages beetwen JSTL vs Java Scriplets.
Follow advices and you will win.
Luck and Sorry for any english grammar mistake.
The very first time index.jsp is called it gives out put
inside scriptlet : Bibhudutta
A B C
Note that the variable String name(in index.jsp) is an instance variable. The first time when s==null, getName() is called and the name instance variable is initialised.
So every time index.jsp called, the variable name should retain its value. Isnt it ?
But, when any of the hyperlinks is clicked the expression is evaluated to null. That is the instance variable is null.
The Interesting but confusing is that :
1) the first time when index.jsp is called, getName() is invoked.(OK)
2) If the page is refreshed, getName() is called again(OK).
3) Now , on clicking on the hyperlinks, the expression is evaluated to Bibhudutta as expected.(Not Ok. The page has to be refreshed).
So why do I need to refresh index.jsp ?
I will try to answer you.
1) So every time index.jsp called, the variable name should retain its value. Isnt it ?
Read and make an example about "BEANS" , and you will understand a little about retain variables.
2) Now , on clicking on the hyperlinks, the expression is evaluated to Bibhudutta as expected.(Not Ok. The page has to be refreshed).
I saw in you code this:
I delete the exclamation sign "<%!"
And now when i refresh the page or press any link the variable "name" is NULL. That is ok. Because you are initialling "name = null" in the beginning of you code. Therefore any retain value inside you "name " variable will be replced by "null".
I dont know if i understood very well you questions. Let me know if it is OK for you.