• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp file based on getparameter condition

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic