setIntHeader method of servlet header

Hi,

I am running below servlet example

http://www.tutorialspoint.com/servlets/servlets-server-response.htm

my code looks like
package com.tp.first;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
      private static final long serialVersionUID = 1L;
      private String message;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
   
    public void init() throws ServletException
    {
        // Do required initialization
        message = "Hello World";
    }

    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
    throws ServletException, IOException
{
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 5);

// Set response content type
response.setContentType("text/html");

// Get current time
Calendar calendar = new GregorianCalendar();
String am_pm;
int hour = calendar.get(Calendar.HOUR);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";

String CT = hour+":"+ minute +":"+ second +" "+ am_pm;

PrintWriter out = response.getWriter();
String title = "Auto Refresh Header Setting";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n"+
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<p>Current Time is: " + CT + "</p>\n");
}
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
    // Method to handle POST method request.
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
        throws ServletException, IOException {
       doGet(request, response);
    }

}

I have question on below line
// Set refresh, autoload time as 5 seconds
response.setIntHeader("Refresh", 5);

Is Refresh is a keyword. How does the browser know to refresh every 5 seconds.

void setIntHeader(String name, int value)
Sets a response header with the given name and integer value.

Can given name be anything or it needs to be a specific name. If it is specific where can i get all those specific given names.

How AM and PM are printed
if(calendar.get(Calendar.AM_PM) == 0)
am_pm = "AM";
else
am_pm = "PM";
How _ is recognized in AM_PM.


The format of the HTTP request and HTTP response messages are similar and will have following structure:

An initial status line + CRLF ( Carriage Return + Line Feed ie. New Line )

Zero or more header lines + CRLF

A blank line ie. a CRLF

An optioanl message body like file, query data or query output.

For example, a server response header looks as follows:

HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (Blank Line)
<!doctype ...>
<html>
<head>...</head>
<body>
...
</body>
</html>

what author is trying to mention in above lines.
Please advise

 Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Ken ButtersConnect With a Mentor Commented:
Is Refresh is a keyword. How does the browser know to refresh every 5 seconds.

Yes Refresh is a keyword in the sense that brower's are written to understand and act upon that specific keyword.

The URL you had "http://www.tutorialspoint.com/servlets/servlets-server-response.htm" contains a list of commonly used values.

Look at the table in the middle of the referenced page.  the table has headers of "HEADER / DESCRIPTION"  you will see REFRESH listed as one of the values in that table.

Can given name be anything or it needs to be a specific name. If it is specific where can i get all those specific given names.

What you are asking about here, is the setIntHeader method of the HttpServletResponse.

So if you look at the java docs for HttpServletResponse and then review method setIntHeader... you will see that it takes as parameters a "String" and an "int".  So it will set the header with any string and any int.

However... that doesn't mean that if you set any string and any int, that browers will necessarily know what to do with it.  You will generally only set headers that mean something to the browser, because the browser is generally the consumer of the response.

How _ is recognized in AM_PM.

AM_PM is a method of the Calendar Class.  It stores a value of 0 if the time is "AM" it stores a value of 1 if the time is "PM".

what author is trying to mention in above lines.
The author is describing in detail what HTTP Header and HTTP Responses look like.

He is telling you that the structure of the request and response consist of the following items in the following order.

An initial status line + CRLF ( Carriage Return + Line Feed ie. New Line )
Zero or more header lines + CRLF
A blank line ie. a CRLF
An optioanl message body like file, query data or query output.

After listing out what the items are in general he then goes on to give you a specific example:
HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (Blank Line)
<!doctype ...>
<html>
<head>...</head>
<body>
...
</body>
</html>

Open in new window


In the above example...here is the first part of the response structure, the status-line:
HTTP/1.1 200 OK
0
 
gudii9Author Commented:
It stores a value of 0 if the time is "AM" it stores a value of 1 if the time is "PM".

Where they mentioned about 0,1 in the javadoc. I could not fine it.

I referred javadoc for calendar AM_PM method
http://docs.oracle.com/javase/6/docs/api//java/util/Calendar.html#AM_PM


AM_PM

public static final int AM_PM

    Field number for get and set indicating whether the HOUR is before or after noon. E.g., at 10:04:15.250 PM the AM_PM is PM.

    See Also:
        AM, PM, HOUR, Constant Field Values


Please advise
0
 
Ken ButtersCommented:
Looks like that information is not in explicit in javadoc.

This is as close as it gets under definition of the constants:
static int      AM_PM
Field number for get and set indicating whether the HOUR is before or after noon.

It tells you it is an "int" and it tells you that it indicates whether the hour is before or after noon.
0
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
If you click the "Constant Field Values" link in that Javadoc you will see that there is a constant in the Calendar class for AM whose value is 0 and one for PM whose value is 1
0
 
Ken ButtersCommented:
True... the docs do tell  you that AM=0 and PM=1... but what I was getting at is that it never seems to clearly state in the javadocs that "AM_PM" will will hold a value of zero or one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.