Solved

setIntHeader method of servlet header

Posted on 2014-02-23
5
836 Views
Last Modified: 2014-02-27
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
0
Comment
Question by:gudii9
  • 3
5 Comments
 
LVL 19

Accepted Solution

by:
Ken Butters earned 350 total points
Comment Utility
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
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 150 total points
Comment Utility
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
 
LVL 19

Expert Comment

by:Ken Butters
Comment Utility
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now