I would post a series of servlets questions in coming days, I think its very important for any guy to have a glance who is going for J2EE technical discussion. These set of questions would help you find out, if there are any grey areas left in your preparation. I would post the answers some time later, Till then try to find if you can answer them.
Here goes the first list.
Ans:
Table 1. The Web Application Directory Structure | |
Directory | Contains |
/MyApp | This is the root directory of the web application. All JSP, images, js and XHTML files are stored here or in its subdirectories. Files other than in WEB-INF can be directly accessed through URL or are the Universal scope files. |
/MyApp/WEB-INF | This directory contains all resources related to the application This is where your web application deployment descriptor is located. Note that the WEB-INF directory is not in the public/universal scope. No files contained in this directory can be directly accessed from url/browsers. |
/MyApp/WEB-INF/classes | This directory is where servlets, utility classes and packages are located. |
/MyApp/WEB-INF/lib | This directory contains Java Archive files that the web application depends upon. For example, this is where you would place a JAR file that contained a JDBC driver. |
Ans: Web.xml is called the web application deployment descriptor. We define/configure
the web application related settings/configurations in this file. e.g. Servlet definitions
and configurations.
Q3. Can a web application have more than one web.xml?
Ans: No, one webapplication have one web.xml in WEB-INF/
Q4. What is a war file?
Ans:
Q5. What is <welcome-file-list>?
Ans:
<welcome-file-list>
<welcome-file>Home.jsp</welcome-file>
</welcome-file-list>
Ans:
Ans:
The servlet-mapping element defines a mapping between a servlet and a URL pattern. | ||
Element | Required? | Description |
| Required | The name of the servlet to which you are mapping a URL pattern. This name corresponds to the name you assigned a servlet in a |
| Required | Describes a pattern used to resolve URLs. The portion of the URL after the http://host:port + WebAppName is compared to the |
Q
may not function as expected if we do this.
Ans:
Ans:
Ans:
Ans:
Ans:
Ans: The container always invokes the service() method of a servlet.
depending upon the request method doGet or doPost are called from within service() method.
Ans: Get request is throught URL the parametes are passed in a query string,
the URL size can not exceed 1K whereas there is no such limitation on POST.
Default request method is GET. There is no difference between the 2 in terms of performance.
Q21. What are init parameters? Where do you define them?
e.g. you may want to pass the database username and password information to a servlet or a file name and path etc.
We define them in web.xml. They are of 2 types. Servlet Init Parameters and context parameters. Servlet Init Parameters are available only to the servlet for which they have been defined.context init paramters are available to all the servlets.
If we want to use the same set of parmeters for 2 or more servlets we define them as context init parameters.
Q22. What if I give the same name to 2 init parameters would this be error?
Ans: No this is not an error, we would get the value of the init parameter defined last in the order.
Q23. How would you fetch init parameters in servlets?
The init parameter name should be unique in the Servlets. If 2 init parameter names are not unique, the servlet would pick the value of the init parameter defined last in web.xml.
Ans: No, we don't have the Servlet config reference in servlet constructor.
Ans: Before any request can hit the service method, We may require some setup/initializations in the servlet. e.g. you may want to open the database connection, some datafile or load some cache before the servlet is ready for request handling. To do this we override the init() method of the servlet. The servlet container would call the init before the servlet go into service ready state.
By default the servlet life cycle kicks in when first request is recieved by the servlet, Servlet instantiates and then init is called. untill init is not successfull, each request for the servlet will have to wait.
You may want to intialize the servlet as soon as the webapplication starts or the servle container starts, This can be done if we define the <load-on-startup> for <servlet> in web.xml.
Preinitializing the servlet along with the container is called servlet the pre-intialization.
My Favourite Books :
Core Java: Programmer's Guide to Java Certification : A Comprehensive Primer (Second Edition) by Mughal and Rasmussen
I am very big fan of this book, this is a must have book if you want to explore the core of java in detail.
I recently discovered that the Authors are coming up with their next book called Java Actually : A first course in programming
The TOC looks very good.
0 comments:
Post a Comment