by Dinesh Thakur Category: introduction to Servlet

Both are send input values to target web resource program like servlet program along with the request. The difference are

Http Request Headers

Request Parameters

Holds the browser supplied input values.

Holds the visitor (end user) supplied input values. (from data)

Headers names are fixed but the values are browser specified.

Parameters names and values are user-defined.

Mandatory in every request.

Optional.

Headers names are unique.

Duplicate names are allowed.

Example:- User-agent:

                  Accept:

                  and etc.

Example:- sno=101 & sname=thakur

List of http request headers:

Http Header

Header Values

Accept-Encoding

gzip,deflate

Cookie

ISESSIONID=f73d57hfdn98789654ff8

Connection

keep-alive

Host

Localhost:8080

Referrer

http:// Localhost:8080/project/test

User-agent                   

Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0

If-Modified-since        

Date: Fri, 05 Feb 2014 16:11:54 GMT

Authorization

Authorization: Basic PWxhsdGRpnnjpncGVuIHNlc2FtZQ==

Accept-charset and etc…….

Accept-Charset: utf-8

To gather various details from client generate request being from Servlet program we need to use either ServletRequest object (or) httpServletRequest object. Using httpServletRequest object we can gather all the details from request. Using ServletRequest object we can gather only few details that mean we can’t gather details like header values & some miscellaneous info.  

 1. Different approaches of gathering request parameter values being servlet programs:

Use either ServletRequest object/HttpServletRequest object.

Example request url:

 

http://localhost:8080/project/test1?sno=101 & fname=dinesh & lname=thakur

Approach 1:

String s1=req.getParameter(“sno”); //gives 101

String s2=req.getParameter(“fname”); //gives dinesh

String s3=req.getParameter(“lname”); //gives thakur

Note: 1. we must know parameter name in order to get its value.

          2. If parameter contains multiple values then it gives only one value. (1st value)

Approach 2:              

Enumeration e = request.getParameterNames();

while (e.hasMoreElements()

{

String Pname=(String) e.nextElement();

String Pval=req.getParameter (Pname);

PW.println (Pname +”            ” +Pval);             

}

Approach 3:

String S[]= req.getParameterValues (“sname”);     // (holds dinesh, sandeep as element values)                                       

String S1 = req.getParameterValues (“sname”) [0]; //gives dinesh                                          

String S2 = req.getParameterValues (“sname”) [1]; //gives sandeep

Note:-  This is useful for gather the multiple values of each request parameter.


 If you give any request code then you must write in service (-,-)                                           


2. Different approaches of reading request headers values being from Servlet program:

We must use HttpServletRequest object for this

Approach 1:

String S1=req.getHeader (“user-agent”); //gives browser software name    

String S1=req.getHeader (“accept-language”); //gives en-us

Note: Here we must know header name to get its value. 

 

Approach 2:    

    

Enumeration e=req.getHeaderNames();  

while (e.hasMoreElements())                    

{

  String hname= (String) e.nextElement(); //gives each header name

  String hval=req.getHeader(hname); //gives each header value

  PW.println(hname+”               “ +hval);

}

                                               

Gives all the request heard names and values.

Using browser settings we can change certain header values.

Header values will change based on the browser software we use.

*** We can change accept-language value through browser setting.