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 count number of time servlet access

Previous Home Next

In this servlet program, a user can assess servlet number of time.

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

Servlet Program

/*
 * Save as a CountServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class CountServlet extends HttpServlet {
private int value = 0;
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 CountServlet at " 
	 + request.getContextPath() + "</h1>");
out.println("<form name=\"form\" action=\
             "CountServlet\" method=\"POST\">");
out.println("<BR>Enter Firstname :");
out.println("<input type=\"text\" name=\
             "fname\" value=\"\" size=\"20\" />");
out.println("<BR>Enter Lastname :");
out.println("<input type=\"text\" name=\
               "lname\" value=\"\" size=\"20\" />");
out.println("<BR> <input type=\"submit\" value=\
             "Submit Form\" name=\"submit\" />");
out.println("<input type=\"reset\" value=\"Reset Form \" />");
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 {
// processRequest(request, response);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// increment value by 1 on every refresh of this page
String count = " " + ++value + " time";
// Get value form TextFiled
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
// User can't allow to leave any field empty
if ((!fname.equals("")) && (!lname.equals(""))) {
out.println("<html>");
out.println("<head>");
out.println("<body>");
out.println("<b>Hello:" + fname + " " + lname + "</b>");
//print name of viewer
out.println("<br>viewer:" + count);                                             
//print count digit
out.println("</body>");
out.println("</html>");
} else {
out.println("Please don't leave any textfield empty!");
out.println("<BR><a href=\"CountServlet\">Return to home Page</a>");
}
out.close(); //close text-output stream
}

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