Click here to watch in Youtube : https://www.youtube.com/watch?v=hHcvTVFx9S0
Click the below Image to Enlarge
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DisplayHeadersServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void init() throws ServletException { System.out.println("\n*******************************************\n"); System.out.println("HelloWorld servlet init method has been called"); System.out.println("\n*******************************************\n"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "HTTP Header Request Example"; 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" + "<table width=\"100%\" border=\"1\" align=\"center\">\n" + "<tr bgcolor=\"#949494\">\n" + "<th>Header Name</th><th>Header Value(s)</th>\n" + "</tr>\n"); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = (String) headerNames.nextElement(); out.print("<tr><td>" + headerName + "</td>\n"); String headerValue = request.getHeader(headerName); out.println("<td> " + headerValue + "</td></tr>\n"); } out.println("</table>\n</body></html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void destroy() { // do nothing. } }
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" metadata-complete="true" version="3.0"> <display-name>ServletForm GET</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>readAllHeaders</servlet-name> <servlet-class>DisplayHeadersServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>readAllHeaders</servlet-name> <url-pattern>/readAllHeaders</url-pattern> </servlet-mapping> </web-app>
index.html
<!DOCTYPE HTML><html lang="en"><head> <meta charset="UTF-8"> <title>Order Pizza</title> </head> <body> <p> <h3>Order Pizza</H3> <p></p> <ul> <li><a href="pizza.html">Order Pizza</a></li> </ul> </body></html>
<html> <head> <title>Retrieving All Parameters</title> </head> <body> <h4>Order your Pizza Here</h4> <form action="readAllHeaders" method="POST"> <b>Name</b> <input type="text" name="name"><br><br> <b>Select the Crust:</b> <select name = "crust"> <option value="pan">Pan</option> <option value="thin">Thin Crust</option> <option value="deep">Deep Crust</option> <option value="cheese">Cheese Burst</option> </select> <br><br> <b>Toppings: </b><br> <input type="checkbox" name="toppings" value="peas">Peas<br> <input type="checkbox" name="toppings" value="paneer">Paneer<br> <input type="checkbox" name="toppings" value="redpeppers">Red Peppers<br> <input type="checkbox" name="toppings" value="pineapple">Pineapple<br> <input type="checkbox" name="toppings" value="onion">Onion<br> <input type="checkbox" name="toppings" value="tomato">Tomato<br><br> <b>Select 1 FREE Appetizer</b> <input type="radio" name="appetizer" value="Garlic Bread">Garlic Bread <input type="radio" name="appetizer" value="Cheese Garlic Bread">Cheese Garlic Bread <input type="radio" name="appetizer" value="Veg Soup">Veg Soup <input type="radio" name="appetizer" value="Veg Sandwich">Veg Sandwich<br><br> <b>Address</b><br> <textarea name="address" rows=3 cols=40></textarea><br><br> <b>Credit Card:</b><br> <input type="radio" name="cardType" value="Visa">Visa <input type="radio" name="cardType" value="MasterCard">MasterCard <input type="radio" name="cardType" value="Amex">American Express <br><br> <b>Credit Card Number:</b> <input type="password" name="cardNum"> <b>Repeat Credit Card Number: </b><input type="password" name="cardNum"><br><br> <input type="submit" name="submit" value="Order Pizza"> </form> </body> </html>
Environment Used
JDK version :1.6.0_30
Tomcat version : 7.0.50
To Download DisplayAllHeadersApp Project Click the below link
https://sites.google.com/site/javaee4321/servlets/DisplayAllHeadersApp.zip?attredirects=0&d=1
See also:
Great post! I found your insights really valuable and informative. Your explanation was clear and helped me understand the topic better.
ReplyDeleteBest Advanced Java Online Training
Best Java Training in Hyderabad
Advanced Java Course
Advanced Java Online Course