Question: 1 What is a servlet filter?Ans:A filter dynamically 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 provide the ability to encapsulate recurring tasks in reusable units and can be used to transform the response from a servlet or a JSP page. Filters can perform many different types of functions:* Authentication* Logging and auditing* Data compression* LocalizationThe filter API is defined by the Filter, FilterChain, and FilterConfig interfaces in the javax.servlet package. You define a filter by implementing the Filter interface. A filter chain, passed to a filter by the container, provides a mechanism for invoking a series of filters. Filters must be configured in the deployment descriptor :<!--Servlet Filter that handles site authorization.--><filter><filter-name>AuthFilter</filter-name><filter-class>Test.AuthFilter</filter-class><description>This Filter authorizes user access to application components based upon request URI.</description><init-param><param-name>error_page</param-name><param-value>../../error.jsp</param-value></init-param></filter><filter-mapping><filter-name>AuthFilter</filter-name><url-pattern>/restricted/*</url-pattern></filter-mapping>Question: 2 What is a WAR file?Ans:A Web archive (WAR) file is a packaged Web application. WAR files can be used to import a Web application into a Web server. The WAR file also includes a Web deployment descriptor file. There are special files and directories within a WAR file. The /WEB-INF directory in the WAR file contains a file named web.xml which defines the structure of the web application. If the web application is only serving JSP files, the web.xml file is not strictly necessary. If the web application uses servlets, then the servlet container uses web.xml to ascertain to which servlet a URL request is to be routed. One disadvantage of web deployment using WAR files in very dynamic environments is that minor changes cannot be made during runtime. WAR file is created using the standard Java jar tool. For example:cd /home/alex/webapps/mywebappjar cf ../mywebapp.war *Question: 3 What is servlet exception?Ans:ServletException is a subclass of the Exception. It defines a general exception a servlet can throw when it encounters difficulty. ServletException class define only one method getRootCause() that returns the exception that caused this servlet exception. It inherit the other methods like fillInStackTrace, getLocalizedMessage, getMessage, printStackTrace, printStackTrace, printStackTrace, toString etc from the Throwable class.Packages that use ServletException :* javax.servlet* javax.servlet.httpQuestion: 4 What is the difference between an application server and a web server?Ans:A web server deals with Http protocols and serves the static pages as well as it can ask the helper application like CGI program to generate some dynamic content. A web server handles the client request and hands the response back to the client.An application server exposes business logic to client applications through various protocols, possibly including HTTP. An application server provides access to business logic for use by client application programs. In addition, J2EE application server can run EJBs - which are used to execute business logic. An Application server has a 'built-in' web server, in addition to that it supports other modules or features like e-business integration, independent management and security module, portlets etc.Question: 5 How can you implement singleton pattern in servlets ?Ans:Singleton is a useful Design Pattern for allowing only one instance of your class. you can implement singleton pattern in servlets by using using Servlet init() and <load-on-startup> in the deployment descriptor.Question: 6 Do objects stored in a HTTP Session need to be serializable? Or can it store any object?Ans:It's important to make sure that all objects placed in the session can be serialized if you are building a distributed applicatoin. If you implement Serializable in your code now, you won't have to go back and do it later.Question: 7 What is deployment descriptor?Ans:Deployment descriptor is a configuration file named web.xml that specifies all the pieces of a deployment. It allows us to create and manipulate the structure of the web application. Deployment descriptor describes how a web application or enterprise application should be deployed. For web applications, the deployment descriptor must be called web.xml and must reside in a WEB-INF subdirectory at the web application root. Deployment descriptor allows us to modify the structure of the web application without touching the source code.Question: 8 What is the web container?Ans:The Web container provides the runtime environment through components that provide naming context and life cycle management, security and concurrency control. A web container provides the same services as a JSP container as well as a federated view of the Java EE. Apache Tomcat is a web container and an implementation of the Java Servlet and JavaServer Pages technologies.Question: 9 Is HTML page a web component?Ans:No! Html pages are not web component, even the server-side utility classes are not considered web components. Static HTML pages and applets are bundled with web components during application assembly, but are not considered web components by the J2EE specification.Question: 10 How HTTP Servlet handles client requests?Ans:HTTP Servlet is an abstract class that must be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:* doDelete(HttpServletRequest req, HttpServletResponse resp) : Called by the server (via the service method) to allow a servlet to handle a DELETE request.* doGet(HttpServletRequest req, HttpServletResponse resp) : Called by the server (via the service method) to allow a servlet to handle a GET request.* doOptions(HttpServletRequest req, HttpServletResponse resp) : Called by the server (via the service method) to allow a servlet to handle a OPTIONS request.* doPost(HttpServletRequest req, HttpServletResponse resp) : Called by the server (via the service method) to allow a servlet to handle a POST request.* doPut(HttpServletRequest req, HttpServletResponse resp) : Called by the server (via the service method) to allow a servlet to handle a PUT request.* doTrace(HttpServletRequest req, HttpServletResponse resp) : Called by the server (via the service method) to allow a servlet to handle a TRACE request. etc.Question: 11 What are the uses of ServletResponse interface?Ans:ServletResponse interface defines an object to assist a servlet in sending a response to the client. The servlet container creates a ServletResponse object and passes it as an argument to the servlet's service method.To send binary data in a MIME body response, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter object returned by getWriter(). To mix binary and text data, for example, to create a multipart response, use a ServletOutputStream and manage the character sections manually.Packages that use ServletResponse :* javax.servlet* javax.servlet.http* javax.servlet.jspQuestion: 12 What are the uses of ServletRequest?Ans:ServletRequest defines an object to provide client request information to a servlet. The servlet container creates a ServletRequest object and passes it as an argument to the servlet's service method.A ServletRequest object provides data including parameter name and values, attributes, and an input stream. Interfaces that extend ServletRequest can provide additional protocol-specific data (for example, HTTP data is provided by HttpServletRequest.Packages that use ServletRequest :* javax.servlet* javax.servlet.http* javax.servlet.jspQuestion: 13 What is pre initialization of a servlet?Ans:Servlets are loaded and initialized at the first request come to the server and stay loaded until server shuts down. However there is another way you can initialized your servlet before any request come for that servlet by saying <load-on-startup>1</load-on-startup> in the deployment descriptor. You can specify <load-on-startup>1</load-on-startup> in between the <servlet></servlet> tag.Question: 14 Explain the directory structure of a web application?Ans:The directory structure of a web application consists of two parts.A private directory called WEB-INFA public resource directory which contains public resource folder.WEB-INF folder consists of1. web.xml file that consist of deployment information.2. classes directory cosists of business logic.3. lib directory consists of jar files.Question: 15 How do servlets handle multiple simultaneous requests?Ans:Servlets are under the control of web container. When a request comes for the servlet, the web container find out the correct servelt based on the URL, and create a separeate thread for each request. In this way each request is processed by the different thread simultaneously.Question: 16 Can we call a servlet with parameters in the URL?Ans:Yes! We can call a servlet with parameters in the URL using the GET method. Parameters are appended to the URL separated with the '?' with the URL and if there are more than one parameter, these are separated with the '&' sign.Question: 17 How will you communicate from an applet to servlet?Ans:You can write a servlet that is meant to be called by your applet.Applet-Servlet Communication with HTTP GET and POST :The applet can send data to the applet by sending a GET or a POST method. If a GET method is used, then the applet must URL encode the name/value pair parameters into the actual URL string.Communicating with Object Serialization :Instead of passing each parameter of student information as name/value pairs, we'd like to send it as a true Java object. Upon receipt of the this object, the servlet would add the this to the database.Sending Objects from a Servlet to an Applet :After registering the object in the database. Now the servlet has to return an updated list of registered students that is then returned as a vector of objects.Question: 18 What is Servlet chaining?Ans:Servlet Chaining is a phenomenon wherein response Object (Output) from first Servlet is sent as request Object (input) to next servlet and so on. The response from the last Servlet is sent back to the client browser.In Servlets, there are two ways to achieve servlet chaining using javax.servlet.RequestDispatcher:1. Include:RequestDispatcher rd = req.getRequestDispatcher("Servlet2");rd.include(req, resp);2. Forward, where req is HttpServletRequest and resp is HttpServletResponse:RequestDispatcher rd = req.getRequestDispatcher("Servlet3");rd.forward(req, resp);Question: 19 How do you communicate between the servlets?Ans:Servlets can communicate using the request, session and context scope objects by setAttribute() and getAttribute() method. A servlet can placed the information in one of the above scope object and the other can find it easily if it has the reefrence to that object.Question: 20 What is the use of setSecure() and getSecure() in Cookies ?Ans:The setSecure(boolean flag) method indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL.The getSecure() method returns true if the browser is sending cookies only over a secure protocol, or false if the browser can send cookies using any protocol.
Comments
Post a Comment