Pages

Saturday, February 16, 2013

Servlet Retrieving all request, response, header elements

Request Handling Practices :

                Hi, When we going to resolving issue need to ensure the request parameters are valid, and all the parameters are valid.

1.Retrieve the request parameters:

Usage : This 'll help you to ensure the request parameters:

String values[];String attripute="";

Enumeration obj_enum=request.getParameterNames();
  while(
obj_enum.hasMoreElements()){
    
attripute=(String) obj_enum.nextElement();
   values=request.getParameterValues(
attripute);
  if(values!=null){
  for(String str:values)
  System.out.println("Name : "+
attripute+" && Values : "+str);
   }
  }


                                          ***********************
2.Retrieve the Header values:

Usage : This 'll help you to allow to retrieve all the associated Header Parameters:

String attripute="",value="";


Enumeration header=request.getHeaderNames();
  while(header.hasMoreElements()){
    
attripute=(String) header.nextElement();
    
value=request.getHeader(attripute);
  if(
value!=null){
  System.out.println(
attripute+" && : "+value);
     }
  }

                                     *************************


3.Setting Response Contents type:

Usage : This 'll allow to set response types

PrintWriter out=response.getWriter();
 
response.setContentType("text/html");
out.println("<h1>Welcome "+firstname+"</h1><br><br>");
out.println("<h2>Your input Name : "+firstname+" "+lastname+"</h2>");
out.println("<h2>Ur Under "+category+" Category</h2>");

No comments:

Post a Comment