我在将类导入 .jsp 文件时遇到问题。当我尝试导入时,程序继续运行,但无法访问变量。我正在尝试将 GameServlet.java 类导入到 game.jsp 中。
导入如下:
<%@ page import="com.example.TextGame, com.example.GameServlet, java.util.Scanner" %>
由于某种原因它不起作用
项目结构为:
我的 .xml 看起来像这样:
xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- Servlet Mapping -->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
</web-app>
java类:
GameServlet.java
package com.example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/GameServlet")
public class GameServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String playerName = (String) session.getAttribute("playerName");
session.setAttribute("game", new TextGame());
response.sendRedirect("game.jsp");
}
}
TextGame.java
package com.example;
public class TextGame {
private String currentLocation;
public TextGame() {
this.currentLocation = "start";
}
public String getCurrentLocation() {
return currentLocation;
}
public void handlePlayerInput(String input) {
if (input.equalsIgnoreCase("go north")) {
currentLocation = "north_location";
} else if (input.equalsIgnoreCase("go south")) {
currentLocation = "south_location";
}
}
}
jsp 文件:
游戏.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.example.TextGame, com.example.GameServlet, java.util.Scanner" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Текстова гра - Гравець: ${sessionScope.playerName}</title>
</head>
<body>
<h1>Вітаємо, ${sessionScope.playerName}!</h1>
<p>Ви знаходитесь в ${sessionScope.game.currentLocation}.</p>
<form action="GameServlet" method="post">
<label for="playerInput">Введіть команду:</label>
<input type="text" id="playerInput" name="playerInput" required>
<input type="submit" value="Виконати">
</form>
</body>
</html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Текстова гра</title>
</head>
<body>
<form action="GameServlet" method="post">
<label for="playerName">Введіть ваше ім'я:</label>
<input type="text" id="playerName" name="playerName" required>
<input type="submit" value="Почати гру">
</form>
</body>
</html>
我尝试导入:
<%@ page import="com.example.TextGame, com.example.GameServlet, java.util.Scanner" %>
或:
<%@ page import="com.example.TextGame" %>
<%@ page import="com.example.GameServlet" %>
<%@ page import="java.util.Scanner" %>
但是不起作用
您的 GameServlet 正在从 HttpSession 访问数据,但数据从未存储在那里。
看看用户将如何使用该应用程序。最初他在index.jsp 上,这使得浏览器显示一个表单。用户填写他的用户名,然后在提交时将该用户名发送到下一个网址:GameServlet
GameServlet 现在可以将提交的用户名读取为
request.getParameter("playerName")
并将其存储为 getHttpSession().setAttribute("playerName", request.getParameter("playerName"))
只有在此之后后续页面/servlet 才能够读取会话属性。