• Post Reply Bookmark Topic Watch Topic
  • New Topic

use getParameter() to get info. from text box

 
Ian Yang
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Sura Lin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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");
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • 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
 
sandeep balyan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friend just try this if it works..
if (!(name == null))
{
}else
{
//code for Forward
}
regd's
sandeep
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!