0

I have one text box. While submitting my form it navigates to another JSP file. On that file i want to set session value and next to line i want to get that session value.

For example:

File1.JSP

<form action="File2.JSP" method="post">
    <input type="text" name="searchtxt"/>
    <input type=submit value="Save"/>
</form>

File2.JSP

<% 
    String searxhtxt=request.getParameter("searchtxt");
    if(searchtxt!=null && !searchtxt.equals("")){
        request.getSession().setAttribute("searchtxt",searchtxt);
    }
    String text=session.getAttribute("searchtxt").toString();
 %>

But always I am getting null value even the text box contains empty or some value. How to solve this problem?

Uooo
  • 6,204
  • 8
  • 36
  • 63
Selva
  • 37
  • 1
  • 3
  • 10

2 Answers2

0

There is typo mistake
Change

String searxhtxt=request.getParameter("searchtxt");
           ↑  

to

String searchtxt=request.getParameter("searchtxt");

You are using searchtxt

if(searchtxt!=null && !searchtxt.equals("")){
       ↑                   ↑
    request.getSession().setAttribute("searchtxt",searchtxt);
                                                      ↑
}    

See also

Community
  • 1
  • 1
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
0

Aniket is right you have typo mistake in your code.

Another things i notice about getting null value is when you do not enter any string(empty textbox). You have checked in if condition that is textbox value is not empty than and than you store that value in session, when value is empty your session didn't contain value or session variable searchtxt which give null as return.

When you enter any value its work fine and you get value correct.

Edit :
You may change following to avoid Exception

String text= session.getAttribute("searchtxt")!=null?session.getAttribute("searchtxt").toString():"";    
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50