gpt4 book ai didi

java - servlet 到 JSP 以及 JSP 到 servlet 上的变量

转载 作者:行者123 更新时间:2023-12-01 09:32:21 25 4
gpt4 key购买 nike

我有一个作业,是使用 jsp 和 servlet 创建剪刀石头布的基本练习。在index.html 上,我创建了一个表单来接收“Rock”、“Paper”或“Scissors”。这将进入一个 Servlet,用于计算回合数、失败数、胜利数和平局数。

一旦您提交了您的移动,无论是石头、布还是剪刀,您将被引导到一个 JSP,该 JSP 将显示记分牌和服务器的移动,但在此 JSP 上有一个表单,其中一个是返回索引的链接.html ,回合、胜利、失败计数器继续计数,然后有一个按钮,它是一个重置按钮,应该将所有值变成 0,这是 jsp 上的不同形式,具有不同的操作。我的老师建议制作两个 servlet,但他说如果我能制作一个 servlet 就可以了,但是制作两个 servlet 会更容易。

摘要:如何从一个 servlet 重置另一个 servlet 上的变量?

index.html:

<html>
<head>
<title>Actividad 4</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Rock Paper Scissors Game</h1>
<p>Choose your attack: </p>
<form method="post" action="move.do">
<select name="attack">
<option value="1">Rock</option>
<option value="2">Scissors</option>
<option value="3">Paper</option>
</select>
<button type="submit">Attack!</button>
</form>
</body>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd” 版本=“3.1”> 30 Servlet 行为 4 服务程序 Servlet 行为 4 /move.do

<servlet>
<servlet-name>Servlet2 Act 4</servlet-name>
<servlet-class>Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet2 Act 4</servlet-name>
<url-pattern>/reset.do</url-pattern>
<init-param>
<param-name>turn</param-name>
<param-value>1</param-value>
</init-param>
</servlet-mapping>

第一个 servlet:

import java.io.IOException;

import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author maple
*/
@WebServlet(name = "Servlet Act 4", urlPatterns = {"/Servlet"})
public class Servlet extends HttpServlet {
static int turn = 1;
static int wins = 0;
static int defeat = 0;
static int ties = 0;

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Servlet at " + request.getContextPath() + "</h1>");
out.println("</body>");
out.println("</html>");
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int attack = Integer.parseInt(request.getParameter("attack"));
int move = randMove();
String counter = "";
String decision = "";
//Checa el metodo randMove(), obtengo un numero aleatorio entre 1 a 3 que lo convierto en
//Rock = Piedra
//Scissor = Tijera
//Paper = Papel
switch(move) {
case 1: counter = "Rock";
break;
case 2: counter = "Scissor";
break;
case 3: counter = "Paper";
break;
}

//Este if comparo que si los dos son iguales, el piedra papel o tijera del servidor
//Y el piedra papel tijera del jugador para determinar el ganador.
if(attack == move) {
ties++;
decision = "It's a tie!";
} else if (attack == 1) {
switch(move){
case 2: decision = "Player wins!";
wins++;
break;
case 3: decision = "Server wins!";
defeat++;
break;
}
} else if (attack == 2) {
switch(move){
case 1: decision = "Server wins!";
defeat++;
break;
case 3: decision = "Player wins!";
wins++;
break;
}
} else if (attack == 3) {
switch(move) {
case 1: decision = "Player wins!";
wins++;
break;
case 2: decision = "Server wins!";
defeat++;
break;
}
}

//Esto es para mantener cuenta que turno es, que movimiento hizo, quien gano, cuantas veces
//gano quien y perdio quien.
request.setAttribute("turn", turn);
request.setAttribute("counter", counter);
request.setAttribute("winner", decision);
request.setAttribute("ties", ties);
request.setAttribute("wins", wins);
request.setAttribute("defeat", defeat);
if(turn < 5) {
turn++;
} else {
turn = 1;
ties = 0;
wins = 0;
defeat = 0;
}
RequestDispatcher view =
request.getRequestDispatcher("Scoreboard.jsp");
view.forward(request, response);
}

/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

public static int randMove() {
int min = 1;
int max = 3;
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;

return randomNum;
}

}

JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Scoreboard</title>
</head>
<body>
<h1>Server: <%=request.getAttribute("counter")%></h1>
<h3>Round <%=request.getAttribute("turn")%> of 5</h3>
<p><%=request.getAttribute("winner")%></p>
<table>
<thead>
<tr>
<td></td>
<td>Win</td>
<td>Defeat</td>
<td>Tie</td>
</tr>
</thead>
<tbody>
<tr>
<td>Player</td>
<td><%=request.getAttribute("wins")%></td>
<td><%=request.getAttribute("defeat")%></td>
<td><%=request.getAttribute("ties")%></td>
</tr>
<tr>
<td>Server</td>
<td><%=request.getAttribute("defeat")%></td>
<td><%=request.getAttribute("wins")%></td>
<td><%=request.getAttribute("ties")%></td>
</tr>
</tbody>
</table>
<form method="get" action="reset.do">
<a type="button" href="index.html">Next round</button></a>
<button type="submit">Reset</button>
</form>
</body>
</html>

最佳答案

变量turn、wins、feat 和tie都是静态的,所以如果你想在另一个servlet中进行更改,只需将它们声明为public,然后用如下代码修改它们

 Servlet.turn = 0 

但这不是维护 servlet 状态的正确方法,如果您跨请求声明,您应该使用如下代码将它们保存到 session 中:

request.getSession().setAttribute("turn",0);

关于java - servlet 到 JSP 以及 JSP 到 servlet 上的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39303376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com