Introduction to Java Web Application:
- It is used to create dynamic websites.
- Java provides support for web applications by using the Servlets and JSPs.
- The website can be created with static HTML pages, but when we want the information to be dynamic, we need a web application.
Java Web Application:
- The primary purpose of the java web application is to provide basic information about different components in Web Application and also provide the details on how we can use Servlet and JSP to create our first java web application.
Web Application Directory Structure:
Any web application we can have the following components like
- The Static contents like HTML.
- We have Client-side files we say, CSS and the Javascript.
- JSP (Java Server Pages), which use to generate dynamic content.
- Servlets.
- Some External library or the jar files.
- Java utility classes.
- We mainly use web.xml, also called as deployment descriptor.
- All the web servers and containers expect the web application to be available in a specific directory structure.
Below are the typical directory structure like:
1.Web Application Root Directory –
- It is an important or Root folder of the web application.
- Usually, the name of this folder becomes as per your web application context. For example, if our web application name is TestWebApplication, then the folder name will be TestWebApplication, and this web application will be accessible by using http://localhost:8080/TestWebApplication.
2. WEB-INF-
- It is the special directory under the web application of the root directory.
It is very special because very secured folder and the files available within this folder will not be accessible to the client directly, that means we say if this directory has one file “home.html,” then this file cannot be accessed directly via http://localhost:8080/TestWebApplication/home.html
3.WEB-INF/lib-
Every required jar file is placed inside under this directory.
4. WEB-INF/classes-
The java code, including the servlets for a web application, moves inside the classes folder.
5. web.xml –
This file is residing under the WEB-INF folder, and it is also called the deployment descriptor.
Web.xml (Deployment Descriptor)
- Web.xml is the standard file for any of the web applications and containers, which we come to know about the detail of web applications through web.xml file only.
- Every configuration of all the components for web application is done under the web.xml, and it is directly placed under the WEB-INF folder.
Let’s learn about Servlet Mapping:
- All the servlets mapping should be done in ‘web.xml’ so that the servlet container should be aware of the servlets and also make easy access from a browser.
- We should tell the servlet container what servlets to deploy and what URL’s to map with the servlets too.
- Below web.xml configures the Servlet “FirstServlet” with the two init-param.
<servlet> <servlet-name> FirstServlet </servlet-name> <servlet-class> FirstServlet </servlet-class> <init-param> <param-name>name</param-name> <param-value>First Servlet</param-value> </init-param> <init-param> <param-name>test</param-name> <param-value>learn</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name> FirstServlet </servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping>
Let’s understand about the XML file:
- In this Servlet is configured using the < Servlet> element where we assign the Servlet by a unique name, and also writes the qualified class of the Servlet. After that, map the Servlet to a specific URL. This should be done inside the <servlet-mapping> element.
- As we see above, all URL’s ending in .html is sent to the Servlet.
- As we see above, we are defined as one init-param which can be accessed in a servlet.
- As we see, the init params are defined within a servlet tag, which is limited to that Servlet.
Let us understand the concept of Context Parameters:
As we see, init parameters that are limited to the Servlet, but the context parameters are available to every Servlet of the web application.
<context-param> <param-name>param</param-name> <param-value>value</param-value> </context-param>
3 Responses