0

Background: In my journey to become a good programmer I am creating a tic tac toe program in Java EE for web deployment. I have a good basic understanding of java and so started with the same program flow I had already. I moved it to a JSP (because that is what I had been working on). It is working fine there with some slight tweaks. Now I am getting it over to a servlet (because every post I read says that using JSP is not good and makes life hard on bigger programs).

Issue:

I have the board coming up, and it seems that the buttons I am using are good. However when I select one nothing happens. When I changed this from JSP to a servlet I got an error dealing with my Session.getAttribute. I removed it and all was well - minus not working. I think this could be my issue and so I am asking what am I doing wrong with it.

The error is saying "can not find symbol variable session" It asks me to create a field for session. If I remove this section then i need to initialize both "tiles" and "winner" but that puts them at null which is what I think my issue is.

If you want to see the entire code let me know and I will edit with it. Thank you for your time and the help.

public class TicTacToeServlet 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> Tic-Tac-Toe Boss");

  out.println("</HEAD>");
  out.println("<BODY>");


 char[][] tiles; // the board that will hold buttons
 String winner; 

 tiles = (char[][]) session.getAttribute("tiles");  //Here is the error
winner = (String)session.getAttribute("winner");    //Error - "can not find symbol Session" 
if (tiles == null || winner == null) {
tiles = new char[3][3];
for (int r=0; r<3; r++) 
  for (int c=0; c<3; c++) 
    tiles[r][c] = ' ';
winner = new String("");
}

This is how it looks when I have it display but not change the buttons on click:

  char[][] tiles = null; // the board that will hold buttons
  String winner = null; 

  // Initialize if not already present in the session (i.e. after reset is hit)

  if (tiles == null || winner == null) {
  tiles = new char[3][3];
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Luis Ramos
  • 529
  • 1
  • 6
  • 13

0 Answers0