Monday, 18 November 2013

Reading HTTP request headers from a servlet

Reference:


When a browser requests a web page, it generally sends some additional headers which specify things such as:
  • any cookies set for that page;
  • which browser or "agent" is making request, along with information about the operating system, installed browser plugins etc;
  • the referrer (generally the link that the user clicked on that provoked the request);
  • what types of content the browser can deal with.
It's beyond the scope of this tutorial to give all the details of every possibly HTTP request header. And it's actually quite rare to need to read request headers. If necessary, it is recommended that you look at the HTTP protocol specification if you need more details. However, we'll look at a couple of useful cases here by way of example.

Reading a request header

Usually, we'll know the name of the request header that we want to read. In this case, it is a simple matter of calling getHeader() on theHttpServletRequest object that was passed to our doGet()/doPost() method. For example, here is how to read the user-agent header, which essentially tells us which browser (IE, Mozilla, Safari etc) and operating system is making the request:
public final void doGet(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {
  String agent = req.getHeader("user-agent");
  if (agent != null && agent.indexOf("MSIE") > -1) {
    // Internet Explorer mode
  } else {
    // Non-Internet Explorer mode
  }
}
Note that we should be careful of null values being returned for a given header if it wasn't sent by the client. Note that for a given header, knowing what the actual format of data that we might get can be something of a black art and the user-agent string is one of the worst offenders...!

Some useful headers

Although it is not so usual to need to read request headers, here are some possibly useful cases:
user-agent
As mentioned above, the user-agent can give us the browser (or robot) and version, plus some information about plugins installed. A big problem with this header is the wide variety of formats that different agents use in practice. (See the link for details and examples.)
referer
The referer header (yes, with the word "referrer" misspelt) indicates the link that caused the current item to be requested.

What not to use request heaeders for

You should remember that, like pretty much anything sent in a request to your server, request headers are untrusted data. It is trivial to program a client to set any old string it likes in any request header. You should interpret request headers as a statement about "how the client would like its data", and not as the basis for security-related decisions. The BBC discovered this the embarrasing way when they attempted towithhold iPhone content on the basis of the user-agent string. (For similar reasons, you should take phrases such as "hot link prevention" with a pinch of salt.)

Thursday, 3 October 2013

Ways to create JSON object


1.
String response = "[
    {"result":
    {"3":{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"},
    "2":{"Hold":"0","Live":"0","Verification":"1","Conversion":"0","All":"1","Delivered":"0"},
    "1":{"Hold":"1","Live":"0","Verification":"1","Conversion":"0","All":"2","Delivered":"0"}},"errCode":"0","isSuccess":"true"}
    ]"



JSONArray jsonArrayResponse = new JSONArray(response);

//display
"[
    {"result":
    {"3":{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"},
    "2":{"Hold":"0","Live":"0","Verification":"1","Conversion":"0","All":"1","Delivered":"0"},
    "1":{"Hold":"1","Live":"0","Verification":"1","Conversion":"0","All":"2","Delivered":"0"}},"errCode":"0","isSuccess":"true"}
    ]"





2.
JSON jsonObj = jsonArrayResponse.getJSONObject(0).getJSONObject("result");




//display {"3":{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"},
    "2":{"Hold":"0","Live":"0","Verification":"1","Conversion":"0","All":"1","Delivered":"0"},
    "1":{"Hold":"1","Live":"0","Verification":"1","Conversion":"0","All":"2","Delivered":"0"}},"errCode":"0","isSuccess":"true"}

3.
JSONObject x = (JSONObject) jsonObj.get("3");
//display
{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"}
               
JSONObject cvb = new JSONObject(jsonObj.get("3").toString());

{"Hold":"0","Live":"1","Verification":"0","Conversion":"0","All":"1","Delivered":"0"}

String hold1 =    cvb.get("Hold").toString();

// display 0
           
String hold =    x.get("Hold").toString();

//display 0

System.out.println(x);



One additional method


4. jsonObj = JSONFactoryUtil.createJSONObject(result);

Tuesday, 6 November 2012

"Use same function by using element ID":Pass id as an argument to use function for different events on different elements


 we can also use a common function that binds to different event associated with different elements and we only pass id and access the function by identifying id in function argument like this


function ValidateForAllValue(id )
{
  var str=""
  $("select#"+id+" option:selected").each(function ( )
  {
    if($(this).val() =="Defined" )
    {
 
     str=str+$(this).val();


    }

    if(str=="Defined" && $(this).val() != "Defined")
    {
 
     alert("Invalid selection: option 'All' can not be selected with any other option");
 
 
        $(".AllClass").val([])
 
      return false;
    }
    if ($(this).val() == "None")
    {
      $(".AllClass").val([]);
 
      alert("Select some valid option");
 
    }
  });
};


Here this function displays alert for the element if  user select "All" along with other option.Therefore we can call this function for any select list by passing on onclick attribute like this:

<select onclick="ValidateForAllValue(this.id)">



$(this) always works on iteration

You cannot access  an element with $(this)  at event binding like this:

<select onclick="fun()">
....


fun()
{
 $(this).each(function ( )
{

.....

........

Reason : It works in iteration to refer to the element
                    $("select#"+id+" option:selected").each(function ( )
                   {
                          if($(this).val() =="Defined" )
                           {
   
                                    str=str+$(this).val();

                                   .............................

or you can also use that element at finction calling like this.....

<select id="xyz" onclick="fun(this.id)">