Manage HTTP headers with Java Servlets: Quick Notes

With Java Servlet API, it is quite easy to send and receive HTTP headers. Below code examples shows the details.

In Java Servlets API, both HttpServletRequest and HttpServletResponse interfaces (in javax.servlet.http package) provide methods to programatically manipulate HTTP headers. There are a number of standard HTTP headers exchanged between a web server and a client (eg: a browser). "Content-Type" is a commonly used header (which is used to specify MIME type) in Servlets. In this article we are discussing how headers are read/written with Servlet classes.

Reading Headers

A servlet can read HTTP headers sent by a client request using HttpServletRequest interface. This interface has two methods for this.
String getHeader(String headerName);
int getintHeader(String headerName);

Both these methods are similar except getIntHeader() method is used to return value of headers with int type values. Below code shows how value of "Content-type" header is read from the user request. (HttpServlet.doGet() method is used in the example).
import javax.servlet.http.*;
import java.io.*;

public class MyServlet extends HttpServlet {
    public void doGet(re, res) throws IOException{
        String contentType = request.getHeader("Content-type");
        //....
    }
}

Creating/Writing Headers

A servlet can create a header and send back to the client using HttpServletResponse interface; using the following setter methods.
void setHeader(String headerName, String headerValue);
int setintHeader(String headerName, int headerValue);

Below code shows how a new header named "My_Header" is created and set on response.
import javax.servlet.http.*;
import java.io.*;

public class MyServlet extends HttpServlet {
    public void doGet(re, res) throws IOException{
        response.setHeader("My_Header", "new Header value");
        //....
    }
}

Now the client (generally the browser) will receive this new header.

COMMENTS

BLOGGER: 4
Loading...

Read More...

Name

About,2,Adsense,3,Ant,1,Apache,3,Axis,3,Blogger,1,Books,1,CentOS,2,Chrome,2,CSS,2,Database,3,Earn Online,3,Eclipse,10,Facebook,1,Firefox,10,Gmail,4,GNU/Linux,9,Google,26,GWT,8,Hardware,2,IE,5,Interesting,15,Internet,14,Java,49,Javascript,7,JBoss,1,Jenkins,1,Log4j,2,Me,6,Microsoft,2,Miscellaneous,1,News,11,Opinion,10,OSGi,1,PHP,1,Productivity,3,Programming,36,Puzzle,3,Security,4,Software,41,Sports,9,Spring,2,Story,6,Subversion,3,TDD,4,Tech,2,Tips,1,Tomcat,6,Tutorial,13,Ubuntu,4,Web application,14,Web Design,2,Web services,3,Windows,10,Yahoo,1,Zip,2,
ltr
item
Digizol: Manage HTTP headers with Java Servlets: Quick Notes
Manage HTTP headers with Java Servlets: Quick Notes
With Java Servlet API, it is quite easy to send and receive HTTP headers. Below code examples shows the details.
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7MD01KU_Xq4WZd0IKPSxUnji3clxDKxbuuOMOD6uvTOsQDDCLHbBroTdwT2F_WNjJycgc2isB1Hxz4Unxf_9zveHsN5bqBTGUV4yVG-7vXFsdhfyO-C0cn2YdUZVEqFGNm5dqQA/s1600/Manage+HTTP+headers+with+Java+Servlets+Quick+Notes+www.digizol.com.jpg
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7MD01KU_Xq4WZd0IKPSxUnji3clxDKxbuuOMOD6uvTOsQDDCLHbBroTdwT2F_WNjJycgc2isB1Hxz4Unxf_9zveHsN5bqBTGUV4yVG-7vXFsdhfyO-C0cn2YdUZVEqFGNm5dqQA/s72-c/Manage+HTTP+headers+with+Java+Servlets+Quick+Notes+www.digizol.com.jpg
Digizol
https://www.digizol.com/2008/10/http-headers-java-servlets-notes.html
https://www.digizol.com/
https://www.digizol.com/
https://www.digizol.com/2008/10/http-headers-java-servlets-notes.html
true
7440473
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy