Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace new line character by <br>

Status
Not open for further replies.

TyzA

Programmer
Jan 7, 2002
86
BE
Hi,

Is it possible to replace the new line character with the HTML tag <br>.

For logging I'm printing the stackTrace of an exception to a log file. I'm also printing this to the browser.
In the log file the stackTrace is displayed nicely, but in the browser it is displayed as one string.

I'm doing it like this:
( x = exception )
OutputStream buf = new ByteArrayOutputStream();
PrintStream p = new PrintStream(buf);
x.printStackTrace(p);
strErrorDetails = buf.toString();
p.close();

In the log file it's displayed like this:
java.lang.NullPointerException
at be.belgacom.mobile.display.CampaignContacts.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.sssw.srv.busobj.AgoFilterChain.doServlet(AgoFilterChain.java:396)
at com.sssw.srv.busobj.AgoFilterChain.doFilter(AgoFilterChain.java:182)
at com.sssw.srv.resources.AgWarResource.service(AgWarResource.java:629)
at com.sssw.srv.resources.AgWarURLResource.perform(AgWarURLResource.java:115)
at com.sssw.srv.http.httpd.perform(httpd.java:5632)
at com.sssw.srv.http.Client.processRequest(Client.java:902)
at com.sssw.srv.http.Client.loop(Client.java:1268)
at com.sssw.srv.http.Client.runConnection(Client.java:1484)
at com.sssw.srv.http.Client.run(Client.java:1432)
at java.lang.Thread.run(Thread.java:479)



In the browser like this:
java.lang.NullPointerException at be.belgacom.mobile.display.CampaignContacts.doGet(Unknown Source) at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853) at com.sssw.srv.busobj.AgoFilterChain.doServlet(AgoFilterChain.java:396) at com.sssw.srv.busobj.AgoFilterChain.doFilter(AgoFilterChain.java:182) at com.sssw.srv.resources.AgWarResource.service(AgWarResource.java:629) at com.sssw.srv.resources.AgWarURLResource.perform(AgWarURLResource.java:115) at com.sssw.srv.http.httpd.perform(httpd.java:5632) at com.sssw.srv.http.Client.processRequest(Client.java:902) at com.sssw.srv.http.Client.loop(Client.java:1268) at com.sssw.srv.http.Client.runConnection(Client.java:1484) at com.sssw.srv.http.Client.run(Client.java:1432) at java.lang.Thread.run(Thread.java:479)


Is it possible to display this on the browser the same as in the log file.

Thanks for any assistance.

Tijs
 
yeh,
in your servlet instantiate a printwriter object like this:

PrintWriter out = res.getWriter();

res is my HttpServletResponse instance, you might have a different name for it, remember to use yours!!

insert this line wherever you want the <br> tag in your html file in the servlet:

out.println(&quot;<BR>&quot;);
 
Hi,

I know this,

But what i want to do is to replace the new line character in the StackTrace by &quot;<br>&quot;.

 
try something along the lines of:

String replaceAll(String regex, String replacement)

Replaces each substring of this string that matches the given regular expression with the given replacement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top