Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

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

In this servlet program, all 26 English Alphabet can generate, put all into List, and then six random Alphabet can be pick form List.

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

Servlet Program

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

//Declare an ArrayList
private ArrayList arrayList = new ArrayList();
private String string = null;

@Override
public void init() throws ServletException {

// Provide Alphabet Range [65- 99]
char ascii[] = new char[100];
for (int i = 0, a = 65; a < 99; a++) {
ascii[i] = (char) a; //Type conversion
i++;
}

// Generate Alphabet [0-26] value and store all into array
System.out.println("Alphabet Generate :\n");
int count = 0;
for (int i = 0; i < 26; i++) {
string = "'" + ascii[count++] + "'" + " ";
arrayList.add(string); // store into array
}
}

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 randomValueServlet at " 
	         + request.getContextPath() + "</h1>");
out.println(" <form name=\"form\" action=\
             "randomValueServlet\" method=\"POST\">");
out.println("<BR><input type=\
         "submit\" value=\"Generate Random Value\" />");
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();
// Print array value [Generated Alphabet] on browser 
out.println
 ("Alphabet Generate :" +"<b>" + arrayList.toString() +"</b>");
        
/*
 * ---------------------------------------------------
 * Method -1: Pick six random alphabet value form List
 * ---------------------------------------------------
 */
int randomValue1 = 
	(0 + (Math.abs(new Random().nextInt())) % arrayList.size());
String string1 =
	(String) arrayList.get(randomValue1);       //Type conversion
int randomValue2 =
	(0 + (Math.abs(new Random().nextInt())) % arrayList.size());
String string2 =
	(String) arrayList.get(randomValue2);
int randomValue3 =
	(0 + (Math.abs(new Random().nextInt())) % arrayList.size());
String string3 = (String) arrayList.get(randomValue3);
int randomValue4 =
	(0 + (Math.abs(new Random().nextInt())) % arrayList.size());
String string4 = (String) arrayList.get(randomValue4);
int randomValue5 =
	(0 + (Math.abs(new Random().nextInt())) % arrayList.size());
String string5 = (String) arrayList.get(randomValue5);
int randomValue6 = 
	(0 + (Math.abs(new Random().nextInt())) % arrayList.size());
String string6 = (String) arrayList.get(randomValue6);
out.println("<BR><BR>Method-1: Six Random Value Pick form List : ");
out.println
	("<b>" + string1 + " " + string2 + " " + string3 +
	" " + string4 + " " + string5 + " " + string6 + "</b>");

/*
 * ---------------------------------------------------
 * Method -2: Pick six random alphabet value form List
 * ---------------------------------------------------
 */
out.println("<BR>Method-2: Six Random Value Pick from List :");
// Shuffle List value and pick six random value form array
Collections.shuffle(arrayList);
for (int i = 0; i < 6; i++) {
String value = (String) arrayList.get(i);
out.println("<b>" + value + "</b>" + " ");
}
out.println
("<BR><BR><a href=\"randomValueServlet\">Generate value again</a>");
out.close();
}

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