title: advice moving code in JSP to a Servlet
body: <p>I have been starting to learn JavaEE and programming in general. I have created 6 different JSP programs so far. I am getting comfortable with it and now want to do things better. I understand that I need to reduce the amount of code in my JPS. I am including a common program in JSP form and would like pointers on what parts should go where within the servlet or how to look at this (should I make a bunch of classes then use the servlet to put them together or run the whole thing with a servlet :/).
  I am putting my initial thoughts in the code with each section.
  Please don't hand me answers, I need to struggle to learn - but please get me moving not staying stagnate. </p>
  
  <pre><code>&lt;%@ page language="java" import ="java.util.*" import="java.math.*" %&gt;
  
  &lt;html&gt;
  
  &lt;head&gt;&lt;title&gt;TicTacToe&lt;/title&gt;&lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;h1&gt;TicTacToe&lt;/h1&gt;
  
  &lt;% // game state variables to include in the servlet I think
  char[][] tiles; // the board
  String winner;  
  tiles = (char[][]) session.getAttribute("tiles");
  winner = (String)session.getAttribute("winner"); 
  if (tiles == null || winner == null) {
  tiles = new char[3][3];
  for (int r=0; r&lt;3; r++) 
    for (int c=0; c&lt;3; c++) 
      tiles[r][c] = ' ';
    winner = new String(""); //not too sure how this should change, I am thinking I would need a class for "winner" maybe
    }
    %&gt;
  
    &lt;%  // All things dealing with display I am thinking Servlet under processRequest 
    if (request.getParameter("reset") != null) {
  for (int r=0; r&lt;3; r++) 
    for (int c=0; c&lt;3; c++) 
      tiles[r][c] = ' ';
  winner = new String("");
    }
    %&gt;
  
    &lt;%  // Pretty sure this needs a person class, maybe put both this one and the "O" player in the same class. 
   boolean didX = false;
   if (winner.equals("")) {
    for (int r=0; r&lt;3; r++) { 
    for (int c=0; c&lt;3; c++) {
      if (request.getParameter("B" + r + c) != null &amp;&amp; tiles[r][c] == '     ') {
        tiles[r][c] = 'X';
        didX = true;
      }
     }
    }
   } 
   if (didX) { //thinking ether a winner class to check both or run this in the servlet
  // see if "X" player won yet
  for (int r=0; r&lt;3; r++) {
    if (tiles[r][0]=='X' &amp;&amp; tiles[r][1]=='X' &amp;&amp; tiles[r][2] == 'X') {
       winner = new String("X");
    }
    }
    for (int c=0; c&lt;3; c++) {
    if (tiles[0][c]=='X' &amp;&amp; tiles[1][c]=='X' &amp;&amp; tiles[2][c] == 'X') {
       winner = new String("X");
    }
     }
     if (tiles[0][0]=='X' &amp;&amp; tiles[1][1]=='X' &amp;&amp; tiles[2][2] == 'X') {
     winner = new String("X");
     }
     if (tiles[0][2]=='X' &amp;&amp; tiles[1][1]=='X' &amp;&amp; tiles[2][0] == 'X') {
    winner = new String("X");
    }
    }
  
    // same as with "X" player
    if (didX &amp;&amp; winner.equals("")) {
    boolean playable = false;
    for (int r=0; r&lt;3; r++) for (int c=0; c&lt;3; c++) {
    if (tiles[r][c] == ' ') {
      playable = true;
    }
    }
    if (playable) {
    boolean didO = false;
    while (!didO) {
      int r = (int) (Math.random() * 3);
      int c = (int) (Math.random() * 3);
      if (tiles[r][c] == ' ') {
        tiles[r][c] = 'O';
        didO = true;
      }
      }
  
       // same as with "X" 
      for (int r=0; r&lt;3; r++) {
      if (tiles[r][0]=='O' &amp;&amp; tiles[r][1]=='O' &amp;&amp; tiles[r][2] == 'O') {
         winner = new String("O");
      }
      }
      for (int c=0; c&lt;3; c++) {
      if (tiles[0][c]=='O' &amp;&amp; tiles[1][c]=='O' &amp;&amp; tiles[2][c] == 'O') {
        winner = new String("O");
        }
      }
      if (tiles[0][0]=='O' &amp;&amp; tiles[1][1]=='O' &amp;&amp; tiles[2][2] == 'O') {
       winner = new String("O");
       }
      if (tiles[0][2]=='O' &amp;&amp; tiles[1][1]=='O' &amp;&amp; tiles[2][0] == 'O') {
       winner = new String("O");
      }
      }
      }
       %&gt;
  
       &lt;% // I am very lost on where this goes in the servlet, I guess that depends on how it gets laid out
       session.setAttribute("tiles", tiles);
       session.setAttribute("winner", winner);
       %&gt;
  
       &lt;form action="TicTacToe2.jsp" method="post"&gt;
  
       &lt;table border="1" &gt;
  
       &lt;tbody&gt;
  
       &lt;%  // This goes in the servlet I am sure
       for (int r=0; r&lt;3; r++) {
       out.println("&lt;tr&gt;");
       for (int c=0; c&lt;3; c++) {
       out.println("&lt;td&gt;&lt;input type=\"submit\" name=\"B" + r + c + "\"" +
      " value=\"" + tiles[r][c] + "\"&gt;&lt;/td&gt;");
         }
        out.println("&lt;/tr&gt;");
       }
       %&gt;
  
       &lt;/tbody&gt;
  
       &lt;/table&gt;
  
        &lt;%  // Announce winner
        if (!winner.equals("")) {
        out.println("&lt;p&gt;" + winner + " Won!&lt;/p&gt;");
        }
        %&gt;
  
        &lt;p&gt;&lt;input type="submit" name="reset" value="Reset"&gt;&lt;/p&gt;
  
        &lt;/form&gt;
  
        &lt;/body&gt;
  
         &lt;/html&gt;
  </code></pre>