What is Servlet ?
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Servlets also have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.
The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines lifecycle methods. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
Servlet Lifecycle
A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet
- Servlet Initialization using init() method
- Serving Client Request using service() method
- Reallocating memory using destroy() method
The servlet is initialized by calling the init () method. It is used for one-time initializations and called when the servlet is first created, not for each user request.
The servlet calls service() method to process a client's request. Each time the server receives a request for a servlet, the server creates a new thread and calls service. The service() method checks the HTTP request type (GET, POST etc.) and calls doGet, doPost etc. methods as appropriate.
The servlet is terminated by calling the destroy() method. It allows developers to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.
Finally, servlet is garbage collected by the garbage collector of the JVM.
Advantage of Servlet
1. Servlets provide a way to generate dynamic documents that is both easier to write and faster to run.
2. provide all the powerful features of JAVA, such as Exception handling and garbage collection.
3. Servlet enables easy portability across Web Servers.
4. Servlet can communicate with different servlet and servers.
5. Since all web applications are stateless protocol, servlet uses its own API to maintain session
URL Mapping
When there is a request from a client, servlet container decides to which application it should forward to. Then context path of url is matched for mapping servlets.
If you change url-pattern of AddPhotoServlet from /AddPhotoServlet to /MyUrl. Then, AddPhotoServlet servlet can be accessible by using /MyUrl. Good for the security reason, where you want to hide your actual page URL.
Rule 1 :
The server context on the servlet container matches the pattern in /inbox/* as follows:
http://apptech-solution.blogger.in/inbox/synopsis <—Correct
http://apptech-solution.blogger.in/inbox/complete?date=today <— Correct
http://apptech-solution.blogger.in/inbox <— Correct
http://apptech-solution.blogger.in/server1/inbox <— Incorrect
Rule 2 :
A context located at the path /geo matches the pattern in *.map as follows:
http://apptech-solution.blogger.in/geo/US/Oregon/Portland.map <—Correct
http://apptech-solution.blogger.in/geo/US/server/Seattle.map <—Correct
http://apptech-solution.blogger.in/geo/Paris.France.map <—Correct
http://apptech-solution.blogger.in/geo/US/Oregon/Portland.MAP <—Incorrect (case-sensitive)
http://apptech-solution.blogger.in/geo/US/Oregon/Portland.mapi <—Incorrect
Rule 3 :
A mapping that contains the pattern / matches a request if no other pattern matches. This is the default mapping. The servlet mapped to this pattern is called the default servlet.
The default mapping is often directed to the first page of an application. Explicitly providing a default mapping also ensures that malformed URL requests into the application return are handled by the application rather than returning an error.
thanks
ReplyDelete