This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes getting null in second servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting null in second servlet" Watch "getting null in second servlet" New topic
Author

getting null in second servlet

Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
hey all

i have 2 servlets called signup.java and submitfavourite.java

i am trying to pass a variable username from signup to submitfavourite like this...

code for signup.java



code for submitfavourite.java



but in my database i am getting null instead of username means session.getAttribute("uname") is returning null. i also tried this by request.getAttribute but that is also returning null.
please help me why it is returning null..

thanhx.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Please properly indent your code when posting, unindented code is very hard to read. Also you should be following established naming conventions for Java. Class names should be Signup and SubmitFavourite,


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
sorry sir but now it have done it..
please help me.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

What is "req" in submitfavourite.java referring to? Should it be "req1" instead?

Also note that creating SQL by concatenating strings together is prone to SQL injection. You want to use binding variables instead.
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
sir i also tried with req instead of req1 but same null is returning.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

You'll get more help if you re-post your code with proper indentation and following conventions.
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Bear Bibeault wrote:You'll get more help if you re-post your code with proper indentation and following conventions.

That's right, it's currently difficult to read and I'm not sure if some errors like the one below are just a formatting issue or actual mistake in the code:

(notice the quote character after the username variable)
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
code for SignUp






code for SubmitFavourite




Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
now is it okay or should i do anything else sir???
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Sachin Kadian wrote:now is it okay or should i do anything else sir???

Now, now...

In one place you do request.setAttribute("uname"), but in the other you then do session.getAttribute("uname")... did you try request.getAttribute("uname") instead?
Sachin Kadian
Ranch Hand

Joined: Jan 24, 2012
Posts: 33
ohh yaa.. that was realy an error sir.. i did it by mistake. thanhx for showing this... now it is working properly.. thanhx a lot.
 
 
subject: getting null in second servlet
 
Similar Threads
problem with while loop when ResultSet.next() is called
problem with code
Regarding Servlet connectivity with ms-access
Requested Dispatcher
problem with retrieving data