When we look into the code we write, we always go through an indefinite thought process and it stops at simple question in the end “How I can make it Better?”. The word “Better” will stay with you and it will give you some kind of superpower to analyze and breakdown the things you did and you will eventually find an even “Better” way to write the same code.
Now think the the scenario mentioned in the previous part where a server returns some content when client access the web application. Here we had only one static HTML file called “index.html” which always displays the “Hello World!” text.
Now apply the thought process. “How I can make it Better?”
Yes – It would be nice if the server responds with different content every time client sent a request to server. Say user should see a different greetings whenever client send the request –Sounds Good 🙂
Now we have a problem and we have to find a solution.We will see how to do this with the help of Servlets.
Understanding the Deployment Descriptor
As the name says, It describes how a web application should be deployed. Since we use Java language for writing the web application, the deployment descriptor is written using XML syntax and we name it as “web.xml” and store it in “WEB-INF” directory.
Our next step is adding a servlet information in the “web.xml” file. To do this we have two XML elements: servlet and servlet-mapping. We declare the servlet using the servlet element and specify the URL mapping using servlet-mapping element.
<servlet> <servlet-name>welcome_greeting</servlet-name> <servlet-class>GreetingServlet</servlet-class> </servlet>
servlet-name element is used to give our servlet a meaningful name and servlet-class element is used to specify the actual servlet Java class (Without extension).
<servlet-mapping> <servlet-name>welcome_greeting</servlet-name> <url-pattern>/greetings</url-pattern> </servlet-mapping>
servlet-name element is used specify the name of the servlet to which we can map the URL pattern which is specified using element url-pattern. The URL pattern we give here is used to resolve the URLs. Here we have used pattern “/greetings”. So if we apply URL “localhost:8080/helloword/greetings” we will see the response from our GreetingServlet. The complete “web.xml” file is given below.
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- To save as "tomcat_home\webapps\helloworld\WEB-INF\web.xml" --> <!-- Note: All <servlet> elements MUST be grouped together and placed IN FRONT of the <servlet-mapping> elements --> <servlet> <servlet-name>welcome_greeting</servlet-name> <servlet-class>GreetingServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>welcome_greeting</servlet-name> <url-pattern>/greetings</url-pattern> </servlet-mapping> </web-app>
That is all for today and we will see how to write the GreetingServlet class in the coming part using Java language and servlet API 🙂