Open zhamri opened 6 years ago
import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;;
@WebServlet("/TotalRandomNumbers") public class TotalRandomNumbers extends HttpServlet { private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Total of 10 random numbers</title>");
out.println("</head>");
out.println("<body bgcolor =\"FFCC99\"> <center>");
out.println("<h1>Total of 10 Random Numbers</h1>");
Random r = new Random ();
int number [] = new int [10];
int totalNumber = 0;
for (int i = 0; i < 10; i++) {
number [i] = 100+r.nextInt(101);
totalNumber = number [i] + totalNumber;
}
out.print("<font size = '6'>Random Numbers : </font>");
for (int i = 0; i < 10; i++) {
out.print("<font size = '6'>"+number[i]+"</font)");
if (i != 9) {
out.print("<font size = '6'>,</font>");
}
}
out.print("<br><br>");
out.println("<font size = '6'>Total : "+totalNumber+"</font>");
out.println("</center> </body>");
out.println("</html>");
}
}
http://env-0670119.kilatiron.com/
http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;;
/**
*
* @author Azimjon Khamdamov
*/
public class servlet1 extends HttpServlet {
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<center>");
out.println("<title>SUM OF 10 RANDOM NUMBERS</title>");
Random num = new Random ();
int number [] = new int [10];
int sum = 0;
for (int n = 0; n < 10; n++) {
number [n] = 100+num.nextInt(101);
sum = number [n] + sum;
}
out.print("CHOOSEN 10 RANDOM NUMBERS: ");
for (int n = 0; n < 10; n++) {
out.print("<br>"+number[n]+"");
if (n != 9) {
}
}
out.print("<br>");
out.println("SUM OF NUMBERS : "+sum+"");
out.println("</center>");
}
}
Output
Link http://sumrandom.j.layershift.co.uk/
Reference: https://stackoverflow.com/questions/20331042/sum-of-ten-random-numbers http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3
thanks john ..
Servlet Code :
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Number Generator</title>");
out.println("</head>");
out.println("<body bgcolor = pink>");
out.println("<h1>Random Number Generator for 10 numbers between 100-200 </h1>");
Random r = new Random();
int generator[] = new int[10];
int sum = 0;
int b=0;
for (int k = 0; k <= 9; k++) {
generator[k] = 100 + r.nextInt(101);
sum = generator[k] + sum;
}
out.print("Output :");
out.print("<table border='1'>");
for (int i = 0; i < 10; i++) {
out.print("<tr>");
out.print("<td>Random number " + i + "</td>");
out.print("<td>"+ generator[i] +"</td>");
out.print("</tr>");
}
out.print("</table>");
out.println("</body>");
out.println("</html>");
out.println("Sum is : " + sum);
}
}
http://rdmgenerator.j.layershift.co.uk/
References : https://stackoverflow.com/questions/22167708/random-number-generating-using-jsp-servlets
Servlet Code import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>RANDOM NUMBERS</title>");
out.println("</head>");
out.println("<body bgcolor = orange>");
Random random = new Random();
int randomNum[] = new int[10];
int sum = 0;
for (int num = 0; num < 10; num++) {
randomNum[num] = 100 + random.nextInt(101);
sum = sum + randomNum[num];
}
out.print("<h1>PRINTING 10 RANDOM NUMBERS...</h1>");
out.print("<pre>");
for (int num2 = 0; num2 < 10; num2++) {
out.print(+ randomNum[num2] + " ");
if (num2 < 9) {
}
}
out.print("<br>");
out.print("<br>");
out.print("<h2>SUM IS = " + sum + "</h2>");
out.println("</body>");
out.println("</html>");
}
}
Output
Link http://random.j.layershift.co.uk/
References https://stackoverflow.com/questions/5271598/java-generate-random-number-between-two-given-values http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3 https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java
Code 1 ---> Index.html
Code 2 ----> sumNumber.java
import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/sumNumber"}) public class sumNumber extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<HTML>");
out.println("<Head>");
out.println("<body bgcolor = \"lightblue\"/>");
Random random = new Random();
int sum = 0;
int number []= new int[10];
for(int a = 0; a <10; a++){
number[a] = 100 + random.nextInt(101);
sum = number[a] + sum;
}
out.println("Random 10 number:");
out.println("<BR></BR>");
for(int i =0; i<10; i++){
out.println(i+1+" random number --> "+ number[i]);
out.println("<Br></br>");
}
out.println("Sum : " +sum);
out.println("<HR>DID YOU HAVE THE LUCK TODAY ?</HR>");
out.println("<A HREF = \"sumNumber\"> tryAgain </A>");
out.println("</HTML>");
}
Main Page
2nd Page
if User click tryAgain.. Same page but with different result
my Link : http://issue6.kilatiron.com/
reference : http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3
import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/*
@author CheeWei */ public class random_number 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. You may use following sample code. / out.println("<!DOCTYPE html>"); out.println(""); out.println("
"); out.println(" Random ran = new Random();
int num[] = new int[10];
int totalnum = 0;
for (int i = 0; i < 10; i++) {
num[i] = 100 + ran.nextInt(101);
totalnum = num[i] + totalnum;
}
for (int i = 0; i < 10; i++) {
out.println("<font size ='4'>Random number " + (i + 1) + " is " +"<font color='red'>"+ num[i] + "</font>");
out.println("<br></br>");
}
out.println("<font size ='8' color='blue'>Total random number is: " + totalnum + "</font>");
out.println("</center></body>");
out.println("</html>");
} finally {
out.close();
}
}
//
GET
method./**
POST
method./**
}
My link: http://test123.kilatiron.com/
reference: http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3
http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3
<%--
Document : index
Created on : 2017-12-1, 15:04:36
Author : Administrator
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Random Number</h1>
<h3><a href="RandomNum">Click It</a></h3>
</body>
</html>
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/RandomNum"})
public class RandomNum extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet RandomNum</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Random Number</h1>");
Random n=new Random();
int num[]=new int[10];
int Sum=0;
for(int i=0; i<10; i++){
num[i]=100+n.nextInt(100);
Sum=num[i]+Sum;
out.println(num[i]+" ");
}
out.println("<h3>Sum of Random number:"+Sum+"</h3>");
out.println("</body>");
out.println("</html>");
}
}
@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);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
package RandomNumber;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Aman
*/
@WebServlet(urlPatterns = {"/RandomNumber"})
public class RandomNumber extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet RandomNumber</title>");
out.println("</head>");
out.println("<body style=\"padding:100px 15%;font-size: 20px;\">");
out.println("<table><tr><td><Strong>Random numbers: </Strong></td><td>");
int total = 0;
Random r = new Random();
for(int i=0;i<10;i++){
int rn = r.nextInt((200 - 100) + 1) + 100;
out.println(rn);
if(i!=9)out.print(", ");else out.print(".");
total+=rn;
}
out.println("</td></tr><tr><td><Strong>Total: </Strong></td><td>"+total+". </td></tr></table>");
out.println("</body>");
out.println("</html>");
}
}
@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);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
Later
None
index.jsp
random.java
Result:
References: http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3 http://www.cnblogs.com/ningvsban/p/3590722.html
Link : http://randomnumberr.kilatiron.com/
Source Code :
Output :
References : http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3
servlet source codes <%-- Document : index Created on : Nov 24, 2017, 9:51:06 PM Author : thiviya --%>
<%@ page import="java.sql.*" %>
screenshot of the result
link of the servlet http://rannumber.kilatiron.com/
references http://www.java-examples.com/generate-random-numbers-using-math.random
,,,java servlet import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
/*
@author lilinhong */ @WebServlet(urlPatterns = {"/se6Servlet"}) public class se6Servlet extends HttpServlet {
/**
GET
and POST
@throws IOException if an I/O error occurs / protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { / TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println(""); out.println("
"); out.println(" arr[i] = 100+r.nextInt(100);
sum += arr[i];
out.print(arr[i]+" ");
}
out.println("<h1> the sum of ten number: "+sum+"</h1>");
out.println("</body>");
out.println("</html>");
} }
//
GET
method./**
POST
method./**
}
,,,
import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/randomNum"}) public class randomNum extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Random Number</h1>");
Random rdm = new Random();
int listNum[] = new int[10];
int add = 0;
out.println("");
out.println("List of Random Number : ");
for (int a = 0; a <= 9; a++) {
int rdmNum = 100 + rdm.nextInt(101);
listNum[a] = rdmNum;
add = rdmNum + add;
}
for (int b = 0; b < 10; b++) {
out.print(listNum[b]);
if (b < 9) {
out.print(", ");
}
}
out.println("<h2>Total : " + add);
}
}
Write a
Java Servlets
to sum 10 random numbers between 100 and 200. Then list all the numbers and the result.Example of output: Random numbers: 105, 150, 199, 133, 171, 175, 101, 188, 111, 136 Total: 1469
To answer this issue, you need to follow the guideline below: