0% found this document useful (0 votes)
198 views3 pages

Servlet Invoked by HTML Form

The document provides code examples for creating a servlet in Java that displays the current date and time. The servlet uses HTML, Java, and XML code to: 1) Define an HTML page with a link to invoke the servlet 2) Write the Java servlet class to generate and output the HTML 3) Configure the servlet in an XML file so it can be accessed at a specific URL.

Uploaded by

Prasanth Bala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
198 views3 pages

Servlet Invoked by HTML Form

The document provides code examples for creating a servlet in Java that displays the current date and time. The servlet uses HTML, Java, and XML code to: 1) Define an HTML page with a link to invoke the servlet 2) Write the Java servlet class to generate and output the HTML 3) Configure the servlet in an XML file so it can be accessed at a specific URL.

Uploaded by

Prasanth Bala
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write programs in Java using Servlets: o To invoke servlets from HTML forms HTML Code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Refresh Servlet Timer</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <style type="text/css"> A:link {text-decoration: none; padding: 3px 7px; margin-right: 3px; border-bottom: none; color: #2d2b2b; } A:visited {text-decoration: underline; padding: 3px 7px; margin-right: 3px; color: #2d2b2b; } A:active {text-decoration: none} A:hover {text-decoration: none; padding: 3px 7px; margin-right: 3px; border: 0px; color: #2d2b2b; } </style> </HEAD> <BODY> <br><br><br> <br><br><br> <table width="200px" height="100px" align="center" bgcolor="#BBFFFF" border=0> <tr> <td style="text-align:top;" valign="middle" align="center" border=0> <a href="timer" ><b>Refresh Servlet Timer</b></a> </td> </tr>

</BODY> </HTML>

Servlet Code:
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; public class timer extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); Date now = new Date(); // The current date/time out.println("<html>"); out.println("<head><title> Time Check </title></head>"); out.println("<body>"); out.println("<table width='500' align='center' valign='top' bgcolor='#FFFFE0'>"); out.println("<tr>"); out.println("<td>&nbsp;"); out.println("</td>"); out.println("</tr>"); out.println("</tr>"); out.println("<tr>"); out.println("<td valign='top' align='center' valign='top'>"); out.println("<p style='color:#00000;font-size:20pt'><b>Date and Time Refresh After 10 Seconds.</b></p>"); out.println("<td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>&nbsp;"); out.println("</td>"); out.println("</tr>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>&nbsp;"); out.println("</td>"); out.println("</tr>"); out.println("</tr>");

out.println("<tr>"); out.println("<td style='backgroundcolor:#C6EFF7;color:blue;border:1px solid;width: 100px' align='center'>"); out.println("<b>The current time is: " + now + "</b>"); out.println("</td>"); out.println("</tr>"); out.println("<table>"); out.println("</body></html>"); response.setHeader("Refresh", "10"); } }

XML Code
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <servlet> <servlet-name>timer</servlet-name> <servlet-class>timer</servlet-class> </servlet> <servlet-mapping> <servlet-name>timer</servlet-name> <url-pattern>/timer</url-pattern> </servlet-mapping> </web-app>

Output:

You might also like