aspose file tools*
The moose likes Servlets and the fly likes Posting a form with jQuery to servlet : Losing newlines? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Posting a form with jQuery to servlet : Losing newlines?" Watch "Posting a form with jQuery to servlet : Losing newlines?" New topic
Author

Posting a form with jQuery to servlet : Losing newlines?

olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Hi all,

There is a little problem that i do not fully understand...

In the beginning, i used to work on the standard post method with html...using no script...everything went well...Jsp-post -> servlet -> done

Now, when i try to post that same form to a servlet, using jQuery post method, everything goes fin...except that i lose my newlines.

i work with the jQuery serialize() function.

$('form#dataform').serialize()

All textarea's that contain newlines, are converted to the following :

textarea1=line1%0Aline2&textarea2=line1%0Aline2

why not %0D%0A?...i don't know, but i accept it...

BUT, when the data is posted, in the servlet i receive (using req.getParameter(textarea1)...)

textarea1 = line1line2
textarea2 = line1line2

why is the %0A not comming in?

So, in the end, all newlines are gone...

I googled but didn't find an answer that was clear enough...and solutions are not that good...
they are talking about replacing the "%0A" etc etc...

anybody?

thanks in advance!!
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

nobody else ever got this problem? or maybe my question is not clear enough?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

How have you determined that the line feeds are gone?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81


first at the client, i used a script "alert" , where i saw that the linefeeds (0D0A) were converted to only %0A...i even think that this could be browser dependend...
using the jquery .serialize() function.


but, when i read them out in the servlet (like i said, using req.getParameter...), even the %0A is gone!

How im sure? i display them in hex format...no doubt that they are gone! only the hexadecimal representation of the text is shown.
I can assume that this is a common problem, because google is full of it...but no good solution.
But, just because it is a common problem...there must be a standard work around for it...a solution...something...

or am i doing something terrible wrong, i don't know!
i am not "script" specialist, because i want to use it as less as possible, cause of browser dependencies...

for the moment, i "solved" the problem in script by replacing all "%0A" by "--BRAKE--" and in the backend, replacing "--BRAKE--" by "\r\n"
it works, but i don't want to use a crappy solution like this!

If you want, tommorow i can post the code i use, client, server, etc...





Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61429
    
  67

Post an SSCCE showing the issue rather than a lot of code that might not be relevant to the problem.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

shame on me, shame on me...

I made a simple project like you suggested, just using the basics, and it worked!!

Original problem is, that i use a customized framework, and it takes care of everything. Deep inside the code i found out that all the 10's were replaced by ''.

So everything is ok now...
Actually, the problem raised because jQuery transforms the 0D0A to only 0A...
And a normal html post sends 0D0A...

thanks for the reply!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Posting a form with jQuery to servlet : Losing newlines?