In this tutorial you will learn how to dispaly date in servlet
Displaying Date In The Servlet
To display date into the servlet you need to import java.util.Date() class.
ServletDateExample.java
import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletDateExample extends HttpServlet{ public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{ PrintWriter out=response.getWriter(); response.setContentType("Text/Html"); Date date=new Date(); out.println("<Body bgcolor=green>"); out.print("<H1> Today - " +date.toString()+"</H1>"); out.println("</Body>"); } }Now add the following mapping code in your web.xml file
<servlet> <servlet-name>DateExample</servlet-name> <servlet-class>ServletDateExample</servlet-class> </servlet> <!-- servlet mapping --> <servlet-mapping> <servlet-name>DateExample</servlet-name> <url-pattern>/today</url-pattern> </servlet-mapping>When you run this application it will display message as shown below:
[ 0 ] Comments