Solved

using Spaces with jsp paramters

Posted on 2004-03-30
19
346 Views
Last Modified: 2010-04-01
Hi,

My problem is im using javascript that displays an error message based the input name ,
say my input name is "MobileNumber" is displays "MobileNumber" should a be a number blah blah.....
but i want to display "Mobile Number" should be a number, but if use this im messing up other code in servlets that cant use

request.getParameter("Mobile Number");

tried using request.getParameter("Mobile+Number"); but it doesnt seem to work


Any suggestions,
Triggered
0
Comment
Question by:Triggered
  • 5
  • 5
  • 4
  • +3
19 Comments
 
LVL 9

Expert Comment

by:Nick_72
ID: 10713085
??

>>but if use this im messing up other code in servlets that cant use

>>request.getParameter("Mobile Number");

You should be able to use a space like that. Look at the small example below:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>

<body>
<form action="#" method="post">
<table>
<tr>
<td><input type="text" name="Mobile Number"><input type="submit" name=""></td>
</tr>
</table>
</form>
<%  
   out.println(request.getParameter("Mobile Number"));
%>
</body>
</html>
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10713128
As Nick says the name of the parameter does not matter, I think that Nick thought at the other question that you wanted to put spaces on the VALUE of the parameter not on the name like this

http://www.yourweb.com/yourJsp.jsp?MOBILE NUMBER=+ 34 670 09 09 09

That will get messed but not the Param name itself.

Javier
0
 

Author Comment

by:Triggered
ID: 10713211


in my servlet i have

String MobileNumber = String.valueOf(request.getParameter("Mobile Number"));

System.out.println("MobileNumber ="+MobileNumber );  //displays MobileNumber=null



but it falls over when i do the following
Integer.parseInt(MobileNumber);                //I need MobileNumber as an integer here


,but if my input name is changed to "MobileNumber" instead of using "Mobile Number" everything works??
0
 
LVL 7

Accepted Solution

by:
searlas earned 500 total points
ID: 10713236
Prefix all upper caps with spaces (exception: does not prefix the start of the string with a space):

var withSpaces = "MobileNumberSpacedOut".replace(/([^A-Z])([A-Z])/g, "$1 $2");

Prefix all changes from lower to upper caps with spaces (without space-separating a sequence of capital letters):
var withSpaces = "MobileNumberXML".replace(/([^A-Z])([A-Z]+)/g, "$1 $2");

Example:
<script type="text/javascript">
    alert("MobileNumberAndAcronymnHTML".replace(/([^A-Z])([A-Z]+)/g, "$1 $2"));
</script>

0
 
LVL 6

Expert Comment

by:jarasa
ID: 10713252
Just one thing, Why you use a Mobile Number as an Integer?

Anyway try to see if works to do this:

Integer MobileNumber = new Integer(request.getParameter("Mobile Number"));

Javier
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10713292
>Just one thing, Why you use a Mobile Number as an Integer?

To clarify this:

I don't know if you've readed my profile but I've been working all my life for a Phone company an never used the "Number of the line" as Numeric.

:c)

Javier


0
 

Author Comment

by:Triggered
ID: 10713478
still not working, business rules mean i have to use numbers....would make my job much easier if we had'nt to, but thats another story :-)
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10713502
Are you doing a post or a get to the servlet??? because as other experts have said.. having a space in the parameter doesn't matter is should still be working.....


you might get in to the problems if you are making the url in the javascript and then sending it over to servlet.
In that case you will have to use escape method of script over your variables.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10713549
Yup.... Post the code Triggered pls.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:searlas
ID: 10713628
I guess I should point out that my answer above avoids the problem/diffficulty you are having putting spaces in your parameter names.  It adds the spaces at the point where you are using javascript to display an error message.
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10713722
Yes I agree but we are trying to put the point that parameter names with spaces DOES work...

yours is a easy way out of it....:-)
0
 

Author Comment

by:Triggered
ID: 10713773

my html page has the following:-




<FORM METHOD=POST ACTION="servlet/storedetailservlet" >

where "Mobile Number" is declared as

<input name="Mobile Number" type="text" id="Mobile Number">


....................................................


heres the servlet code:





import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import Myproj.DBConectionManager;


public class storedetailservlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html";
  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  }

  public void doPost( HttpServletRequest request, HttpServletResponse response)
   throws  ServletException, IOException
  {
      HttpSession session = request.getSession();
      String MemberID= (String)session.getAttribute("MemberID");

      String MobileNumber= String.valueOf(request.getParameter("Mobile Number"));


     dbCon.storeDetails(MemberID,Integer.parseInt(MobileNumber));

   RequestDispatcher Dispatche=request.getRequestDispatcher("myAccount.jsp");
   
  }
  //Clean up resources
  public void destroy() {
  }
}
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10713827
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import Myproj.DBConectionManager;


public class storedetailservlet extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html";
  //Initialize global variables
  public void init() throws ServletException {
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  }

  public void doPost( HttpServletRequest request, HttpServletResponse response)
   throws  ServletException, IOException
  {
      HttpSession session = request.getSession();
      String MemberID= (String)session.getAttribute("MemberID");
      int  num=0;
      String MobileNumber=request.getParameter("Mobile Number");
      MobileNumber = MobileNumber.trim();
      System.out.println("got the Number as "+MobileNumber);
    try{
      num=Interger.parseInt(MobileNumber);
      }catch(java.lang.NumberFormatException)
    {
    System.out.println("Error Formating the Number, it might not be a valid Number");
    }
     dbCon.storeDetails(MemberID,num);

   RequestDispatcher Dispatche=request.getRequestDispatcher("myAccount.jsp");
   
  }
  //Clean up resources
  public void destroy() {
  }
}

Try this
0
 

Author Comment

by:Triggered
ID: 10714468
ill try that but ill b pleasantly suprised if it works as the servlet was prnting out "null" awhile ago when i had in a system.out.() for the "Mbile Number"
0
 
LVL 7

Expert Comment

by:searlas
ID: 10714832
Resorting to w3 docs again, it appears you are NOT allowed spaces in name attributes:


ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

http://www.w3.org/TR/REC-html40/types.html#type-cdata
0
 
LVL 7

Expert Comment

by:searlas
ID: 10714844
Ah shhhugar.

Forget that.  I misread the spec.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10718306
IMO, although you CAN have space in name, I'll never do it in my life.
searlas is right that you can always do something in javascript to restore those spaces.
0
 
LVL 6

Expert Comment

by:jarasa
ID: 10721787
I agree with Kenneth. but as triggered is using a servlet he should implement searlas Javascript Function as a Java one in his servlet.

I guess it will work as is in Java right?

And of course searlas should get the points. but as Kuldeepchaturvedi says we where trying to know why triggered can't get that working with the spaces in, wich is a good question too.

Javier
0
 

Author Comment

by:Triggered
ID: 10740285
Sorry for not getting back sooner,searlas that worked perfectly...thanks a million
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java.lang.UnsupportedClassVersionError 8 372
some methods private some are not 8 196
How to find missing packages when using Netbeans IDE 8.1 ? 19 46
spring example errors 33 251
Scenario: Your operations manager has discovered an anomaly in your security system. The business will start to suffer within 15 minutes if it is a major IT incident. What should she do? We have 6 recommendations for managing major incidents (https:…
Note: This is the third blog post in a series on email clearinghouses (https://www.xmatters.com/alert-management/blog-email-has-failed-us?utm_campaign=70138000000ydLoAAI&utm_source=exex&utm_medium=article&utm_content=blog-post).   We’ve been talki…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now