How to print on a new line in JSP - java

I had a database name which consists of two tables. I retrieved the data from both tables in the same JSP page.
I got the correct output, but I also got the both table data in only one line. I want to print a newline between each table.
I tried printing \n and out.newline()but it did not work.

Print <br /> instead of \n

\n is a new line character in Java not in HTML.
If you want to have new line in HTML rendered by JSP, you could use <BR>. If you want to use it in scriptlet, use the following code:
<jsp:scriptlet>
out.println("<BR>");
</jsp:scriptlet>

Related

Cannot escape a quotation(") character when retriveing a string containg quotation inside a string from DB in jsp

I have saved quotation(") in a string using escape character i database. That is working ok. But when i am retrieving the value in a jsp field from database, the string is being ended at the first quotation it gets in the whole string. I am giving an example below:
Lets take a string that i have stored in database as -
" Hello David. This is a "customer"."
Now, i am somehow need to save the string back from databse into a hidden field in a jsp page like below-
<input type="hidden" name="string_from_database" id="string_from_database" value="<%=some varibale that holds the data from database%>">
issue is -
Part of the string is getting exposed (means it is being written on top of the page) which i do not want. In this case,the below phrase is written on the beginning of the jsp page, which i don't want.
customer".
kindly suggest on how to resolve this issue.
Using this function you could replace the quote marks with the html entity variant ". Here's a simple function for it. Hope it fits into your templating system, but should be easy to modify if not.
function escapeQuotes(str){
return str.replace(/"/g,'"');
}
Here's a working fiddle
Use Jstl rather than scriptlets for further Explanation
use EL - Expression Language (${variable}) to get the Value eg. ${welcome}
<c:out value="${some varibale that holds the data from database}"/>

Insert new line in struts2 messages.properties

I want to display the error message in two lines in Struts2
User Name is not valid
Password is not valid
and my property is:
username.password.errrorMsg: User Name is not valid \n Password is not valid.
I added \n but its displaying in single line.
Can you suggest to display in two lines?
If you use a message format then \n symbol add a new line character. If you want to display this message with actionerror or actionmessage tags you need to use <br> and let it not escape. For example
<s:actionmessage escape="false"/>
Because new line/breaking character depends on where do you use/show message it is better to use different messages for that.
invalid.userName = User Name is not valid
invalid.password = Password is not valid
In this way you can use them separately in case you want to show specific message and display them as you want.
If you displaying them in HTML/JSP using S2 <s:text> tag then <br/> should work. But several tags are escaping HTML so for example to use this kind of message in <s:property> with getText() you need to set escapeHTML attribute to false.
Probably < br/> (without the space between < and b :P), as the output format is html.

Data from database not same in java string

Here is my data when i view in SQL Developer tool
introduction
topic 1
topic end
and after i read it using a ResultSet,
ResultSet result = stmt.executeQuery();
result.getString("description")
and display in JSP page as
<bean:write name="data" property="description" />
but it will display like this
introduction topic 1 topic end
how can i keep the display same as in the SQL Developer?
Newlines aren't preserved in HTML. You need to either tell the browser it's preformatted:
<pre>
<bean:write name="data" property="description"/>
</pre>
Or replace the newlines with HTML line breaks. See this question for examples.
how can i keep the display same as in the SQL Developer?
The data presumably contains line breaks, e.g. "\r\n" or "\n". If you look at the source of your JSP, you'll probably see them there. However, HTML doesn't treat those as line breaks for display purposes - you'll need to either use the <br /> tag, or put each line in a separate paragraph, or something similar.
Basically, I don't think this is a database problem at all - I think it's an HTML problem. You can experiment with a static HTML file which you edit locally and display in your browser. Once you know the HTML you want to generate, then work on integrating it into your JSP.

out.println() does not work

I have homework which I have to use scriptlets in ,
I need to make new line in my jsp page usint out object
I tried to use
<%
out.println();
out.newLine();
%>
but both doesn't work !!! I treid to use
out.flush()
but it doesn't work!!
Perhaps out.println("<br>"); is what you're after. (Remember that the browser in which you're viewing the jsp-page in, interprets the output of your script as HTML, which basically ignores newline characters.)
You can look at the source of the page to see what the jsp-page actually generates.
If you really want to see the verbatim output of the jsp-script, you could do
out.println("<html><body><pre>");
// ...
out.println("</pre></body></html>");
#Alaa - out.newLine() does work. It just doesn't do what you are expecting it to do ... assuming that your JSP is generating an HTML page.
When you use out.newLine(), it adds a newline character to the content stream that you are generating. If you use view source on the page in your web browser you can see the newline character.
But a newline character in an HTML document typically does not result in a line break in the displayed page as rendered by a browser. To get the browser to render line break in the displayed page, you typically* need to output a <br /> element.
* - Actually, there are other ways to get the visual equivalent of a line break involving CSS, etcetera. And within a <pre>...</pre> a raw newline character does get rendered as a line break.
Remember the JSP code is outputting HTML. The HTML will then be rendered by the browser. A single blank line in HTML may not be shown as a blank line on the screen when the HTML is rendered.
You need to either examine the HTML source in the browser and look for the blank line. Or else try output more significant HTML to verify the JSP scriptlets are working like:
<%
out.println("<p>hello</p>");
%>

In a spring messages.properties, how to ensure line break of error message when using an error code as key?

In messages.properties:
error.code=This is error message.\nThis is next line of error message.
Now, when I set "errors.rejectValue" with this "error.code" for a form field, I cannot get the line break of '\n' to display on the jsp page when displaying the error message using the form:errors element.
Instead of '\n', using <br/> also does not work and gets displayed as is on the page.
In order to display a <br> as a line break, or for any other html tag in the error message body to take effect e.g. a <b>, simply turn html escaping off at tag level by adding htmlEscape="false" to your form:errors element.
The layout of distinct lines in an HTML page is not really something that a message bundle can deal with, it's just not suitable for the task. If you need multiple lines to be displayed, then realistically you're going to need multiple messages, with multiple entries in the properties file.
Perhaps you could cook up something which iterates over a sequence of properties, with something like this in your properties file:
error.code.1=This is error message.
error.code.2=This is next line of error message.
It then becomes the job of the JSP to render the sequence of messages. Not very elegant, though, but I can't think of a better solution right now :)
i had the same problem while loading the messages from the database, meaning that the '\n' (new line) sequence was escaped. so here is the sample solution, replace the "\\n" with "\n" :
String twoLinesMessage=((String)context.getMessage("error.code", null, locale)).replace("\\n","\n");
Extending the spring tag form:errors and adding conversion from \n to <br /> is also a non-beatiful solution, but one that will probably work.
Use "<br/>" in place of "\n" in your code.
For example:
return i + " x " + j + " = "+ (Integer.valueOf(i * j) + "<br/>"));
This returns the multiplication table in the web with line breaks.

Categories

Resources