Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for display digital clock in servlet
Previous Home Next

In this servlet program servlet and server information display on Browser.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
 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_3_0.xsd">
 <servlet>
  <servlet-name>dateTimeServlet</servlet-name>
  <servlet-class>r4r.servlet.dateTimeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>dateTimeServlet</servlet-name>
  <url-pattern>/dateTimeServlet</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
  30
  </session-timeout>
 </session-config>
</web-app>

Index.jsp

<%-- 
Document   : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
          content="text/html; charset=UTF-8">
  <title>r4r.co.in-servlet</title>
 </head>
 <body>
  <form>
  <h1>Access Servlet by servlet's Name!</h1>
  <a href="dateTimeServlet">dateTimeServlet</a>
  </form>
 </body>
</html>

Servlet Program

/*
 * Save as a dateTimeServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class dateTimeServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
	HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// Make custom Date and Time 
        

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String newDate = dateFormat.format(Calendar.getInstance().getTime());
try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet dateTimeServlet at "
               + request.getContextPath() + "</h1>");
/*Browser is refresh in every 1 sec, 
and date/Time is change on every refresh.*/
out.println
 ("<meta http-equiv='refresh' content='1'" + "URL=
'http://localhost:8084/servletExample/dateTimeServlet'> Current date/Time:"
             + newDate);
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

@Override
protected void doGet(HttpServletRequest request,
	    HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
	  HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "r4r.co.in-dateTimeServlet";
}
}
Previous Home Next