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):
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???
Do you find ads popping up, that are based on your past searches and clicks, to...
Do you find ads popping up, that are based on your past searches and clicks, to be convenient to your online experience? Or would you rather, ads specific to you, not come up?
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run...
Task #1 void Methods Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.    Below the main, write the function...
How do you calculate a change in residual earnings? Also how do you upload a file...
How do you calculate a change in residual earnings? Also how do you upload a file or paste in the question area?
How do you run an ANOVA test when some of the values are non-numeric? I have...
How do you run an ANOVA test when some of the values are non-numeric? I have a large excel database, over 100,000 cells worth of data. For the assignment, I need to see if there is a relation between the type of distribution channel (non-numeric) and the average amount of money spent (numeric). I HAVE to use ANOVA to solve this but I am not sure how. There are 11 different distribution channels and 100,000 different values for the amount...
Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
Using c programming language How do you put data from a text file into a 2d...
Using c programming language How do you put data from a text file into a 2d array For example a text file with names and age: john 65 sam 34 joe 35 sarah 19 jason 18 max 14 kevin 50 pam 17 bailey 38 one 2d array should have all the names from the file and one 2d array should have all the ages and both arrays should be printed out separately and be 3x3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT