• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with hyperlinking the same jsp page  RSS feed

 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a jsp page named index.jsp. And a simple java file MyClass.java
MyClass.java contain a method public String getName().

index.jsp
------------------


MyClass.java
-------------------


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.
 
Prasad Krishnegowda
Ranch Hand
Posts: 672
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you to clear all Java codes inside JSP and move to servlets, its been discredited for 10 years now.
Then, you can post the error/exception if any, and we are glad to help you out.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. The time to be using Java code in a JSP is long long passed.

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.
 
Saurabh Pillai
Ranch Hand
Posts: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First time it works because, it does not find parameter param and hence s is set to null. so it evaluates if condition.

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 :)
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Of course this is just a toy programe. I m just experimenting in my home.

The code is running without any exception.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the purpose of this toy program? I suspect it's to learn, right? If so, why are you using bad practices rather than learning the good ones?
 
jhon masco
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Hi Bibhudutta
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)
and:

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.
JHON

 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. so jsp should not contain any scriptlets ?
please explain it for me. because i dont know what ig good and what is bad .
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's easy: scriptlets are out, JSTL and EL are in. This has been true since 2002.

Want to make sure you don't slip up? Set the <scripting-invalid> element in the deployment descriptor to true.
 
Bibhudutta Pradhan
Ranch Hand
Posts: 31
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jhon. Thanks for reply.

The very first time index.jsp is called it gives out put
------------------------------------
inside scriptlet : Bibhudutta
name loaded
A B C

Bibhudutta
------------------------------------
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 ?
 
jhon masco
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HI Bibhudutta
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.

Suerte!!!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!