HTML to Servlet Communication Using Hyperlinks

HTML to Servlet Communication Using Hyperlinks | For this, we need to place the request URL or servlet component request URL as href attributes value of the <a> tag.

Syntax:- <a href= ‘request-url-of-servlet-component’>Anchor-text</a>

Description:- In the browser, the html page should contain the link to the servlet component to get the wish message based on the current hour of the day. 

If it is between 5 AM to 12 PM then we should get “good morning” as a result, for 12 PM to  5 PM “good afternoon”, for 5 PM to 8 PM “good evening”, and for the remaining time the servlet component should give a result as “good night”. In all the cases, the generated page (by servlet component) should also contain a hyperlink to return back to the home (i.e. servlet component to HTML page).

Current Hour of the dayServlet component should give
result with message
5 AM to 12 PMGood Morning
12 PM to 5 PMGood Afternoon
5 PM to 8 PMGood Evening
8 PM to 5 AMGood Night

Create the following directory structure in <Tomcat_Home>/webapps folder

WishApp (Create directory in <Tomcat_Home>/webapps folder)

|==> home.html
|==> WEB-INF
   |==> web.xml
   |==> classes
      |==> WishServlet.java

HTML Page (home.html),

// home.html
<!DOCTYPE html>
<html>
<head>
   <title>Home</title>
</head>
<body>
   <h1 align="center">HTML to Servlet Communication through Hyperlinks</h1>
   <h2 align="center"><a href="getwish">Get Wish Message</a></h2>
</body>
</html>

Servlet Component (WishServlet.java),

// WishServlet.java
package com.kp.servlet;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import java.io.*;
import java.util.*;

public class WishServlet extends HttpServlet {
  public void service(HttpServletRequest req, HttpServletResponse res) 
                throws ServletException, IOException {
     // declare variables
     PrintWriter pw = null;
     Calendar cal = null;
     int hour = 0;

     // set content type
     res.setContentType("text/html");
     // get PrintWriter object
     pw = res.getWriter();

     // Get system date and hour
     cal = Calendar.getInstance();
     hour = cal.get(Calendar.HOUR_OF_DAY);
     // gives hour in 24 HOUR format

     // conditions for different result
     if(hour>=5 && hour<12)
       pw.println("<h1 style='color:green; text-align:center'>Good Morning</h1>");
     else if(hour>=12 && hour<17)
       pw.println("<h1 style='color:blue; text-align:center'>Good After-noon</h1>");
     else if(hour>=17 && hour<20)
       pw.println("<h1 style='color:orange; text-align:center'>Good Evening</h1>");
     else
       pw.println("<h1 style='color:yellow; text-align:center'>Good Night</h1>");

     // link to return back to the home
     pw.println("<h2><a href='home.html'>HOME</h2>");

     // close stream
     pw.close();
  }
}

Configure servlet component with URL pattern

// web.xml
<web-app>
   <servlet>
      <servlet-name>wish</servlet-name>
      <servlet-class>com.kp.servlet.WishServlet</servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>wish</servlet-name>
      <url-pattern>/getwish</url-pattern>
   </servlet-mapping>
</web-app>

See this application code at GitHub. We can give requests to the HTML file through  http:localhost:8080/WishApp/page.html, and from that HTML page, we can give requests to the servlet component through hyperlinks.

Priority of Public vs Private area web component

The servlet container gives first priority to pick-up private area web components based on the given request url. If it is not available then it will search for a public area web-component. 

While linking the received request to the web component, the servlet container always gives first priority to the private area web component. If they are not available mapped with current request url then the same component will be verified in the public area.

In this example, when we type http:localhost:8080/WishApp/page.html, then the execution flow will be:-

Tomcat web server hand-over the request to the web-container. Based on the “/page.html” the web-container checks that the information is available inside the INmemory metadata of web.xml file or not, but it is not available (because it is first request) so it will search in the public area of the “WishApp” folder. The files inside the WEB-INF comes under private area. The “page.html” is found in the public area so web-container fetch it as handovers the tomcat web-server. Finally, the tomcat web-server sends it to the web-browser.

Due to these reasons, don’t give public area web-components as the url pattern for the private area web component.

To demonstrate the above problem, do some changes in the web.xml file.

<web-app>
   <servlet>
      <servlet-name>wish</servlet-name>
      <servlet-class>com.kn.servlet.WishServlet</servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>wish</servlet-name>
      <url-pattern>/page.html</url-pattern>
   </servlet-mapping>
</web-app>

The updates are done in the web.xml file and the public area component will be fetched by the tomcat web-server, no need to reload the application. 

Now, give a request to http://localhost:8080/WishApp/page.html. We can observe that the HTML page developed by us will never be executed. Every time it only executes the servlet component, because the URL for the servlet component is “/page.html” placed in the web.xml file as the URL pattern. It proves that servlet containers give higher priority to private area web components compared to public area web components.

If the request path/URL for HTML and servlet component is matching then which will be executed? Answer:- Servlet component will be executed. Servlet containers always give high priority to private area web components (inside the WEB-INF folder) compared to a public area (outside of the WEB-INF folder).

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.