Servlets FAQ

How do I call one servlet from another servlet?

 

 

 

BookDBServlet database = (BookDBServlet)
                getServletConfig().getServletContext().getServlet("YOURSERVLETNAME");

—————

about servlet filters?

 

The Java Servlet specification version 2.3 introduces a new component type, called a filter. A filterdynamically intercepts requests and responses to transform or use the information contained in the requests or responses. Filters typically do not themselves create responses, but instead provide universal functions that can be "attached" to any type of servlet or JSP page.

Filters are important for a number of reasons. First, they provide the ability to encapsulate recurring tasks in reusable units. Organized developers are constantly on the lookout for ways to modularize their code. Modular code is more manageable and documentable, is easier to debug, and if done well, can be reused in another setting.

Second, filters can be used to transform the response from a servlet or a JSP page. A common task for the web application is to format data sent back to the client. Increasingly the clients require formats (for example, WML) other than just HTML. To accommodate these clients, there is usually a strong component of transformation or filtering in a fully featured web application. Many servlet and JSP containers have introduced proprietary filter mechanisms, resulting in a gain for the developer that deploys on that container, but reducing the reusability of such code. With the introduction of filters as part of the Java Servlet specification, developers now have the opportunity to write reusable transformation components that are portable across containers.

—————

what is servlet Listeners?

 

 

Listener is basically pre-defined interfaces that are available for developers in the application lifecycle to achieve some tasks especially when dealing with the ServletContext as well as HttpSession objects. While it saves a lot of time, it also makes the application less complex and more maintainable. In one web application, multiple listeners are allowed so it means that ServletContextListener may co-exist with HttpSessionListener. As you may have known, there are two Listeners that are widely used i.e. ServletContextListener and HttpSessionListener. They both are having different functionalities but both are equally important.
 
ServletContextListener will be executed once your web application is deployed in your application server (Tomcat or etc). If you have any requirements that need to be executed before the application is started, ServletContextListener is the best place for you. ServletContextListener also detects when your web application is removed. For example, if you replace the WAR file in Tomcat, Tomcat will automatically re-deploy your web application based on the latest WAR. Re-deploying means that Tomcat first removes the web application and then deploy the new web application. In this case, ServletContextListener should be able to notice when the web application is destroyed (removed) as well as when the web application is started (deployed). Just for your information, ServletContextListener is produced for you to deal with the ServletContext. Every web application in J2EE will have one ServletContext associated with it. The details of the ServletContext are not covered in this tutorial.
 
HttpSessionListener deals with the HttpSession object. HttpSession object are always used in every web application and are very useful in maintaining the data as it is available throughout the lifecycle of the web application until it is invalidated or the user closes the browser. This is the definition of HttpSession taken from the Sun website – “Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user”.

 

 

—————

difference between servlet filters and servlet Listeners?

 

Servlet Filter is used for monitoring request and response from client to the servlet, or to modify the request and response, or to audit and log.

Servlet Listener is used for listening to events in a web containers, such as when you create a session, or place an attribute in an session or if you passivate and activate in another container, to subscribe to these events you can configure listener in web.xml, for example HttpSessionListener.

Filetrs are used for pre and post process requests. look at the javax.servlet.Filter in your tomcat/jboss/other container javadoc

 

Where as the listeners are like triggers that can be attached to events in your app server (let's use the term container here) With listeners you can track application-level, session-level, life-cycle changes, attribute changes etc. The implemented interfaces are javax.servlet.*Listener interface.

Filters are called on a per-request basis and listeners are not. Filetrs have access to request object and listeners don't.

—————


Topic: Servlets FAQ

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————

Date: 23/12/2023

By: 1

Subject: Mr.

1

—————