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?
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...
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???
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?
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...
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?
how to read a csv file in php and make a html table? I can't use...
how to read a csv file in php and make a html table? I can't use the PHP function fgetcsv. I can use explode. I can't put a php inside a php. Acme,Walmart,Ross,BJs,Target,Marshalls,Foot Locker,Giant,Charming Charlie 142,160,28,10,5,3,60,0.28,3167 175,180,18,8,4,1,12,0.43,4033 129,132,13,6,3,1,41,0.33,1471 138,140,17,7,3,1,22,0.46,3204 232,240,25,8,4,3,5,2.05,3613 135,140,18,7,4,3,9,0.57,3028 150,160,20,8,4,3,18,4.00,3131 207,225,22,8,4,2,16,2.22,5158 271,285,30,10,5,2,30,0.53,5702 89,90,10,5,3,1,43,0.30,2054 153,157,22,8,3,3,18,0.38,4127 87,90,16,7,3,1,50,0.65,1445 234,238,25,8,4,2,2,1.61,2087 106,116,20,8,4,1,13,0.22,2818 175,180,22,8,4,2,15,2.06,3917 165,170,17,8,4,2,33,0.46,2220 166,170,23,9,4,2,37,0.27,3498 136,140,19,7,3,1,22,0.63,3607 <!DOCTYPE html> <html> <head>    <meta charset="utf-8">    <title>Stores</title>    <link rel="stylesheet" href="style.css"> </head> <body> <h1>Stores</h1> <?php <table> <tr> <th>Acme</th> <th>Walmart</th> <th>Ross</th> <th>BJs</th> <th>Target</th> <th>Marshalls</th> <th>Foot Locker</th>...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT