Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jsp file based on getparameter condition

 
Ganesh N
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
 
Raghvendra Sharma
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • 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
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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.
 
Poornima Shetty
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic