Я начал изучать JavaEE и программирование в целом. На данный момент я создал 6 различных JSP-программ. Мне это нравится, и теперь я хочу сделать что-то лучше. Я понимаю, что мне нужно уменьшить количество кода в JPS. Я включаю общую программу в JSP-форму и хотел бы указать указатели на то, какие части должны идти туда, где внутри сервлета или как смотреть на это (я должен сделать кучу классов, а затем использовать сервлет, чтобы собрать их или запустить все с помощью сервлет: /). Я помещаю свои первоначальные мысли в код с каждым разделом. Пожалуйста, не передавайте мне ответы, мне нужно учиться, но, пожалуйста, заставьте меня двигаться, не останавливаясь.совет код перехода в JSP на сервлет
<%@ page language="java" import ="java.util.*" import="java.math.*" %>
<html>
<head><title>TicTacToe</title></head>
<body>
<h1>TicTacToe</h1>
<% // 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<3; r++)
for (int c=0; c<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
}
%>
<% // All things dealing with display I am thinking Servlet under processRequest
if (request.getParameter("reset") != null) {
for (int r=0; r<3; r++)
for (int c=0; c<3; c++)
tiles[r][c] = ' ';
winner = new String("");
}
%>
<% // 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<3; r++) {
for (int c=0; c<3; c++) {
if (request.getParameter("B" + r + c) != null && 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<3; r++) {
if (tiles[r][0]=='X' && tiles[r][1]=='X' && tiles[r][2] == 'X') {
winner = new String("X");
}
}
for (int c=0; c<3; c++) {
if (tiles[0][c]=='X' && tiles[1][c]=='X' && tiles[2][c] == 'X') {
winner = new String("X");
}
}
if (tiles[0][0]=='X' && tiles[1][1]=='X' && tiles[2][2] == 'X') {
winner = new String("X");
}
if (tiles[0][2]=='X' && tiles[1][1]=='X' && tiles[2][0] == 'X') {
winner = new String("X");
}
}
// same as with "X" player
if (didX && winner.equals("")) {
boolean playable = false;
for (int r=0; r<3; r++) for (int c=0; c<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<3; r++) {
if (tiles[r][0]=='O' && tiles[r][1]=='O' && tiles[r][2] == 'O') {
winner = new String("O");
}
}
for (int c=0; c<3; c++) {
if (tiles[0][c]=='O' && tiles[1][c]=='O' && tiles[2][c] == 'O') {
winner = new String("O");
}
}
if (tiles[0][0]=='O' && tiles[1][1]=='O' && tiles[2][2] == 'O') {
winner = new String("O");
}
if (tiles[0][2]=='O' && tiles[1][1]=='O' && tiles[2][0] == 'O') {
winner = new String("O");
}
}
}
%>
<% // 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);
%>
<form action="TicTacToe2.jsp" method="post">
<table border="1" >
<tbody>
<% // This goes in the servlet I am sure
for (int r=0; r<3; r++) {
out.println("<tr>");
for (int c=0; c<3; c++) {
out.println("<td><input type=\"submit\" name=\"B" + r + c + "\"" +
" value=\"" + tiles[r][c] + "\"></td>");
}
out.println("</tr>");
}
%>
</tbody>
</table>
<% // Announce winner
if (!winner.equals("")) {
out.println("<p>" + winner + " Won!</p>");
}
%>
<p><input type="submit" name="reset" value="Reset"></p>
</form>
</body>
</html>
Спасибо, это то, что я ищу, кажется, все хорошие программисты говорят, что просто прыгайте. Я не могу этого сделать, мне нужна точка отсчета. Я ценю ваше время, чтобы помочь. –