Question

In: Computer Science

How do you schedule a Servlet to run, from an HTML file, when the User clicks...

How do you schedule a Servlet to run, from an HTML file, when the User clicks on the Submit button? Show html code here

Solutions

Expert Solution

To run a Servlet from HTML page Request have to mapped to a backend (Servlet in our case). In HTML Page there will be button that will be used as a trigger to run the servlet. When you click Run servlet button request will be made to /MyServlet to resolve this mapping we have to make the entry to the web.xml with /MyServlet and map it to servlet named MyServlet. In MyServlet.java GET request will be received and the Output page will be generated as per output.print("...") statements written as HTML format. That page will be displayed by the Servlet file

>> INDEX.HTML:-

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content= "width=device-width">
<title>Running Servlet from HTML</title>
</head>
<body>
<h1>Scheduling a servlet from HTML Page</h1>
<hr>
<h3>This is a dummy form</h3>

<form action="MyServlet" method="get">
<input type="text" name="username" id="username" placeholder="User Name"/> <br/><br/>
<input type="submit" Value="Run Servlet"></submit> <br/><br/>
</form>
<hr>

</body>
</html>

>> WEB.XML:-

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>index</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>

>> MyServlet.java:-

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      
       response.setContentType("text/html");
       PrintWriter output=response.getWriter();   //used to Write on a web page
       String name=request.getparameter("username");       //Getting input from User Form Entry
       output.print("<html><body>");
       output.print("<h3>Hello "+ name+", this is called from a servlet</h3>");
       output.print("</body></html>");
   }

}

>> Output:-

.


Related Solutions

In a Servlet, how do you read HTML Form Data (data from the previous Web page)...
In a Servlet, how do you read HTML Form Data (data from the previous Web page) into the Servlet?
When you put a Servlet in a Web Application on the Tomcat/Glassfish server, how do you...
When you put a Servlet in a Web Application on the Tomcat/Glassfish server, how do you go about running this Servlet? a. From the Browser’s URL(give path):
How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
Create a HTML file that prompts the user to enter the number of dice (1-5) to...
Create a HTML file that prompts the user to enter the number of dice (1-5) to play with and display exactly as shown below (example run on Chrome). If the user enters a number less than 1, then set the number to 1. If the user enters a number greater than 5, then set it to 5. Center align elements and use height:200px for dice images. rollDice.html:   When user enters a number less than 1 When user enters a number...
Python Explain what happens when a user clicks a command button in a fully functioning GUI...
Python Explain what happens when a user clicks a command button in a fully functioning GUI program.
Explain how it is that we build the “processRequest()” method in a Servlet class, when the...
Explain how it is that we build the “processRequest()” method in a Servlet class, when the Servlet interface requires us to write 3 methods(init(), service() and destroy()). Why don’t we have to build these 3 methods?  
Explain how it is that we build the “processRequest()” method in a Servlet class, when the...
Explain how it is that we build the “processRequest()” method in a Servlet class, when the Servlet interface requires us to write 3 methods(init(), service() and destroy()). Why don’t we have to build these 3 methods???
When a user tries to write a file, the file system needs to detect if that...
When a user tries to write a file, the file system needs to detect if that file is a directory so that it can restrict writes to maintain the directory’s internal consistency. Given a file’s name, how would you design NTFS to keep track of whether each file is a regular file or a directory?
When logged in as the root user, how often would you do the command rm -rf...
When logged in as the root user, how often would you do the command rm -rf / ?
How do I make an HTML JSON button that when clicked will say Hello World!
How do I make an HTML JSON button that when clicked will say Hello World!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT