aspose file tools*
The moose likes Servlets and the fly likes getParameter() in JSP always returns null...why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getParameter() in JSP always returns null...why?" Watch "getParameter() in JSP always returns null...why?" New topic
Author

getParameter() in JSP always returns null...why?

Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
How come
<%= request.getParameter("testParam") %>
always return null, this is my code:
<HTML>
<HEAD>
<TITLE>JSP ExpressionsZZZZ</TITLE>
</HEAD>
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date() %>
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %>
<LI>The <CODE>testParam</CODE> form parameter:
<%= request.getParameter("testParam") %>
</UL>
</BODY>
</HTML>

thanks.
sim sim
Ranch Hand

Joined: Jun 05, 2002
Posts: 55
Where is the element by name 'testParam' and where are u setting its value ?
Sim Sim
Sridhar M Swaminathan
Greenhorn

Joined: Apr 08, 2002
Posts: 7
Hi..
Well..before using the variable u should declare it.
it should be <DataType> <varName> = request.getParameter("..");
then u can access <varName> in your jsp page...
Hope this will help
- Sridhar
Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
I'm actually reading a ref. book by marty hall. I still don't know were to define the parameter ?
Could you be more specific please.
Is it possible to embed the parameter definition, instead of create a text item and etc...
thanks.
[ June 26, 2002: Message edited by: Ayman Jaffar ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Each request may (or may not) contain a 'querystring'.

This request doesn't:
http://www.foo.com/mypage.jsp

This request does:
http://www.foo.com/mypage.jsp?testParam=Hi%20There

So what would happen if you used the first URL to view your jsp page? When you try to retrieve the value of testParam from the querystring (by calling request.getParameter() ), you will get 'null' printed out. This is only appropriate, since you didn't send a querystring containing that parameter.

There is no real way to 'fake' a request parameter (ie: you can't declare it as a variable in your JSP, and then expect request.getParameter to find it). But for 'quick and dirty', you can make it a hidden field in the form that submits to the jsp in question.
[ June 26, 2002: Message edited by: Mike Curwen ]
Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
Thanks for this reply, it helped alot, but why won't the following work when added to the same jsp file ?


ayman.
[ June 29, 2002: Message edited by: Ayman Jaffar ]
sim sim
Ranch Hand

Joined: Jun 05, 2002
Posts: 55
Hey dude , U r working with html and not xml so just remove that '/' before closing the input tag. That might help
Sim Sim.
Dave Comeau
Greenhorn

Joined: Jun 14, 2002
Posts: 27
Actually, it's valid XHTML. Most web browsers don't have a problem with it, although there should be a space before the />
I wite all my HTML as XHTML.
Check out http://www.w3schools.com/tags/tag_input.asp
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

If you on a HTML/JSP/... have:

Then on 'page2.jsp' you can use:

to get the value of 'testParam'
/Rene
[ July 01, 2002: Message edited by: Rene Larsen ]

Regards, Rene Larsen
Dropbox Invite
Ayman Jaffar
Ranch Hand

Joined: Sep 06, 2000
Posts: 57
Thanks alot guys.
I just saw this post, and wow, thanks for the replies.

[ July 01, 2002: Message edited by: Ayman Jaffar ]
 
Consider Paul's rocket mass heater.
 
subject: getParameter() in JSP always returns null...why?