Monday, October 8, 2012
Today I am going to post a program that will be able to show all the headers associated with a servlet request; Here in the code request object is used to get an enumerations of headers associated with the request of the client. Then with a loop one by one all the headers are extracted and their corresponding values are available using the getHeader(String headername) method. All these headers and their values are sent to the client as a dynamically generated html. Also note that the values will differ according to the browser which has been used to send the request.
Here is the code for you -->
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/header.html")
public class HeaderViewer extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException{
Enumeration headers=req.getHeaderNames(); /*getting enumerations of all headers */
PrintWriter out=res.getWriter(); //getting writer
out.write("<html><head><title>HeaderViewer</title></head>");
out.write("<body><h1><center>Headers associated with your request</center></h1>");
while(headers.hasMoreElements()){
String header=(String)headers.nextElement(); /*extracting header */
out.write("</br><b>"+header+" : </b>"+req.getHeader(header)); /* sending header name and its value to client */
}
out.write("</body></html>");
out.close();
}
}
Screenshot of output as seen on Chrome in Windows7 |
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
@WebServlet("/header.html")
public class HeaderViewer extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException{
Enumeration headers=req.getHeaderNames(); /*getting enumerations of all headers */
PrintWriter out=res.getWriter(); //getting writer
out.write("<html><head><title>HeaderViewer</title></head>");
out.write("<body><h1><center>Headers associated with your request</center></h1>");
while(headers.hasMoreElements()){
String header=(String)headers.nextElement(); /*extracting header */
out.write("</br><b>"+header+" : </b>"+req.getHeader(header)); /* sending header name and its value to client */
}
out.write("</body></html>");
out.close();
}
}
Labels:J2EE
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
0 comments:
Post a Comment