Put your text ad here
WestNIC provides reliable web hosting services
Web hosting directory, find affordable web hosting
Fastwebhost offers cheap web hosting & reseller hosting services
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.
Servlet are there?
HttpServlet declared abstract?
HttpServlet was concrete?
GenericServlet and HttpServlet?
ServletRequest and HttpServletRequest types?
getAttribute() and getParameter()?
service method, what next?
HttpServletRequest is an interface, how do I get an instance?
doPost() or doGet()?
doGet() and doPost()?
doPost() call doGet()?
Servlet are there?
A: In the Java servlet API there are several sub-interfaces and implementations of the Servlet interface. The GenericServlet class has general purpose implementations of servlet methods that are common to all, including getInitParameter(String), init(ServletConfig) and log(String). GenericServlet is abstract and leaves the key method service(ServletRequest, ServletResponse) abstract for sub-class implementation.
… full answer hidden
Premium members click below for full answer
What types of Servlet are there?
A: The class structure of servlets is fundamentally the same as any other Java class, however they must fulfil the Servlet and GenericServlet interface. These interfaces define a set of so-called lifecycle methods, a service method and various supporting methods for getting servlet configuration parameters and for logging.
The lifecycle methods init(ServletConfig) and destroy() are called when the servlet container first puts the servlet into service and ultimately removes it. The basic service(ServletRequest, ServletResponse) method is called whenever a servlet request is received by the servlet container.
… full answer hidden
Premium members click below for full answer
What is the class structure of servlets?
HttpServlet declared abstract?
A: The HttpServlet class is declared abstract because the default implementations of the main service methods do nothing and must be overridden. This is a convenience implementation of the Servlet interface, which means that developers do not need to implement all service methods. If your servlet is required to handle doGet() requests for example, there is no need to write a doPost() method too.
HttpServlet was concrete?
A: HttpServlet would be viable as a concrete class because it has no abstract methods, but it would be pointless because the default implementations of the service methods doGet(), doPost() etc. simply report that the relevant HTTP request methods are not supported. The HttpServlet class is part of a public Application Programming Interface (API), so the abstract modifier is a useful signal to programmers that the class has no practical use in itself and is intended to be extended to create HTTP servlets. In this case the abstract modifier is a design compromise that provides the default implementation of un-supported methods with a minimal safeguard to suggest these methods should be overridden.
GenericServlet and HttpServlet?
A: The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods. Java application developers normally extend the HttpServlet because it implements all the HTTP features necessary to create a Web application.
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers. The HttpServlet subclass passes generic service method requests to the relevant doGet() or doPost method. In principle, the GenericServlet could be extended to implement other protocols, such as an FtpServlet for example.
A: The servlet container creates an instance of the interface type HttpServletRequest to represent the details of a single HTTP request, which is passed to a servlet's service method (with an instance of HttpServletResponse). The implementation classes for the HttpServletRequest and HttpServletResponse objects are provided by the servlet container and may include proprietary code to support the vendor's overall container implementation. From a programmer's point of view you should not need to concern yourself with the vendor-specific features of these objects, only those provided by the servlet API.
ServletRequest and HttpServletRequest types?
A: ServletRequest and HttpServletRequest are interfaces, not a classes. Servlet containers must provide their own implementations of these types, which are passed to servlets' doGet and doPost methods.
… full answer hidden
Premium members click below for full answer
Why are there separate ServletRequest and HttpServletRequest types?
A: A servlet session is a very different thing from a servlet context. An HttpSession is a mechanism used to simulate and maintain a continuous user session between a Web browser and Web application, largely managed by the servlet container. The HTTP protocol is stateless, it is essentially a request-response scheme, so servlet sessions are maintained by passing a unique HTTP cookie value for each request, or by dynamically including an identifier in servlet URLs, known as URL-rewriting.
A ServletContext object represents the overall configuration of the servlet container and has several methods to get configuration parameters, exchange data amongst servlets, forward requests and load resources. The servlet context is usually obtained indirectly through the ServletConfig object, passed to a servlet's init(ServletConfig) method, or from the servlet getServletConfig() method.
A: The HttpServletRequestWrapper and HttpServletResponseWrapper classes are designed to make it easy for developers to create custom implementations of the servlet request and response types. The classes are constructed with the standard HttpServletRequest and HttpServletResponse instances respectively and their default behaviour is to pass all method calls directly to the underlying objects.
… full answer hidden
Premium members click below for full answer
What's the use of the servlet wrapper classes?
getAttribute() and getParameter()?
A: The servlet request getParameter(String) method gets the value of the named request parameter and returns it as a String. The most visible example of HTTP request parameters are those you see appended to a search engine URL after you submit a query, e.g. http://www.google.com/search?q=getParameter().
In this case a call to request.getParameter("q") would return the String getParameter().
This example shows the results of an HTTP GET request passed by an HTML form, the parameter name q is the name of the text input field. Request parameters may also be included in an HTTP POST submission too, but in this case they are not displayed as part of the URL on the form action handler.
The servlet request getAttribute(String) method is used to retrieve Java Object properties that may be added to the servlet request by the servlet container or Web application developer. Developers can add Java objects to a request with the request.setAttribute(String, Object) method. Attributes should be named like Java package names to ensure they don't clash. Servlet request attributes are most often used with RequestDispatcher methods to pass object references through the forward() and include() methods, as below.
… full answer hidden
Premium members click below for full answer
What's the difference between getAttribute() and getParameter()?
service method, what next?
A: For most common servlet implementations you should not override the service method, only the doGet or doPost methods. The servlet container provides its own abstract implementation of the HttpServlet class and its service method forwards requests to the doGet or doPost methods as appropriate.
HttpServletRequest is an interface, how do I get an instance?
A: The HttpServletRequest interface is implemented by the servlet container. The container instantiates and passes servlet request and response objects to the servlet's service method, which calls doGet or doPost in turn.
doPost() or doGet()?
A: Neither doPost() or doGet() methods are intrinsically faster than each other. The speed with which a servlet container responds to such requests is partly a matter of how much data is submitted with the request and the statements your servlet methods contain.
HTTP post forms are often designed to submit longer free-form text content, but this is not necessarily the case. If any servlet request handler does a great deal of data retrieval, network communication and processing it will take longer to execute, regardless of the submission method.
doGet() and doPost()?
A: The doGet() and doPost() methods are designed to handle HTTP GET and POST type requests respectively. When a GET request is submitted to a servlet container, it calls the doGet method of the servlet that handles the request via the service method.
The doGet() and doPost() methods both take the same arguments and will handle the servlet response in an equivalent manner, so for convenience a doPost() request can be passed to a doGet() method and vice-versa. However, bear in mind several properties of the servlet request object will be different for POST requests; it will not include a query string, and its getMethod() method will return "post".
doPost() call doGet()?
A: There is no requirement for doGet() to call doPost() or vice versa. As a convenience some servlets are set up so their doPost() method calls doGet() because they are intended to handle GET and POST requests identically. If both methods called each other you would create an endless loop that would quickly throw an exception.
A: The HttpServletRequest object contains all the key information for the HTTP request, not the source of the requested Web resource. Normally, JSP pages are compiled into servlets and processed automatically by the servlet container, so there is no need to write any additional processor code yourself. If you need to dynamically configure the servlet response, you should use JSP script elements or JSP tags in the body of the document and let the servlet container handle the processing.
If you really want to process the source of a static HTML document or suchlike, you can use the getResourceAsStream(String path) method of the ServletContext object to get an InputStream of the file.
… full answer hidden
Premium members click below for full answer
Does the servlet request contain the source of the Web page?
getRequestDispatcher() methods?
A: There is very little difference between the ServletContext and ServletRequest versions of this method, the RequestDispatcher you get operates in exactly the same way. However, the ServletRequest method also allows you to use a path argument that is relative to the "current" resource, starting with "./Example.jsp" or "../Example.jsp". The ServletContext getRequestDispatcher method should only be used for paths relative to the application root, starting with "/Example.jsp".
It generally avoids ambiguity and possible confusion to use a path relative to the application root.
getRequestDispatcher(String)?
A: This error means the compiler does not recognise this method signature in the context that you are calling it. The getRequestDispatcher method is not an HttpServlet method, it can only be called on a ServletRequest, HttpServletRequest or ServletContext object. For instance, you could use...
… full answer hidden
Premium members click below for full answer
Why do I get a compilation error with getRequestDispatcher(String)?
include() and forward() methods?
A: The RequestDispatcher include() method inserts the contents of the specified resource directly in the flow of the servlet response, as if it were part of the calling servlet. If you include a servlet or JSP document, the included resource must not attempt to change the response status code or HTTP headers, any such request will be ignored. The include() method is often used to include common "boilerplate" text or template markup that may be included by many servlets.
The RequestDispatcher forward() method is used to show a different resource in place of the servlet that was originally called. The forwarded resource may be another servlet, JSP or static HTML document, but the response is issued under the same URL that was originally requested. In other words, it is not the same as a redirection. The forward() method is often used where a servlet is taking a controller role; processing some input and deciding the outcome by returning a particular response page.
| Front-end FAQs | Back-end FAQs | Learn Java |
|---|---|---|
About us: site help, text ads and premium content FAQs.