hari
hari

Reputation: 1377

Displaying double-quotes from text box in JSP

I have written a small JSP page that contains a text box and a button. What I am attempting to do is to display whatever I have written in the text box (which is a String) once i click the button. It has worked for all scenarios but did not work for the one scenario where I put the string within double quotes (" "). Is there any way I can make that work?

For example: If I write abcd within the text box, it works fine. But if I write "abcd" (i.e with double quotes), then the text within the double quotes don't display.

I understand why that is happening (example: If we write abcd in the text box, it is taken as "abcd" and if I write "abcd" within the text box, it is taken as ""abcd""), but couldn't find a solution for that.

Or is there a solution for that at all?

Upvotes: 2

Views: 3835

Answers (3)

user1237385
user1237385

Reputation:

Hey try this it will work fine without any other libraries:

for example:

In jsp file,

<form action="Login">
        Enter :<input type="text" name="txtStr"></input>
        <input type="submit" name="btnGo" value="Go"></input>
</form>

In Servlet,

String str = request.getParameter("txtStr");    
System.out.println("helo : " + str);

request.setAttribute("str", str);
request.getRequestDispatcher("/dis.jsp").forward(request, response);

In dis.jsp;

<body>
    ${str}
</body>

Upvotes: 1

Shashank Kadne
Shashank Kadne

Reputation: 8101

You can use StringEscapeUtils to escape HTML..

Use escapeHtml(String str) function to escape HTML characters..

StringEscapeUtils.escapeHtml(str)

Upvotes: 0

Bozho
Bozho

Reputation: 597116

Yes - escape the quotes. You should replace them with &quot;

A ready-to-use solution that will cover some more cases is commons-lang StringEscapeUtils.escapeHtml(str)

Upvotes: 7

Related Questions