• 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

use getParameter() to get info. from text box

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In "Input.jsp" page, we have a form including a text box field for user to enter their name, like <INPUT TYPE="text" NAME="userName" ...>, when user clicks the submit button, it goes to "ProcessServlet.java". Now in the receiving servlet, if we use
String name = req.getParameter("userName");
it should get the input string, but we also need to handle a case when user does NOT enter anything in the box, we want to redirect to an error page. but, I found the code
if(name == null)
res.sendRedirect("errorPage.htm");
does NOT work though.
Is it because the "req.getParameter("userName")" is NOT returning a null string even user doesn't enter anything ? then what's the easiest way to detect if user enters anything using the .getParameter() ?
Thanks,
Ian
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getParameter("userName") will generally be null if the parameter was not on the page (ie there was no textbox called 'userName')
otherwise getParameter("userName").trim().equals("") (ie it will be the empty String)
Carefull to check it wasn't null first...
Dave
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just like this:
String temp1=new String();
temp1=req.getParameter("userName");
int n=temp1.length();
if(n == 0)
res.sendRedirect("errorPage.htm");
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check for null before using the code that Sura posted.
if temp is null, int n=temp1.length() will throw a NullPointerException.
What David posted should do it.

Bosun
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friend just try this if it works..
if (!(name == null))
{
}else
{
//code for Forward
}
regd's
sandeep
reply
    Bookmark Topic Watch Topic
  • New Topic