无法访问.jsp文件中导入的.java类中的变量[重复]

r1wp621o  于 5个月前  发布在  Java
关注(0)|答案(1)|浏览(74)

此问题在此处已有答案

How do you import classes in JSP?(6个回答)
25天前关闭
我在将类导入到.jsp文件时遇到问题。当我尝试导入时,程序仍在工作,但我无法访问变量。我正在尝试将GameServlet.java类导入到game.jsp中。
导入类似:

<%@ page import="com.example.TextGame, com.example.GameServlet, java.util.Scanner" %>

字符串
由于某种原因,它不工作,
该项目的结构是:

  • 演示
  • src
  • 主要
  • Java
  • com.example
  • GameServlet.java
  • TextGame.java
  • 资源
  • webapp
  • WEB-INF
  • web.xml
  • game.jsp
  • index.jsp
  • 目标
  • pom.xml

我的.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>

javaclasses:
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文件:
game.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" %>


但它不起作用

55ooxyrt

55ooxyrt1#

不幸的是,你尝试的import并不能解决你描述的/通过查看你的代码可见的问题。你最好删除这些不必要的报告。
GameServlet正在从HttpSession访问数据,但数据从未存储在那里。
看看用户是如何浏览应用程序的,最初他在index.jsp上,这使得浏览器显示一个表单,用户填写他的用户名,然后在提交时,该用户名被发送到下一个URL:GameServlet
GameServlet现在可以将提交的用户名读取为request.getParameter("playerName")并将其存储为getHttpSession().setAttribute("playerName", request.getParameter("playerName"))
只有在将属性存储在会话中之后,后续的页面/servlet才能再次读取它。因此,您必须更改您的GameServlet,或者创建另一个servlet/JSP来存储会话中的玩家名。

相关问题