Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

Create an application for generate Fibonacci series
Previous Home Next

In this servlet program, a custom Fibonacci series generated up to user demand.

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

Servlet Program

/*
 * Save as a FibonacciServlet.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 FibonacciServlet extends HttpServlet {

private int MAX_INDEX = 0;
private String string = null;

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 FibonacciServlet at "
            + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\
                     "FibonacciServlet\" method=\"POST\">");
out.println(" Series Generate Upto # <input type=\"text\
     " name=\"series\" value=\"\" size=\"20\" maxlength=\"2\"/>");
out.println
	("<BR><input type=\"submit\" value=\"Generate Series\" />");
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");
PrintWriter out = response.getWriter();
// Get value form TextField
String series = request.getParameter("series");
// User can't allow to leave textField empty
if (!series.equals("")) {
MAX_INDEX = Integer.parseInt(series);   //type conversion 
out.println("<b>Fibonacci Series Generated upto : " + " " 
	         + MAX_INDEX + "</b><BR><BR>");
// Generate Fibonacci Series upto Enter Value....
int high = 1;
int low = 1;
out.println("1 : " + low);
for (int i = 2; i < MAX_INDEX; i++) {
/*
* If value divided by 2 then
add string '+' sign, else leave it blank
*/
if ((i % 2) == 0) {
string = "+";
} else {
string = "";
}
out.println("<br>" + i + " : " + high + string);
high = low + high;
low = high - low;
}
} else {
out.println("Please Don't leave any TextField Empty !");
out.println
 ("<BR><a href=\"FibonacciServlet\">Return to Home Page</a>");
}
out.close(); //close text-output stream
}
@Override
public String getServletInfo() {
return "r4r.co.in-FibonacciServlet";
}
}
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: