• Post Reply Bookmark Topic Watch Topic
  • New Topic

sessions keep incrementing and session.getattribute("xx") returns null

 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i am having a problem with sessions.

im working on introducing a simple captcha type aunthentication in my form submission.

here are the files:

register.html


image.jsp
<%

%>

reg.jsp


file.xml



when i checked my number of session variable, they were incrementing everytime i made a refresh or a page request to image.jsp,rather than updating the num attribute.
and sesssion.getattribute returns null in action.jsp

why is that so???,whats the remedy???

banging my head!!,help me out.
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, did some heavy bruteforcing and found a solution

made the register.html a jsp page and this time - moved the session.setAttribute from image.jsp to this jsp page(register.jsp)

problem solved.

but why didnt it work out the last time??

also want to know what could be the drawbacks of this approach,i mean about my "captcha type authentication".

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is your Java code in JSPs rather than in servlets as it should be?
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Why is your Java code in JSPs rather than in servlets as it should be?


yeah i know that!!!
it should be either be in jstl tags or servlets.....

but if find it easy to debug in jsps......you know,if you got a problem-you just make changes in the jsp file and refresh the page to check out what happens.
i dont have to recompile nor have the burden of editing the web.xml file in case of servlets.

Yes,i know,its a bad habit,but right know im not concerned about it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65522
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CodeRanch is all about learning, and learning to do things the right way. Do not be surprised of your attitude means that you'll get little in the way of help. Certainly the most senior members who could give you the best help are unlikely to help someone who doesn't care that they are using bad practices.
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:CodeRanch is all about learning, and learning to do things the right way. Do not be surprised of your attitude means that you'll get little in the way of help. Certainly the most senior members who could give you the best help are unlikely to help someone who doesn't care that they are using bad practices.


oh!!,come on man,cut me some slack..

i do respect what you guys are doing,but what difference would it make if i used servlets or jstl???,i mean the questions i asked will be the same,i've told you im concerned more about the problem not the context ...

If that is what still bothering ,i swear i'll convert those jsps into servlets,no big deal.

heyy....no offense meant so far.
 
Paul Clapham
Sheriff
Posts: 21876
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess because you're using JSP, you're assuming it's going to join your session automatically. Whereas if you were using a servlet, you would have had to write the code to get the session and then it would have worked right.

But you said it was easier to debug in JSP? So what happened all of a sudden that you couldn't debug this problem? Really, I was surprised when you said that because I've always found it a real pain to debug JSP code.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nagarjuna

Taking the advice of the more senior members means you are less likely to encounter similar problems in the future. You say you know its wrong but I'm seriously questioning what year we are in? Java code within JSP? What you doing for the millennium on new years eve?
 
Stefan Evans
Bartender
Posts: 1822
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to answer the question of "why didn't it work the first time", Paul hit the nail on the head.
You hadn't established a session.

To establish the session in the normal fashion, the server sends your browser a session cookie on the first request that establishes your session.
Your first request was to an html file - no session.
Your second request was to a jsp - it started a session, but then you overrode the response with the image file so it never sent the session cookie
The third request tried to access the session - which wasn't properly established.

Making the first page a jsp page establishes the sesion with that page, and gets your cookie which is then sent with all remaining requests.
So the required fix was just to make the first page a jsp (or visit any page that worked in your web app to establish a session)

 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
But you said it was easier to debug in JSP? So what happened all of a sudden that you couldn't debug this problem? Really, I was surprised when you said that because I've always found it a real pain to debug JSP code.

ok,i agree,but for petty problems i find it easy to solve them in jsp(i mean like - change the suspected-code->hit refresh->check if done).

you probably wouldnt have faced this,because you might be using jstl all the time,and i appreciate it.

James Boswell wrote:
Taking the advice of the more senior members means you are less likely to encounter similar problems in the future. You say you know its wrong but I'm seriously questioning what year we are in? Java code within JSP? What you doing for the millennium on new years eve?

ok,ok,i'll start learning jstl,thanks for your concern.

Stefan Evans wrote:
Your second request was to a jsp - it started a session, but then you overrode the response with the image file so it never sent the session cookie

wow,pretty cool,really made sense.

i was thinking all the time why the image.jsp failed in establishing session.

it never did occur to me,wonder you figured it out in no time,good work you have done.
 
john wutka
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nagarjuna,

Can you please post the working code with the changes.
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
john wutka wrote:Nagarjuna,

Can you please post the working code with the changes.


Sure,

here it is-

register.jsp


image.jsp


reg.jsp


file.xml


I shouldnt have changed that register.html file to register.jsp just for the sake of setting the session variable successfully,rather i should have tried to set the session successfully using the image.jsp file itself and keep the register.html unchanged,i mean not to change it as register.jsp with session setting

I was preoccupied with something else.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!