Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for create a ToDoList through servlet
Previous Home Next

In this program, header property set through servlet

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>ToDoServlet</servlet-name>
      <servlet-class>r4r.servlet.ToDoServlet</servlet-class>
    </servlet>
	<servlet-mapping>
        <servlet-name>ToDoServlet</servlet-name>
        <url-pattern>/ToDoServlet</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="ToDoServlet">ToDoServlet</a>
        </form>
    </body>
</html>

Servlet Program

/*
 * Save as ToDoServlet.java
 */
package r4r.servlet;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
/**
 *
 * @author R4R
 */
public class ToDoServlet extends HttpServlet {

 private ToDoClass toDoClass;
 private List<ToDoClass> toDoClasseList;
 private DateFormat dateFormat;
 private String newDate;

 @Override
 public void init() throws ServletException {
  toDoClass = new ToDoClass(null, null, null, null);
  toDoClasseList = new ArrayList<ToDoClass>();
  // get the current date/Time in custom format
  dateFormat = new SimpleDateFormat("dd/mm/yyyy");
  newDate = dateFormat.format(Calendar.getInstance().getTime());
 }

 protected void processRequest
	 (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();
  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 ToDoServlet at "
           + request.getContextPath() + "</h1>");
out.println("<form action=\"ToDoServlet\" method=\"POST\">");
out.println("Set Title:<input type=\"text\" name=\"title\
        " value=\"\" size=\"30\" maxlength=\"20\"/><BR>");
out.println("Description: <textarea name=\
     "description\" rows=\"4\" cols=\"25\"></textarea><BR>");
out.println("Priority: <select name=\"priority\">");
out.println("<option> LOW </option>");
out.println("<option> MEDIUM </option>");
out.println("<option> HIGH </option></select><BR>");
out.println(" Create On: <input type=\"text\" name=\"date\" value=\"" 
	+ newDate + "\" size=\"30\" readonly=\"readonly\" /><BR>");
out.println("<input type=\"submit\" value=\"Submit\" />");
out.println("<input type=\"reset\" value=\"Reset\" />");
out.println("</form>");
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 {
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();

  // Get value from textField 
  String title = request.getParameter("title");
  String description = request.getParameter("description");
  String priority = request.getParameter("priority");
  String date = request.getParameter("date");

  // User can't allow to leave TextField empty
  if (!title.equals("") && !description.equals("")) {
toDoClass = new ToDoClass(title, description, priority, date);

// add record into list
toDoClasseList.add(toDoClass);

// Invoke methods in doPost
printHeader(out);
printDataList(out);
printFooter(out);

//Free all resource
out.close();
  } else {
out.println("Don't leave any Field empty !");
out.println
("<BR><BLINK><a href=\"ToDoServlet\">Return to Home page:</a></BLINK>");
  }
 }

 @Override
 public String getServletInfo() {
  return "r4r.co.in-ToDoServlet";
 }

 /* -- Private Method -- */
 // Method handel output form Header
 private void printHeader(PrintWriter out) {
  out.println("<html>");
  out.println("<head>");
  out.println("<title>r4r.co.in-List</title>");
  out.println("</head>");
  out.println("<body>");
  out.println("<h1>ToDo Data list</h1>");
 }

 //Method Handel output form center
 private void printDataList(PrintWriter out) {
  Iterator iterator = toDoClasseList.iterator();
  while (iterator.hasNext()) {
ToDoClass object = (ToDoClass) iterator.next();
out.println(" <hr align=\"left\" width=\"35%\"/>");
out.println("<BR>Title : " + " <b> "
            + object.getTitle() + "</b>");
out.println("<BR>Description  : " + " <b> "
            + object.getDescription() + "</b>");
out.println("<BR>Priority : " + " <b> "
           + object.getPriority() + "</b>");
out.println("<BR>Create Date  : " + "<b> " 
	        + object.getDate() + "</b>");
  }
  try {
// Slow down current Thread for 1 min, then processing request
Thread.sleep(1000);
  } catch (InterruptedException ex) {
Logger.getLogger(ToDoServlet.class.getName()).
	log(Level.SEVERE,
  "InterruptedException generated while processing request", ex);
  }
 }

 // Method Handel Output Form Footer
 private void printFooter(PrintWriter out) {
  out.println("</body>");
  out.println("</html>");
  out.println("<BR><BR><BLINK><a href=\"ToDoServlet\
                ">Add More Data into List</a></BLINK>");
 }
}

ToDoClass Program

/*
 * Save as a ToDoClass.java
 */
class ToDoClass {

    /*
     * This class act as Bean class.
     * This class might also be used as insert 
	 and fetch data form database
     */
    //---------------- Private Field
    private String title;
    private String description;
    private String priority;
    private String date;

    //------- Full Constructor
    public ToDoClass
(String title, String description, String priority, String date) 
		{
        this.title = title;
        this.description = description;
        this.priority = priority;
        this.date = date;
    }

    //--------------------------------- Getter/Setter
    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
Output of Program
Previous Home Next