我在将类导入 .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" %>
但它不起作用:(
问题是你的变量范围。
当您导入一个类(导入到另一个类中,本质上 JSP 也被编译成 servlet 类)时,您可以在代码中使用该类。
但是访问该类并不意味着您已经拥有实例。因此,您必须运行构造函数才能获取实例。即使这样,您也只能访问部分资源,因为字段和方法可以标记为私有、受保护或公共。
更重要的是,您在 GameServlet.doPost() 中显示的变量是仅存在于该方法中的临时变量。从外部任何地方都可以访问它们。