Giving requests to servlet component by directly typing the request URL in the browser address bar is a quite complex process, non-technical end-user may feel complex. To overcome this problem we need to provide GUI to end-user having hyperlinks and submit buttons, for this we need to design webpages by using HTML files and should go for HTML to servlet communication.
HTML files generate static web pages, servlet components generate dynamic webpages, so to make static webpages talking to dynamic webpages we need to go for HTML to servlet communication.
HTML to servlet communication is possible in 3 ways,
1) Using hyperlinks:- The hyperlinks are used to send requests without having end-user supplied data. Example:- getting current trending jobs, show trending news, get all employees.
2) Using forms:- The forms are used to send requests to servlet components having end-user-supplied inputs. Example:- login page/form, registration form page, payment form/page
- OnLoad event:- Pages where we need to choose our country. When the web page is loaded, the request fetches all available countries given in the servlet components and puts them in the select box.
- OnBlur event:- After choosing a new email id, the request goes automatically to check whether it is already available or not. Here form component losing the focus.
- OnChange event:- OnChange event raises when we select items from the select box. When we select a country in a select box then it sends a request and gets all list of states from the servlet component and puts them in another select box.
HTML to Servlet Communication using Hyperlink
Example:- 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 the current hour of the day is between 5 AM to 12 PM then we should get “good morning” as 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 result as “good night”. In all the cases, generated page (by servlet component) should also contain a hyperlink to return back to the home (i.e. servlet component to an HTML page).
|Current Hour of the day||Servlet component should give the result with the following message|
|5 AM to 12 PM||Good Morning|
|12 PM to 5 PM||Good Afternoon|
|5 PM to 8 PM||Good Evening|
|8 PM to 5 AM||Good Night|
To get the current hour of the day you can use the below code. The “hour” variable will get the value in 24-hour format, not in the 12-hour format, therefore write your code accordingly.
// get system date and hour Calendar cal = Calendar.getInstance(); int hour = cal.get(Calendar.HOUR_OF_DAY);
See more here:- HTML to Servlet Communication using Hyperlinks.
Communication Using Forms
Example1:- Take an HTML form to gather the input from the end-user. End-user should pass name and age to check whether he/she is eligible for voting or not. In the servlet, components develop logic to check the age and display an appropriate message to the end-user. Assume 18 is the minimum age for voting.
Example2:- Develop an HTML page that should take the name, age, gender, address, marital status, qualification, and hobbies. Read those form data in the servlet component and display them on the browser.
See the solution here:- HTML to Servlet using Forms
Example:- In the HTML form page one select box will be there containing a different programming language name. Based on the selected language the creator of that language should display it on the browser.
In the HTML to Servlet communication using Form, we were not validating whether the entered data is valid or not. Before accepting the data entered by the end-user, and performing business operations we must check the input values. If the input values are valid then only perform the business operations, else don’t perform.
There are different technique to perform form validations:-
- Placing form validation logic only on the server-side.
- Placing form validation logic only on the client-side.
- Place form validation logic both on the server-side and client-side.
- Place form validation logic both in server-side and client-side, but execute server-side form validation logic only when client-side form validation logic is not executed.
Among them, the last approach is the best one. Learn more here:- Form Validation in Java Servlet
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!