Search This Blog
Friday, March 18, 2011
Servlet 2.4 Specification Notes
SRV.1.1 What is a Servlet?
A servlet is a JavaTM technology-based Web component, managed by a container,
that generates dynamic content. Like other Java technology-based components,
servlets are platform-independent Java classes that are compiled to platform-neutral
byte code that can be loaded dynamically into and run by a Java technology-enabled
Web server. Containers, sometimes called servlet engines, are Web server extensions
that provide servlet functionality. Servlets interact with Web clients via a
request/response paradigm implemented by the servlet container.
SRV.1.2 What is a Servlet Container?
The servlet container is a part of aWeb server or application server that provides the
network services over which requests and responses are sent, decodes MIME-based
requests, and formats MIME-based responses. A servlet container also contains and
manages servlets through their lifecycle.
All servlet containers must support HTTP as a protocol for requests and
responses, but additional request/response-based protocols such as HTTPS
(HTTP over SSL) may be supported.
Sequence of Servlet Request & Response
The following is a typical sequence of events:
1. A client (e.g., a Web browser) accesses a Web server and makes an HTTP request.
2. The request is received by the Web server and handed off to the servlet container.
The servlet container can be running in the same process as the host
Web server, in a different process on the same host, or on a different host from
the Web server for which it processes requests.
3. The servlet container determines which servlet to invoke based on the configuration
of its servlets, and calls it with objects representing the request and response.
4. The servlet uses the request object to find out who the remote user is, what
HTTP POST parameters may have been sent as part of this request, and other
relevant data. The servlet performs whatever logic it was programmed with,
and generates data to send back to the client. It sends this data back to the client
via the response object.
5. Once the servlet has finished processing the request, the servlet container ensures
that the response is properly flushed, and returns control back to the host
Web server.
Comparing Servlets with Other Technologies
In functionality, servlets lie somewhere between Common Gateway Interface (CGI)
programs and proprietary server extensions such as the Netscape Server API
(NSAPI) or Apache Modules.
Servlets have the following advantages over other server extension mechanisms:
• They are generally much faster than CGI scripts because a different process
model is used.
• They use a standard API that is supported by many Web servers.
• They have all the advantages of the Java programming language, including
ease of development and platform independence.
• They can access the large set of APIs available for the Java platform.
Servlet Interface
The Servlet interface is the central abstraction of the Java Servlet API. All servlets
implement this interface either directly, or more commonly, by extending a class
that implements the interface. The two classes in the Java Servlet API that implement
the Servlet interface are GenericServlet and HttpServlet. For most purposes,
Developers will extend HttpServlet to implement their servlets.
SRV.2.1 Request Handling Methods
The basic Servlet interface defines a service method for handling client requests.
This method is called for each request that the servlet container routes to an instance
of a servlet.
The handling of concurrent requests to a Web application generally requires
that the Web Developer design servlets that can deal with multiple threads executing
within the service method at a particular time.
Generally the Web container handles concurrent requests to the same servlet
by concurrent execution of the service method on different threads.
SRV.2.1.1 HTTP Specific Request Handling Methods
The HttpServlet abstract subclass adds additional methods beyond the basic
Servlet interface that are automatically called by the service method in the
HttpServlet class to aid in processing HTTP-based requests. These methods are:
• doGet for handling HTTP GET requests
• doPost for handling HTTP POST requests
• doPut for handling HTTP PUT requests
• doDelete for handling HTTP DELETE requests
• doHead for handling HTTP HEAD requests
• doOptions for handling HTTP OPTIONS requests
• doTrace for handling HTTP TRACE requests
Typically when developing HTTP-based servlets, a Servlet Developer will
only concern himself with the doGet and doPost methods. The other methods are
considered to be methods for use by programmers very familiar with HTTP programming.
SRV.2.1.2 Additional Methods
The doPut and doDelete methods allow Servlet Developers to support HTTP/1.1
clients that employ these features. The doHead method in HttpServlet is a specialized
form of the doGet method that returns only the headers produced by the doGet
method. The doOptions method responds with which HTTP methods are supported
by the servlet. The doTrace method generates a response containing all instances of
the headers sent in the TRACE request.
SRV.2.1.3 Conditional GET Support
The HttpServlet interface defines the getLastModified method to support conditional
GET operations. A conditional GET operation requests a resource be sent only if
it has been modified since a specified time. In appropriate situations, implementation
of this method may aid efficient utilization of network resources.
Thursday, March 17, 2011
Format My Source Code for Blogging
Format My Source Code for Blogging
Monday, March 14, 2011
this Keyword In Java
In java we use 'this' in different place are:
Using 'this field' inside constructor/non-static method.
Using 'this field' inside static method.
Using this() method inside constructor
Using this() method inside static/non-static methods.
Recursive constructor invocation using this().
S.No. |
Place
|
Description |
When
|
Where
|
Sample
|
1 |
Using
|
'this'
Most
|
We
|
Its should
|
package
public
{ int
public
{ this.intvalue=intvalue; } public
{ SampleThis
System.out.println("Int
} } Output: Int
|
2 |
Using
|
Not
|
- |
Compile
Error: cannot use
|
- |
3 |
Using
|
This()
|
While
|
'this()'
Otherwise
Constructor
|
package
public
{ int
public
{ this.intvalue=intvalue; } public
{ this(0); } public
{ SampleThis
System.out.println("Int
} } output: Int
|
4 |
Using
|
Compile
|
- |
Error: Constructor
|
- |
5 |
using
|
Compile
|
- |
|
package
public
{ int
public
{ this(); this.intvalue=intvalue; } public
{ this(0); } public
{ SampleThis
System.out.println("Int
} } |
Role of Super Keyword in Java
super() method @ Child class constructor
Super() with parameter method @ Child class constructor
Super()default/parameter method@ child class member method
Super keyword @child class Constructor
Super keyword @ child class member method
Basically super keyword always refer the parent class object reference. We all know the parent of all java class is java.lang.Object. So when ever we create a java file(class) with out extending any class \,java compiler will do three basic operation.
Extend parent class java.lang.Object
if no constructor(default/Parameter) for the child class its include
the default constructor.If no super() method in constructor its include the super() in the
Fact:
first line of the constructor.
when ever we create the object for any User define class we always able to
access the methods from the Object class.Its does means always
create two object in Heap memory one is user define class and another
one is Object class.
S.No | Place of | Description | When | Where Not | Sample |
1 | Super() default method @ | Super() First | While | It should be Present in Should not be two super() | |
2 | Super() with | Super() First But
| while | Same as S.No:1(Super() | package class Integer public System.out.println("Creating } public } class { public System.out.println("Creating } public super(Parentage); }
} public public { Child System.out.println("Parent } } output: Parent
|
3 | Super() default/parameter | Error as : Constructor | - | Compile time error | - |
4 | Super keyword @ Child | Its act as a parent class
| Its | It can present any where member methods. | package class Integer public System.out.println("Creating } public public {System.out.println("Inside } } class { public System.out.println("Creating } public super(Parentage); super.memberMethod(); super.memberMethod(); } } public public { Child System.out.println("Parent } }
Output: Inside Inside Parent |
5 | Super keword @ child class | Its | Its | It can present any where member methods. | Refer |
Friday, March 4, 2011
Static Keyword In Java
In this article i have explained the use of 'Static' keyword in different place of java.
like
1.Instance variable
2.In methods
3.In Class
4.In Inner Class -Create Object and call inner static and Non-Static methods.
5.Purpose of Static method and Static Class,static instance variable.
With Sample Program.
S.No | Place of static | Description | When executes | Where Not Include | Sample Code |
1 | Static block | Nothing but the block of code will be written inside of the static keyword in class . | Called only first time of object creation. First priority will always go to static block even normal block of code present | Inside Methods we can't declare a static block. | Package staticpck.block; public class StaticBlock { { System.out.println("Inside empty block"); } static {System.out.println("Inside static Block");} }
package staticpck.block; public class StaticBlockMain { public static void main(String arg[]) {StaticBlock staticBlock=new StaticBlock(); StaticBlock staticBlock1=new StaticBlock(); } }
Output: Inside static Block Inside empty block Inside empty block
|
2 | Static Instance Variable | Only one copy for the entire class.(each object reference have only one copy). Even you can access static variable with out creating reference for Object | Only one copy for entire class (Only class name is enough to refer instance variable) | NA(Inside Method local static variable are not possible) | package staticpck.Instvar;
public class InstanceVar { static int Instance=1; } package staticpck.Instvar; public class InstanceVarMain { public static void main(String arg[]) { InstanceVar instanceVar=new InstanceVar(); instanceVar.Instance=2; InstanceVar instanceVar2=new InstanceVar(); instanceVar2.Instance=3; System.out.println("instanceVar.Instance="+instanceVar.Instance); }}
Output: instanceVar.Instance=3
|
3 | Static @ method | Only one copy for the entire class.(each object reference have only one copy). Even you can access static variable with out creating reference for Object | When a method function is independent of its reference variable in that case we can use Static method. |
| package staticpck.method; public class Method { public static String getClassDesc() { return "This Class Refers Static Method";} }
package staticpck.method;
public class MethodImp { public static void main(String[] args) { System.out.println("With Out Obj Ref Call::"+Method.getClassDesc()); System.out.println("With Obj Ref Call::"+new Method().getClassDesc()); }}
OutPut: With Out Obj Ref Call::This Class Refers Static Method With Obj Ref Call::This Class Refers Static Method |
4 | Static @ class | We can't use static for class. | - | Compile time error | - |
5 | Static in inside method | We can't use static inside method. No use to keep its in Stack memory | - | Compile time error | - |
6 | Inner class : static @ instance variable | Error msg: The field “sample” cannot be declared static; static fields can only be declared in static or top level types | - | Compile time error | To eliminate error declare inner class as static |
7 | Inner class : static @ method | Error msg: The method “innermethod” cannot be declared static; static methods can only be declared in a static or top level type | - | Compile time error | To eliminate error declare inner class as static |
8 | Static @Inner class | If u want to declare static instance/static methods inside the Inner class than Inner class must be declared as Static | All execution are same for outer class |
| package staticpck.inner; public class InnerSample { static class InnerStatic{ static{ System.out.println("hi i am from inner static block");
} { System.out.println("Hi i am from Inner class block"); } static int instance; static String InnerMethod() { return "I am from Static InnerMethod"; } } class Inner{ //static int instance; String InnerMethod() { return "I am not able to use static due to My hirearchy"; } } }
package staticpck.inner;
public class InnerSampleImp {
public static void main(String arg[]) { System.out.println(InnerSample.InnerStatic.InnerMethod()); InnerSample innerSample=new InnerSample(); InnerSample.Inner inner=innerSample.new Inner(); System.out.println(); } }
Output: hi i am from inner static block Innser Static class:I am from Static InnerMethod |