Servlets Tutorial


As examined in the past section, when a Web server reacts to a HTTP ask for, the reaction ordinarily comprises of a status line, some reaction headers, a clear line, and the record. A run of the mill reaction resembles this −

HTTP/1.1 200 OK 

Content-Type: content/html 

Header2: ... 

... 

HeaderN: ... 

(Clear Line) 

<!doctype ...> 

<html> 

<head>...</head> 

<body> 

... 

</body> 

</html> 

The status line comprises of the HTTP rendition (HTTP/1.1 in the model), a status code (200 in the precedent), and an extremely short message relating to the status code (OK in the model).

Following is a synopsis of the most valuable HTTP 1.1 reaction headers which return to the program from web server side and you would utilize them in all respects as often as possible in web programming −

Sr.No. Header & Description
1

Allow

This header determines the demand techniques (GET, POST, and so on.) that the server supports.

2

Cache-Control

This header indicates the conditions in which the reaction report can securely be reserved. It can have values public, private or no-cache and so forth. Open methods report is cacheable, Private methods archive is for a solitary client and must be put away in private (non-shared) stores and nocache implies record ought to never be cached.

3

Connection

This header trains the program whether to utilize persevering in HTTP associations or not. An estimation of close teaches the program not to utilize tenacious HTTP associations and keepalive implies utilizing diligent connections.

4

Content-Disposition

This header gives you a chance to ask for that the program request that the client spare the reaction to plate in a record of the given name.

5

Content-Encoding

This header indicates the manner by which the page was encoded amid transmission.

6

Content-Language

This header implies the language in which the report is composed. For instance en, en-us, ru, etc

7

Content-Length

This header shows the quantity of bytes in the reaction. This data is required just if the program is utilizing a steady (keep-alive) HTTP connection.

8

Content-Type

This header gives the MIME (Multipurpose Internet Mail Extension) sort of the reaction document.

9

Expires

This header determines the time at which the substance ought to be considered obsolete and in this way never again be cached.

10

Last-Modified

This header demonstrates when the archive was last changed. The customer would then be able to reserve the record and supply a date by a If-Modified-Since ask for header in later requests.

11

Location

This header ought to be incorporated with all reactions that have a status code during the 300s. This advises the program of the archive address. The program consequently reconnects to this area and recovers the new document.

12

Refresh

This header indicates how soon the program ought to request a refreshed page. You can indicate time in number of seconds after which a page would be refreshed.

13

Retry-After

This header can be utilized related to a 503 (Service Unavailable) reaction to tell the customer how soon it can rehash its request.

14

Set-Cookie

This header indicates a treat related with the page.

Methods to Set HTTP Response Header

There are following strategies which can be utilized to set HTTP reaction header in your servlet program. These techniques are accessible with HttpServletResponse object.

Sr.No. Method & Description
1

String encodeRedirectURL(String url)

Encodes the predetermined URL for use in the sendRedirect technique or, if encoding isn't required, restores the URL unchanged.

2

String encodeURL(String url)

Encodes the predefined URL by incorporating the session ID in it, or, if encoding isn't required, restores the URL unchanged.

3

boolean containsHeader(String name)

Returns a Boolean demonstrating whether the named reaction header has just been set.

4

boolean isCommitted()

Returns a Boolean demonstrating if the reaction has been committed.

5

void addCookie(Cookie cookie)

Adds the predetermined treat to the response.

6

void addDateHeader(String name, long date)

Adds a reaction header with the given name and date-value.

7

void addHeader(String name, String value)

Adds a reaction header with the given name and value.

8

void addIntHeader(String name, int value)

Adds a reaction header with the given name and number value.

9

void flushBuffer()

Forces any substance in the cradle to be composed to the client.

10

void reset()

Clears any information that exists in the cushion just as the status code and headers.

11

void resetBuffer()

Clears the substance of the fundamental support in the reaction without clearing headers or status code.

12

void sendError(int sc)

Sends a blunder reaction to the customer utilizing the predetermined status code and clearing the buffer.

13

void sendError(int sc, String msg)

Sends a blunder reaction to the customer utilizing the predetermined status.

14

void sendRedirect(String location)

Sends an impermanent divert reaction to the customer utilizing the predetermined divert area URL.

15

void setBufferSize(int measure)

Sets the favored support estimate for the body of the response.

16

void setCharacterEncoding(String charset)

Sets the character encoding (MIME charset) of the reaction being sent to the customer, for instance, to UTF-8.

17

void setContentLength(int len)

Sets the length of the substance body in the reaction In HTTP servlets, this strategy sets the HTTP Content-Length header.

18

void setContentType(String type)

Sets the substance sort of the reaction being sent to the customer, if the reaction has not been submitted yet.

19

void setDateHeader(String name, long date)

Sets a reaction header with the given name and date-esteem.

20

void setHeader(String name, String esteem)

Sets a reaction header with the given name and value.

21

void setIntHeader(String name, int esteem)

Sets a reaction header with the given name and whole number value

22

void setLocale(Locale loc)

Sets the region of the reaction, if the reaction has not been submitted yet.

23

void setStatus(int sc)

Sets the status code for this response

HTTP Header Response Example

You as of now have seen setContentType() technique working in past precedents and following model would likewise utilize same strategy, also we would utilize setIntHeader() technique to set Refresh header.

//Import required java libraries 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import java.util.*; 

//Extend HttpServlet class 

public class Refresh extends HttpServlet { 

//Method to deal with GET strategy request. 

public void doGet(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

//Set invigorate, autoload time as 5 seconds 

response.setIntHeader("Refresh", 5); 

//Set reaction content type 

response.setContentType("text/html"); 

//Get present time 

Calendar schedule = 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 open \"-/w3c//dtd html 4.0 " + "transitional//en\">\n"; 

out.println(docType + 

"<html>\n" + 

"<head><title>" + title + "</title></head>\n"+ 

"<body bgcolor = \"#f0f0f0\">\n" + 

"<h1 adjust = \"center\">" + title + "</h1>\n" + 

"<p>Current Time is: " + CT + "</p>\n" 

); 

} 

//Method to deal with POST technique request. 

public void doPost(HttpServletRequest request, HttpServletResponse response) 

throws ServletException, IOException { 

doGet(request, response); 

} 

}

Now calling the above servlet would show current framework time after like clockwork as pursues. Simply run the servlet and hold on to see the outcome −

Auto Refresh Header Setting

Current Time is: 9:44:50 PM







© Javacodegeeks 2018 -
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3