STIW2024 / Main-Issues

2 stars 0 forks source link

Servlets #6

Open zhamri opened 6 years ago

zhamri commented 6 years ago

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:

  1. Paste the servlets source codes here.
  2. Attach the screenshot of the result here
  3. Paste the link of the servlets here
  4. Paste your references here
NgZhengYi commented 6 years ago

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;;

@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>");
}

}

Output

image image

Link

http://env-0670119.kilatiron.com/

Reference

http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3

JohnWick27 commented 6 years ago

Servlet1.java

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 1

2

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

zhamri commented 6 years ago

thanks john ..

NgZhengYi commented 6 years ago

http://env-3197314.kilatiron.com/

ezzat787 commented 6 years ago

CODE: 1 0 1 3

OUTPUT: image image

LINK: http://randomnumber.kilatiron.com/

REFERENCE:http://study.com/academy/lesson/java-generate-random-number-between-1-100.html https://coderanch.com/t/380756/java/give-number

kzkit commented 6 years ago

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);
    }
}

servlet servlet2

http://rdmgenerator.j.layershift.co.uk/

References : https://stackoverflow.com/questions/22167708/random-number-generating-using-jsp-servlets

jairam0905 commented 6 years ago

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 1 2

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

kopilim96 commented 6 years ago

Code 1 ---> Index.html

Sum of Number
            Think of 10 Number within 100 and 200 
            If The Number You THINK in Your Mind is show in result 
            Then Today is Your Luckly Day. GOOD LUCK
            

kopilim96 commented 6 years ago

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>");
    }
kopilim96 commented 6 years ago

Main Page image

2nd Page image

if User click tryAgain.. Same page but with different result image

my Link : http://issue6.kilatiron.com/

reference : http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3

ChoyCheeWei commented 6 years ago

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;

/*

} 1 2

My link: http://test123.kilatiron.com/

reference: http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3

nadiahnaz commented 6 years ago

Servlet

code_index code01 code02

Result

web01 web02

Link

http://random.kilatiron.com/

Reference

http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3

YangYifei666 commented 6 years ago
<%-- 
    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";
    }

}

image image reference:http://www.cnblogs.com/ningvsban/p/3590722.html

Aman748 commented 6 years ago

Code :

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";
    }
}

Result :

2017-12-01 6 36 50

Link :

Later

References :

None

Koksheng commented 6 years ago

1 2 3 4 5 LINK: http://issuerandom.kilatiron.com/ REFERENCE: http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3

yeepheng96 commented 6 years ago

index.jsp screenshot 2017-12-04 10 27 54

random.java screenshot 2017-12-04 10 28 02 screenshot 2017-12-04 10 28 16 screenshot 2017-12-04 10 28 22

Result: screenshot 2017-12-04 10 23 31 screenshot 2017-12-04 10 23 40

References: http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3 http://www.cnblogs.com/ningvsban/p/3590722.html

Nadzifa707 commented 6 years ago

Link : http://randomnumberr.kilatiron.com/

Source Code :

image

image

image

Output :

image

image

References : http://www.onjava.com/pub/a/onjava/excerpt/java_cookbook_ch18/index.html?page=3

AiniArasad commented 6 years ago

image

image

image

image

image

Link : http://myrandomnumber.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

afiqznw commented 6 years ago

image image image image

http://randomnum.kilatiron.com/

thiviya commented 6 years ago

servlet source codes <%-- Document : index Created on : Nov 24, 2017, 9:51:06 PM Author : thiviya --%>

<%@ page import="java.sql.*" %>

Login page <% Connection con= null; PreparedStatement ps = null; ResultSet rs = null; String sql = "select password from userdetail"; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindb", "root", ""); ps = con.prepareStatement(sql); rs = ps.executeQuery(); %>

login page

<% } catch(SQLException sqe) { out.println("home"+sqe); } %>
Enter Your Name :
Enter Your Password :

screenshot of the result image image

link of the servlet http://rannumber.kilatiron.com/

references http://www.java-examples.com/generate-random-numbers-using-math.random

Lilinhong123 commented 6 years ago

,,,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;

/*

}

,,, 3 2 1

NurSyahirah1296 commented 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(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);
}

}

screenshot_1