save control characters in a session object

A

Andreas Bauer

Hi,

I've got a html form in which you can enter a text.
But now I don't know how to preserve characters like
carriage return and so forth.
Any hints?
Thanks in advance

Andi
 
C

Collin VanDyck

Andreas Bauer said:
Hi,

I've got a html form in which you can enter a text.
But now I don't know how to preserve characters like
carriage return and so forth.
Any hints?
Thanks in advance

Andi

Are you trying to process the control characters from the form, or is your
question how to save the control characters into a session object?

If you are dealing with a textarea, the text you receive as a result of the
POST will contain characters such as your line breaks, which are represented
as "\n" in a Java String object.

If your question is how to store them into your session object, then can you
please give more information about your environment, what code you have
tried, what is working, what is not working, etc?

thanks-
 
A

Andreas Bauer

If your question is how to store them into your session object, then can
you please give more information about your environment, what code you
have tried, what is working, what is not working, etc?

I take the text from a textarea and store in a HashMap(don't know
if thats the best way)and it works:
.....
String ueberschrift = (String) req.getParameter("ueberschrift");
String untUeber = (String) req.getParameter("unterueberschrift");
String text = (String) req.getParameter("text");
setArticleText(ueberschrift, untUeber, text);
session.setAttribute("articleText",getArticleText());
res.sendRedirect(res.encodeURL("vorschau.jsp"));
.....

the method:
private synchronized void setArticleText(
String ueberschrift,
String untUeber,
String text)
{
hm.put("ueberschrift", ueberschrift);
hm.put("untUeber", untUeber);
hm.put("text", text);
}

I want to make a little preview before sending the information to the
database. The text should be shown the same way the user entered into
the form.
(jsp)
String ueberschrift = null, untUeber = null, text = null;
java.util.HashMap hm = (java.util.HashMap)
session.getAttribute("articleText");
ueberschrift = (String) hm.get("ueberschrift");
untUeber = (String) hm.get("untUeber");
text = (String) hm.get("text");

Not the best way I guess, but I'm not that experienced in
Servlet programming yet.
 
C

Collin VanDyck

I want to make a little preview before sending the information to the
database. The text should be shown the same way the user entered into
the form.
(jsp)
String ueberschrift = null, untUeber = null, text = null;
java.util.HashMap hm = (java.util.HashMap)
session.getAttribute("articleText");
ueberschrift = (String) hm.get("ueberschrift");
untUeber = (String) hm.get("untUeber");
text = (String) hm.get("text");

Not the best way I guess, but I'm not that experienced in
Servlet programming yet.

Ah, so in creating the preview for the user, your newlines are getting
"deleted" in the page? In other words, the text all runs together?

If in your JSP you are showing the fields like this:

<p>
field one: <%= ueberschrift %>
field two: <%= untUeber %>
....
</p>

Then the HTML page this produces will not care about any newlines in your
strings. One way to get around the newline problem would be to, when you
are displaying the strings, replace all "\n" characters with "<br/>". This
will create the newlines for you in HTML.

A better solution might be to wrap your formatted text with <pre>...</pre>
tags. This attempts to preserve formatting as much as possible when
displayed in the browser.

-Collin
 
A

Andreas Bauer

Then the HTML page this produces will not care about any newlines in your
strings. One way to get around the newline problem would be to, when you
are displaying the strings, replace all "\n" characters with "<br/>".
This will create the newlines for you in HTML.
A better solution might be to wrap your formatted text with <pre>...</pre>
tags. This attempts to preserve formatting as much as possible when
displayed in the browser.

Ok, you're right. Thanks for the hint. I will try this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top