Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for generate random numbers
Previous Home Next

In this servlet program, six random number in between 0-9 range can be generated 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>randomNumberServlet</servlet-name>
   <servlet-class>r4r.servlet.randomNumberServlet
             </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>randomNumberServlet</servlet-name>
  <url-pattern>/randomNumberServlet</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="randomNumberServlet">randomNumberServlet</a>
   </form>
  </body>
</html>

Servlet Program

/*
 * Save as a randomNumberServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class randomNumberServlet extends HttpServlet {

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 randomNumberServlet at " 
	   + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\
             "randomNumberServlet\" method=\"POST\">");
out.println("<BR><input type=\"submit\
                " value=\"Generate Random Number\" />");
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();
/*
 * -------------------------------------
 * Method -1: Generate Six Random Number
 * -------------------------------------
*/
List<Integer> numbers = new ArrayList<Integer>();
//Generate only single digit number in between 0-9
for (int i = 0; i < 9; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
out.print(" Method-1: Six Random number generated are: ");
// Generate Six Random number 
for (int j = 0; j < 6; j++) {
out.println("<b>" + numbers.get(j) + "</b>" + " ");
}
/*
 * -------------------------------------
 * Method -2: Generate Six Random Number
 * --------------------------------------
 */
//Every num generate only single digit number in between 0-9
int num1 = (int) (0 + (Math.random() * 10));
int num2 = (int) (0 + (Math.random() * 10));
int num3 = (int) (0 + (Math.random() * 10));
int num4 = (int) (0 + (Math.random() * 10));
int num5 = (int) (0 + (Math.random() * 10));
int num6 = (int) (0 + (Math.random() * 10));
out.println("<BR>Method-2: Six Random number generated are:");
out.println("<b>" + num1 + " " + num2 + " " + num3 + " " 
	   + num4 + " " + num5 + " " + num6 + "</b>");
out.println("<BR><BR><a href=\"randomNumberServlet\
             ">Generate number again</a>");
out.close();
}

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