-1

I would like to know what happens to the html codes written inside the servlet? What is the need of writing like this?

Sample code:

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{   
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Using GET Method to Read Form Data";
    out.println("<html>"<head><title>"Welcome"</title></head>\n" +
                "<body><p>Welcome to servlet</p></body></html>");
}
Vivek Singh
  • 2,047
  • 11
  • 24
Akhil Mathew
  • 1,571
  • 3
  • 34
  • 69
  • Basically it generates whatever Html you write in there (if you do it right). Personally, I make .jsp pages for my Html, it gives me a better overview of things. – Victoria S. Dec 04 '15 at 10:46
  • @VictoriaS. Is this similar to creating an html page , rather than fetching a html page that is already defined in server? – Akhil Mathew Dec 04 '15 at 10:52

2 Answers2

0

An HTML page is nothing else than plain text following HTML syntax.

Hence, anything you give as a response to a HTTP request, being plain text following HTML syntax (like your String does), IS an HTML page, provided you tell the caller what is the content type of the response :

response.setContentType("text/html");
Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

The HTML code written in the servlet goes to the Client through the Container (or through web server talking with the Container) which is responsible to send a response back to the Client (browser) that, in turn, will render the HTML to the user.

Here you will find a nice explanation of what is going on behind the scenes: How do servlets work? Instantiation, sessions, shared variables and multithreading

Community
  • 1
  • 1
jsfviky
  • 183
  • 1
  • 11