Thursday, December 8, 2011

Java Crazy: Sleep() and Wait()

Java Crazy: Sleep() and Wait(): Difference between sleep() and wait() ? 1) wait() is a method of Object class. sleep() is a method of Thread class. 2) sleep() allows the ...

Java Crazy: Threads Example

Java Crazy: Threads Example: This programme checks the numbers between 0 to 100 and prints the numbers that are not divisible by 10 with a sleeping period of 500 millis...

Java Crazy: Structure of HttpRequest and HttpResponse

Java Crazy: Structure of HttpRequest and HttpResponse: The HttpRequest contains the following things. 1. Request Line ---- It will provide the information about the type of method called, the...

Java Crazy: Structure of HttpRequest and HttpResponse

Java Crazy: Structure of HttpRequest and HttpResponse: The HttpRequest contains the following things. 1. Request Line ---- It will provide the information about the type of method called, the...

Java Crazy: Retrieving form Parameters using HttpServletReques...

Java Crazy: Retrieving form Parameters using HttpServletReques...: Form parameters are key,value pairs where both are string objects. A Form parameter can be associated with single or multiple values. ...

Java Crazy: Statics Example

Java Crazy: Statics Example: class Test{ static int i=10; int j=20; public static void main(String[] args){ Test t1=new Test(); Test t2=new Test(); t2.i...

Java Crazy: Static in java

Java Crazy: Static in java: If you want print something in java without using static blocks or main method. class Google { static int i=m1();` public static...

Static in java

If you want print something in java without using static blocks or main method.


class Google
{
    static int i=m1();`
    public static int m1()
    {
        System.out.println("Hi......");
        return 10;
    }
}
---------- Output ----------
Hi......
java.lang.NoSuchMethodError: main
Exception in thread "main"

Wednesday, December 7, 2011

Statics Example

class Test{
static int i=10;
         int j=20;
public static void main(String[] args){
Test t1=new Test();
Test t2=new Test();
t2.i=200;
t2.j=400;
System.out.println(t1.i+"......."+t1.j);
}
}

O/P : 200,20

Saturday, December 3, 2011

Retrieving form Parameters using HttpServletRequest

  • Form parameters are key,value pairs where both are string objects.
  • A Form parameter can be associated with single or multiple values.
     Methods :
      
     public String getParameter(String pname)   ----  returns the value associated with the specified
                                                                                     parameter.

    public String[] getParameterValues(String pname)  ---- returns all values associated with specified
                                                                                                 paremter.

    public Enumeration getParameternames()  ----   returns all formParameter names associated with 
                                                                                    the  request.

    public Map getParameterMap()          ----              returns map object containing parameter names
                                                                                       as  keys and parameter values as map values.


       key String                                                        value String[]
   ----------------                                               ---------------------
             user                                                           {  degree }
          course                                                          {  SCJP,SCWCD }
             age                                                            {  67 }
           Sales                                                           { 1000 }


Programme to retrieve the above parameters :


Map  m=req.getParameterMap();
for(object o1 : m) {
mapentry m1=(map.entry)o1;
String pname=(String) m1.getkey();
String[] pValues=(String[])m1.getValue();
out.println(pname);
for(String s1:pValues)
{
     out.println(s1);
}
}








Thursday, December 1, 2011

Why is HttpSerlvet declared abstract ?

The HttpServlet doesnot contain any abstract methods.But, still it is declared as abstract because to prevent instantiation of HttpServlet. HttpServlet contains implementations of all the Http methods to send error information. So if you crreate an instance of HttpServlet and try to acess the methods we will get error information so the HttpServlet class is declared as abstract.

Structure of HttpRequest and HttpResponse

The HttpRequest contains the following things.

1. Request Line ----  It will provide the information about the type of method called, the requested 
                                 resource,and the Http protocol version followed by client.
2. Request Header ---- Provides configuration information of the browser like media types,encoding types
                                   supported by the browser. Server will use this request header to prepare response.
3. Request Body ----  Contains information about client. For  'get' request request body is optional. For 'post'
                                   it is mandatory.