MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

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