mardi 1 décembre 2015

How to generate random numbers only on session change

Here I have used JSP to generate random numbers and calculate their sum and average. I haven't used sessions. But how can I implement session such that new random numbers generated only at new sessions and random numbers generated in the first session is retained throughout even with page refresh.

Here's the code

<%@page import="java.util.Date"%>
<%@page import="java.util.Random"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Working Title</title>
    </head>
    <body>
        <h1>Experiment</h1>
        <%
            Date createTime = new Date(session.getCreationTime());
            Date lastAccessTime = new Date(session.getLastAccessedTime());

            int r = 25, c = 5, n;
            double t=0;
            int i=0,j=0;
            double num[][] = new double[27][5];
            Random ran = new Random();
            out.print("<table border='1px'>");
            out.print("<tr><th>Trial-1</th><th>Trial-2</th><th>Trial-3</th><th>Horizontal Sum</th><th>Horizontal Average</th></tr>");
            for(i=0;i<r;i++) {
                t=0;
                out.print("<tr>");
                for(j=0;j<c-2;j++) {
                    n = ran.nextInt(10-1+1)+1;
                    num[i][j]=n;
                    t = t+ n;
                    out.print("<td>"+ n +"</td>");
                }
                num[i][3]=t;
                num[i][4]=t/3;
                out.print("<td>"+ t +"</td> "+ "<td>"+ t/3+"</td> ");
                out.println("</tr>");
            }
            for(i=0;i<c;i++) {
                t=0;
                for(j=0;j<r;j++) {
                    t = t + num[j][i];
                }
                num[j][i]=t;
                num[j+1][i]=t/25;
            }
            for(i=25;i<27;i++) {
                out.print("<tr>");
                if(i==25)
                    out.print("<th colspan='3'>Vertical Sum</th>");
                if(i==26)
                    out.print("<th colspan='3'>Vertical Average</th>");
                for(j=3;j<c;j++) {
                    out.print("<td>"+ num[i][j]+"</td>");
                }
                out.println();
                out.print("</tr>");
            }
            out.print("</table>");
        %>
    </body>
</html>




Aucun commentaire:

Enregistrer un commentaire