Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

Create an application for store data into List
Previous Home Next

In this program, a data record can be store into List and 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>EntryBookServlet</servlet-name>
 <servlet-class>r4r.servlet.EntryBookServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>EntryBookServlet</servlet-name>
 <url-pattern>/EntryBookServlet</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="EntryBookServlet">EntryBookServlet</a>
  </form>
 </body>
</html>

Servlet Program

/*
 * Save as a EntryBookServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class EntryBookServlet extends HttpServlet {

private ArrayList arrayList;
private EntryBookClass bookClass;
private int lastModified = 0;

@Override
public void init() throws ServletException {
bookClass = new EntryBookClass();
arrayList = new ArrayList();
}

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 EntryBookServlet at "
       + request.getContextPath() + "</h1>");
out.println("<form name=\"form\" action=\
           "EntryBookServlet\" method=\"POST\">");
out.println("Enter Firstname : <input type=\"text\" name=\
   "fname\" value=\"\" size=\"30\" title=\"Firstname\" maxlength=\"10\"/>");
out.println("<BR>Enter Lastname : <input type=\"text\" name=\
  "lname\" value=\"\" size=\"30\" title=\"Lastname\" maxlength=\"10\"/>");
out.println("<BR>Enter Number   : <input type=\"text\" name=\
 "number\" value=\"\" size=\"30\" title=\"Number\" maxlength=\"10\"/>");
out.println("<BR>Enter Email Id : <input type=\"text\" name=\
 "email\" value=\"\" size=\"30\" title=\"Email\" maxlength=\"20\"/>");
out.println("<BR>Enter Address  : <textarea name=\"address\" rows=\
   "4\" cols=\"25\" title=\"Address\" /></textarea>");
out.println("<BR><input type=\"submit\" value=\"Submit Detail\" />");
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 form textField
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String number = request.getParameter("number");
// User can't allow to leave TextField empty
if (!fname.equals("") && !lname.equals("") && !number.equals("")) {
// Store data temp into EntryBookClass
bookClass.fname = request.getParameter("fname");
bookClass.lname = request.getParameter("lname");
bookClass.number = Long.parseLong(request.getParameter("number"));
bookClass.email = request.getParameter("email");
bookClass.address = request.getParameter("address");
// Add record into list
arrayList.add(bookClass);
lastModified = (int) System.currentTimeMillis();
// Invoke methods in doPost
printHeader(out);
printDataList(out);
printFooter(out);
} else {
out.println("Please Don't leave any TextField empty !");
out.println
 ("<BR><a href=\"EntryBookServlet\">Return to Home Page</a>");
}
out.close(); //close text-output stream
}

public int getLastModified() {
return lastModified;
}

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

/* -- Private Method -- */
// Method handle Output Form header
private void printHeader(PrintWriter out) {
out.println("<html>");
out.println("<head>");
out.println("<title>List</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Display Data list</h1>");
}

// Method handel output Form center
private void printDataList(PrintWriter out) {
Iterator iterator = arrayList.listIterator();
while (iterator.hasNext()) {
EntryBookClass object = (EntryBookClass) iterator.next();
out.println(" <hr align=\"left\" width=\"25%\"/>");
out.println("<BR>Firstname : " + " <b> " + object.fname + "</b>");
out.println("<BR>Lastname  : " + " <b> " + object.lname + "</b>");
out.println("<BR>Number    : " + " <b> " + object.number + "</b>");
out.println("<BR>Email ID  : " + "<b> " + object.email + "</b>");
out.println("<BR>Address   : " + "<b>" + object.address + "</b>");
out.println("<BR>Record modify : " + "<b>" + lastModified + "</b>");
}
// Slow down thread execution for 1 min.
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger
 (EntryBookServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}

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

Client Class Program


/*
 * Save as a EntryBookClass.java
 */
class EntryBookClass {
    public String fname;
    public String lname;
	public long number;
	public String email;
	public String address;
    
/* This class might also be used as insert
and fetch data form database..*/
}

Output of Program
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: