Keep data from same session in Servlets












0














protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Step 1: set the content type
response.setContentType("Text/html");

// Step 2: get the printwriter
PrintWriter out = response.getWriter();

HttpSession ssn = request.getSession();
if(ssn != null)
{
out.println("Your session Id is : ");
String ssnId = ssn.getId();
out.println(ssnId);
out.println("<br/><br/>");


if (ssn.isNew()) {
out.println("This is a NEW session");
out.println("<br/><br/>");

Random rando = new Random();
int max = 10;
int min = 0;
int trials = 3;
int random = new int[trials];
int fiveColumns = new int[135];
double horAvg = new double[27];

System.out.println(fiveColumns.length);
for(int i = 0; i<fiveColumns.length;i=i+5)
{
fiveColumns[i] = rando.nextInt(max)+1;
fiveColumns[i+1] = rando.nextInt(max)+1;
fiveColumns[i+2] = rando.nextInt(max)+1;

// Horizontal Sum
fiveColumns[i+3] = fiveColumns[i] + fiveColumns[i+1] + fiveColumns[i+2];
// Horizontal Average
fiveColumns[i+4] = fiveColumns[i+3]/3;

}
ssn.setAttribute("arrayOfOldSession", fiveColumns[0]);

out.println("Length of Array: " + fiveColumns.length);
out.println("<br/><br/>");

out.println("<br/><br/>");
System.out.println("Trial1" + "t" + "Trial2" + "t" + "Trial3" + "t" + "Horizontal Sum" + "t" + "Horizontal Avg");
out.println("Trial1" + "&emsp;" + "Trial2" + "&emsp;" + "Trial3" + "&emsp;" + "Horizontal Sum" + "&emsp;" + "Horizontal Avg");
out.println("<br/><br/>");
for(int i = 0; i<fiveColumns.length;i=i+5)
{
System.out.println(fiveColumns[i] + "t" + fiveColumns[i+1] + "t" + fiveColumns[i+2] +
"t" + fiveColumns[i+3] + "tt" + fiveColumns[i+4]);

out.println("<table>");
out.println("<tr><td>");
out.println(fiveColumns[i] + "&emsp;&emsp;&emsp;" +
fiveColumns[i+1] + "&emsp;&emsp;&emsp;" + fiveColumns[i+2] +
"&emsp;&emsp;&emsp;" + fiveColumns[i+3] +
"&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;"
+ fiveColumns[i+4]);
out.println("</tr></td>");

}
out.println("</table>");
out.println("<br/><br/>");

out.println("<html><body>");
out.println("<h2>Hello Word</h2>");
out.println("<hr>");
out.println("Time on the server is: " + new java.util.Date());
out.println("</html></body>");
}
else {
out.print("This is an OLD session");
out.println("<br/><br/>");

int old = (int) ssn.getAttribute("arrayOfOldSession");

out.println(old);

System.out.println(old[0]);
}

}
else
{
out.println("Your session is not created yet");
}


Basically what I'm trying to do is, that if it's an old session, I want to show the data that was displayed when the session was new. The only time I want new data is if the session is new. I'm trying to use setAttribute when the session is new and getAttribute when the session is old. Would I be able to store and grab array data like this? Thanks










share|improve this question



























    0














    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Step 1: set the content type
    response.setContentType("Text/html");

    // Step 2: get the printwriter
    PrintWriter out = response.getWriter();

    HttpSession ssn = request.getSession();
    if(ssn != null)
    {
    out.println("Your session Id is : ");
    String ssnId = ssn.getId();
    out.println(ssnId);
    out.println("<br/><br/>");


    if (ssn.isNew()) {
    out.println("This is a NEW session");
    out.println("<br/><br/>");

    Random rando = new Random();
    int max = 10;
    int min = 0;
    int trials = 3;
    int random = new int[trials];
    int fiveColumns = new int[135];
    double horAvg = new double[27];

    System.out.println(fiveColumns.length);
    for(int i = 0; i<fiveColumns.length;i=i+5)
    {
    fiveColumns[i] = rando.nextInt(max)+1;
    fiveColumns[i+1] = rando.nextInt(max)+1;
    fiveColumns[i+2] = rando.nextInt(max)+1;

    // Horizontal Sum
    fiveColumns[i+3] = fiveColumns[i] + fiveColumns[i+1] + fiveColumns[i+2];
    // Horizontal Average
    fiveColumns[i+4] = fiveColumns[i+3]/3;

    }
    ssn.setAttribute("arrayOfOldSession", fiveColumns[0]);

    out.println("Length of Array: " + fiveColumns.length);
    out.println("<br/><br/>");

    out.println("<br/><br/>");
    System.out.println("Trial1" + "t" + "Trial2" + "t" + "Trial3" + "t" + "Horizontal Sum" + "t" + "Horizontal Avg");
    out.println("Trial1" + "&emsp;" + "Trial2" + "&emsp;" + "Trial3" + "&emsp;" + "Horizontal Sum" + "&emsp;" + "Horizontal Avg");
    out.println("<br/><br/>");
    for(int i = 0; i<fiveColumns.length;i=i+5)
    {
    System.out.println(fiveColumns[i] + "t" + fiveColumns[i+1] + "t" + fiveColumns[i+2] +
    "t" + fiveColumns[i+3] + "tt" + fiveColumns[i+4]);

    out.println("<table>");
    out.println("<tr><td>");
    out.println(fiveColumns[i] + "&emsp;&emsp;&emsp;" +
    fiveColumns[i+1] + "&emsp;&emsp;&emsp;" + fiveColumns[i+2] +
    "&emsp;&emsp;&emsp;" + fiveColumns[i+3] +
    "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;"
    + fiveColumns[i+4]);
    out.println("</tr></td>");

    }
    out.println("</table>");
    out.println("<br/><br/>");

    out.println("<html><body>");
    out.println("<h2>Hello Word</h2>");
    out.println("<hr>");
    out.println("Time on the server is: " + new java.util.Date());
    out.println("</html></body>");
    }
    else {
    out.print("This is an OLD session");
    out.println("<br/><br/>");

    int old = (int) ssn.getAttribute("arrayOfOldSession");

    out.println(old);

    System.out.println(old[0]);
    }

    }
    else
    {
    out.println("Your session is not created yet");
    }


    Basically what I'm trying to do is, that if it's an old session, I want to show the data that was displayed when the session was new. The only time I want new data is if the session is new. I'm trying to use setAttribute when the session is new and getAttribute when the session is old. Would I be able to store and grab array data like this? Thanks










    share|improve this question

























      0












      0








      0







      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // Step 1: set the content type
      response.setContentType("Text/html");

      // Step 2: get the printwriter
      PrintWriter out = response.getWriter();

      HttpSession ssn = request.getSession();
      if(ssn != null)
      {
      out.println("Your session Id is : ");
      String ssnId = ssn.getId();
      out.println(ssnId);
      out.println("<br/><br/>");


      if (ssn.isNew()) {
      out.println("This is a NEW session");
      out.println("<br/><br/>");

      Random rando = new Random();
      int max = 10;
      int min = 0;
      int trials = 3;
      int random = new int[trials];
      int fiveColumns = new int[135];
      double horAvg = new double[27];

      System.out.println(fiveColumns.length);
      for(int i = 0; i<fiveColumns.length;i=i+5)
      {
      fiveColumns[i] = rando.nextInt(max)+1;
      fiveColumns[i+1] = rando.nextInt(max)+1;
      fiveColumns[i+2] = rando.nextInt(max)+1;

      // Horizontal Sum
      fiveColumns[i+3] = fiveColumns[i] + fiveColumns[i+1] + fiveColumns[i+2];
      // Horizontal Average
      fiveColumns[i+4] = fiveColumns[i+3]/3;

      }
      ssn.setAttribute("arrayOfOldSession", fiveColumns[0]);

      out.println("Length of Array: " + fiveColumns.length);
      out.println("<br/><br/>");

      out.println("<br/><br/>");
      System.out.println("Trial1" + "t" + "Trial2" + "t" + "Trial3" + "t" + "Horizontal Sum" + "t" + "Horizontal Avg");
      out.println("Trial1" + "&emsp;" + "Trial2" + "&emsp;" + "Trial3" + "&emsp;" + "Horizontal Sum" + "&emsp;" + "Horizontal Avg");
      out.println("<br/><br/>");
      for(int i = 0; i<fiveColumns.length;i=i+5)
      {
      System.out.println(fiveColumns[i] + "t" + fiveColumns[i+1] + "t" + fiveColumns[i+2] +
      "t" + fiveColumns[i+3] + "tt" + fiveColumns[i+4]);

      out.println("<table>");
      out.println("<tr><td>");
      out.println(fiveColumns[i] + "&emsp;&emsp;&emsp;" +
      fiveColumns[i+1] + "&emsp;&emsp;&emsp;" + fiveColumns[i+2] +
      "&emsp;&emsp;&emsp;" + fiveColumns[i+3] +
      "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;"
      + fiveColumns[i+4]);
      out.println("</tr></td>");

      }
      out.println("</table>");
      out.println("<br/><br/>");

      out.println("<html><body>");
      out.println("<h2>Hello Word</h2>");
      out.println("<hr>");
      out.println("Time on the server is: " + new java.util.Date());
      out.println("</html></body>");
      }
      else {
      out.print("This is an OLD session");
      out.println("<br/><br/>");

      int old = (int) ssn.getAttribute("arrayOfOldSession");

      out.println(old);

      System.out.println(old[0]);
      }

      }
      else
      {
      out.println("Your session is not created yet");
      }


      Basically what I'm trying to do is, that if it's an old session, I want to show the data that was displayed when the session was new. The only time I want new data is if the session is new. I'm trying to use setAttribute when the session is new and getAttribute when the session is old. Would I be able to store and grab array data like this? Thanks










      share|improve this question













      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // Step 1: set the content type
      response.setContentType("Text/html");

      // Step 2: get the printwriter
      PrintWriter out = response.getWriter();

      HttpSession ssn = request.getSession();
      if(ssn != null)
      {
      out.println("Your session Id is : ");
      String ssnId = ssn.getId();
      out.println(ssnId);
      out.println("<br/><br/>");


      if (ssn.isNew()) {
      out.println("This is a NEW session");
      out.println("<br/><br/>");

      Random rando = new Random();
      int max = 10;
      int min = 0;
      int trials = 3;
      int random = new int[trials];
      int fiveColumns = new int[135];
      double horAvg = new double[27];

      System.out.println(fiveColumns.length);
      for(int i = 0; i<fiveColumns.length;i=i+5)
      {
      fiveColumns[i] = rando.nextInt(max)+1;
      fiveColumns[i+1] = rando.nextInt(max)+1;
      fiveColumns[i+2] = rando.nextInt(max)+1;

      // Horizontal Sum
      fiveColumns[i+3] = fiveColumns[i] + fiveColumns[i+1] + fiveColumns[i+2];
      // Horizontal Average
      fiveColumns[i+4] = fiveColumns[i+3]/3;

      }
      ssn.setAttribute("arrayOfOldSession", fiveColumns[0]);

      out.println("Length of Array: " + fiveColumns.length);
      out.println("<br/><br/>");

      out.println("<br/><br/>");
      System.out.println("Trial1" + "t" + "Trial2" + "t" + "Trial3" + "t" + "Horizontal Sum" + "t" + "Horizontal Avg");
      out.println("Trial1" + "&emsp;" + "Trial2" + "&emsp;" + "Trial3" + "&emsp;" + "Horizontal Sum" + "&emsp;" + "Horizontal Avg");
      out.println("<br/><br/>");
      for(int i = 0; i<fiveColumns.length;i=i+5)
      {
      System.out.println(fiveColumns[i] + "t" + fiveColumns[i+1] + "t" + fiveColumns[i+2] +
      "t" + fiveColumns[i+3] + "tt" + fiveColumns[i+4]);

      out.println("<table>");
      out.println("<tr><td>");
      out.println(fiveColumns[i] + "&emsp;&emsp;&emsp;" +
      fiveColumns[i+1] + "&emsp;&emsp;&emsp;" + fiveColumns[i+2] +
      "&emsp;&emsp;&emsp;" + fiveColumns[i+3] +
      "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;"
      + fiveColumns[i+4]);
      out.println("</tr></td>");

      }
      out.println("</table>");
      out.println("<br/><br/>");

      out.println("<html><body>");
      out.println("<h2>Hello Word</h2>");
      out.println("<hr>");
      out.println("Time on the server is: " + new java.util.Date());
      out.println("</html></body>");
      }
      else {
      out.print("This is an OLD session");
      out.println("<br/><br/>");

      int old = (int) ssn.getAttribute("arrayOfOldSession");

      out.println(old);

      System.out.println(old[0]);
      }

      }
      else
      {
      out.println("Your session is not created yet");
      }


      Basically what I'm trying to do is, that if it's an old session, I want to show the data that was displayed when the session was new. The only time I want new data is if the session is new. I'm trying to use setAttribute when the session is new and getAttribute when the session is old. Would I be able to store and grab array data like this? Thanks







      servlets setattribute getattribute






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 18:27









      BayCode

      276




      276





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399281%2fkeep-data-from-same-session-in-servlets%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53399281%2fkeep-data-from-same-session-in-servlets%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft