This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes jsp file based on getparameter condition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp file based on getparameter condition" Watch "jsp file based on getparameter condition" New topic
Author

jsp file based on getparameter condition

Ganesh N
Greenhorn

Joined: May 23, 2001
Posts: 4
This is my jsp file in which I am trying to execute same jsp file after clicking the button.. How to set the if condition? If Iam setting like "if(request.getParameter("xxx") != null)" its not giving any error. The class file is getting created. But, always if condition fails. Any alternative method is there?
I tried like this:
String s=request.getParameter("q");
if( s.length() > 0)
Then, I am getting NullPointerException...

my code:
<HTML>
<HEAD>
<TITLE>
Details
</TITLE>
</HEAD>
<BODY>
<form name="fn" action="file1.jsp?q=tb.getText()">
Query: <input type="text" value="" name="tb">
<input type="submit" value="submit">
</form>
<%
if(request.getParameter("q") != null)
{
out.println(q);
}
%>
</body>
</html>
Regards,
Ganesh Natarajan
Raghvendra Sharma
Ranch Hand

Joined: Oct 09, 2000
Posts: 82
Hi there
I don't think you have been able to describe your problem in the best way. But i think i might help.
if you are getting null pointer exception at the s.length(), it is very much correct. There might not be a parameter called q in the request, in which case it would return a null.
you better check as if(s!=null) { ... }
that wo'nt give you null pointer exception.
hope it helps
raghav
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"Ganesh N",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please log in with a new name which meets the requirements.
Thanks.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Poornima Shetty
Greenhorn

Joined: Dec 12, 2000
Posts: 26
Hi,
This is a very common thing that keeps repeating.So what you can do is whenever you expect a parameter value using request.getParameter() check for not null as:
if (request.getParameter("Mode") != null)
{
//do something here;
}
if you are sure of the value that you are getting,i mean if you know the value then you could check it this way too:
if (request.getParameter("Mode")!= null
&& request.getParameter("Mode").equals("Yes"))
{
//do something here;
}
else the best way to avoid null pointer exception is use ternary operators so that even if the value is null you can put "" and avoid the exception as below:
(request.getParameter("Mode") == null?"":request.getParameter("Mode);
So in the above lines of code if the value is null then "" is put else the actual value gets substituted.
Regards,
P.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp file based on getparameter condition
 
Similar Threads
value for useBean class is invalid
Checking Status of a Checkbox
passing parameters from JSP to a Servlet
Request parameter is missing
Display of Latin Characters