out.println() does not work - java

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>");
%>

Related

How can I print string as-is in JSP? (The string itself looks like JSP code)

I just want to print the following string EXACTLY as-is in JSP (it's a tag for an email template, which will later be replaced)
<%= escapeUrl(message.delivery.mailParameters.subject) %>
I have tried
<% out.println("<%= escapeUrl(message.delivery.mailParameters.subject) %>"); %>
But this gives me a "String literal is not properly closed by a double-quote" error which is weird because there is no open double-quote in the code. Do I need to do anything (like escape a character?)
The following should work, tested in tomcat server. Though it will display error in eclipse IDE, will compile properly and render output
<%="<%=escapeUrl(message.delivery.mailParameters.subject)%\>"%>
The construct you need is called a "static literal" -- you want to bypass the jsp parsing and output the jsp tag directly. The syntax is as follows:
<\%= escapeUrl(message.delivery.mailParameters.subject) %\>
ie, add a backslash between the angle bracket and the percent on each end!
As documented under "comments" here: http://www.tutorialspoint.com/jsp/jsp_syntax.htm

Remove newlines and whitespace from jsp

I need to store the html retrieved from a <jsp:include> in a javascript variable. So I will have something like this
<script>
var html = '<jsp:include page="...">';
</script>
The problem is the jsp file has lots of whitespace and newlines which makes the javascript invalid! I tried using the trimDirectiveWhitespaces directive as suggested here, but that does not remove newlines.
How can I remove newlines as well from html so it can be a valid javascript string?
Or, another solution is welcome as well.
EDIT:
The snippet should eventually look like this (but with many more options):
<script>
var html = '<label class="someClass">Label</label><select><option value="val1">Value</option></select>';
</script>

Delaying Output of JSP Custom Tag?

I want to delay the output of a custom tag.
The reason being: I want to add a tag to the head of the document that will compile a list of styles and scripts to include in the page. The subsequent tags in the page would add to the list of elements and the list would be printed once the body of the page has been generated.
Is this possible or is there a better way to do it. (I don't want to have to know what links are going to be added during the page compilation.)
Thanks
With JSPContext pushBody() and popBody() you can get some control over the order of output.
<%
Writer body = new StringWriter();
out = pageContext.pushBody(body);
// following code will write to 'body' and not to client
%>
...
<%
out = pageContext.popBody();
// normal output again
%>
...
<% // write the captured output %>
<%= body %>
Even though it works it might be better to work around as it is very confusing.
I have asked this question on a variety of sites and didn't get the answer I was looking for (although some were creative, Thanks!) so I ended up doing quite a bit of research on my own for the "best" way to do this. This is what I came up with:
I created three custom tags so my html would look like the following:
<html:page>
<html:head>
...
</html:head>
<html:body>
...
</html:body>
</html:page>
The page tag creates a variable in the REQUEST_SCOPE called "headercontents" in the doStartTag() method that can compile a list of output that I want to place in the head of the document.
The head element simply adds all of its body contents to this variable.
The body element does nothing as of now, just has a tag file with a simple .
Any element in the body can now use the "headercontents" variable to post information to the head of the html page. (Thus linking any stylesheets or scripts that it needs in the head rather than in the body).
Then finally in the page tag's doEndTag() method it prints the contents of the "headercontents" variable then prints the contents of itself (the contents of the html:body tag).
The result is that the document can load and rearrange itself as needed. This is still a rudimentary version but I'll perfect it and post the source code sometime in the future.

Character is corrupted when use request.getParameter() in java

I have a web page where I do a search based on the text given in a text box. This text can be in any language like japanese, chinese etc (or any mbcs character).
Now when i enter a text in japanese (or any other mbcs character), the result populates the screen (form) with some wierd characters.
For Example: testテスト will turn into testãã¹ã.
When i see the post parameters in Firebug (debugging tool) i can see that the search string goes as testテスト however when i put debug statements in my code, i can see that request.getParameter("searchString") is not able to identify the japanese characters and turn them into some wierd chars.
My JSP header already has <%# page contentType="text/html; charset=UTF-8"
I have also tried putting pageEncoding="UTF-8" in this but it didn't help.
I tried setting character encoding like request.setCharacterEncoding("UTF-8") also just before doing request.getParameter but that too didn't work for me.
After going through a few forums and blogs i also tried setting useBodyEncodingForURI=true in the <Connector> of tomcat config but that also did not help me.
Can anybody suggest me something to resolve this issue?
set the following encoding in every servlet/ action
response.setContentType("UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
also set following in first servlet/action
for japanese
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
session.setAttribute(Globals.LOCALE_KEY, new Locale("jp", "ja_JP"));

Handling Spanish characters in Java/JSP

I have a small webapp which handles a lot of Spanish text.
At one point in the code, a JSP page responds with a Json String containing some of this text. If I print the String to the Console, it looks like jibberish. But if I examine the header/content of the response in Chrome Developer Tools, it looks correct. It is transferred in the correct encoding. This part of the webapp functions as expected.
At another point in the code, a different JSP page responds with HTML. Some of this HTML contains more of the Spanish text. This time, the text is transferred (and displayed) as jibberish.
What are potential reasons that this could be happening? Both times, I'm just printing the text using out.print. Why does it work at one point, but not in other?
Examples:
// In a file who's only output is the json string
String jsonString = ...
System.err.println(jsonString); // prints jibberish
out.println(jsonString); // looks correct when the response is viewed in Chrome Developer tools, and looks correct in a browser
...
// In a file who's output is a complete html page
String spanishText = ...
out.println("<label>" + spanishText + "</label>"); // looks like jibberish when the response is viewed in Chrome developer tools, and shows up as jibberish in a browser
You need to set the encoding which the JSP/Servlet response should use to print the characters and instruct the webbrowser to use the same encoding.
This can be done by putting this in top of your JSP:
<%# page pageEncoding="UTF-8" %>
Or if you're actually doing this in a Servlet:
response.setCharacterEncoding("UTF-8");
The "jibberish" when using System.err is a different problem. You need to set the encoding of the console/logfile which is been used to print this information to. If it's for example Eclipse, then you can set it by Window > Preferences > General > Workspace > Text File Encoding.
See also:
Unicode - How to get the characters right? - Fixing JSP/Servlet response
Unicode - How to get the characters right? - Fixing development environment

Categories

Resources