Two Laptop Bag*
The moose likes Servlets and the fly likes HTML Tags Display in Servlet Java File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTML Tags Display in Servlet Java File" Watch "HTML Tags Display in Servlet Java File" New topic
Author

HTML Tags Display in Servlet Java File

John G Wright
Greenhorn

Joined: Oct 25, 2012
Posts: 18
I am sending the request from the client (browser) to the server. In Servlet.java file, I am trying to embed html tags in .java files in println,
ie
System.out.println("<a href="login.html">Login</a><br>")

The above println statment is getting displayed as a string in Mozilla(ver 17.1) whereas I am getting a proper hyperlink in IE( ver 6 ).

What needs to be done to get the same in Mozilla ?

Thanks
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
are you sure it is "System.out.println"?


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Rob Crowther
author
Ranch Hand

Joined: Nov 06, 2012
Posts: 295
    
    5

Is that all your servlet is outputting, or is it also outputting appropriate <html>, <head> and <body> elements?

Is the content type of the servlet output text/html?


Read my books: Hello! HTML5 & CSS3 | HTML5 in Action | Read my blog
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

System.out will not send the string to the response.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
John G Wright
Greenhorn

Joined: Oct 25, 2012
Posts: 18
Thanks to all for the support. My clarifications are as follows :

System.out will not send the string to the response.

are you sure it is "System.out.println"?


It is out.println where out is the object to PrintWriter

Is the content type of the servlet output text/html?


I am getting the values from the input type 'text' from html page and updating in the database. The program has got no errors and can update the rows in the db table. The program works correctly in IE and not in Mozilla where out.println("<a href="login.html">Login</a><br>") printing as a string in the html page instead of giving me the hyperlink. The same not happening in IE where I am getting the proper hyperlink.


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

You did not answer the question.
John G Wright
Greenhorn

Joined: Oct 25, 2012
Posts: 18
I am not able to understand the question clearly.

Let me keep it very simple.

out.println("<a href="login.html">Login</a><br>") is displayed as

<a href="login.html">Login</a><br> in Mozilla 17.1

whereas I open the same html page with IE 6, I am getting the proper hyperlink of Login which when I click going to login.html page.

Whats the correct way of embedding html tags in .java file ? Am I doing it right ?r Do I need to be browser specific in embedding html tags ?

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

You need to find out what the content type of the request is. Before knowing that, any other speculation is useless.

Open the debugger in the browser (Chrome's is really good) and look at the network activity. Find the response's content type.

If you are going to be working on the web, these are essential tools you need to know how to use.
John G Wright
Greenhorn

Joined: Oct 25, 2012
Posts: 18
Please excuse me for lack of knowledge as I am a beginner and for a very very late reply. Thanks for the introduction to Dev Tools in the Chrome Browser.

I figured out the error and it is so simple. By default, Eclipse's internal browser content type set to text/html. So if I try to embed the HTML tags , I am getting the proper HTML output.

But in case of Chrome, the default set content type is plain/text and so if I am not mentioning text/html explicitly as set content type and trying to embed HTML tags say for example out.println("hr") [out being object to printwriter] it is printing as a string instead of HTML page. So if I want to open the link in chrome, I need to set to text/html if I embed HTML tags in servlets.


Thanks
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: HTML Tags Display in Servlet Java File
 
Similar Threads
Java Web Start
Japanese Text in javascript alert
raw HTML in Mozilla
Session Doubt?
Online Tutorial