File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp file based on getparameter condition" Watch "jsp file based on getparameter condition" New topic

jsp file based on getparameter condition

Ganesh N

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:
<form name="fn" action="file1.jsp?q=tb.getText()">
Query: <input type="text" value="" name="tb">
<input type="submit" value="submit">
if(request.getParameter("q") != null)
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
Frank Carver

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 . 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.

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

Joined: Dec 12, 2000
Posts: 26
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.
I agree. Here's the link:
subject: jsp file based on getparameter condition
jQuery in Action, 3rd edition