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

r1wp621o  于 2024-01-04  发布在  Java
关注(0)|答案(1)|浏览(259)

此问题在此处已有答案

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

  1. <%@ 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文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <!-- Servlet Mapping -->
  7. <servlet>
  8. <servlet-name>MyServlet</servlet-name>
  9. <servlet-class>com.example.MyServlet</servlet-class>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>MyServlet</servlet-name>
  13. <url-pattern>/myservlet</url-pattern>
  14. </servlet-mapping>
  15. </web-app>

javaclasses:
GameServlet.java

  1. package com.example;
  2. import java.io.IOException;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebServlet;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. @WebServlet("/GameServlet")
  10. public class GameServlet extends HttpServlet {
  11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  12. HttpSession session = request.getSession();
  13. String playerName = (String) session.getAttribute("playerName");
  14. session.setAttribute("game", new TextGame());
  15. response.sendRedirect("game.jsp");
  16. }
  17. }

TextGame.java

  1. package com.example;
  2. public class TextGame {
  3. private String currentLocation;
  4. public TextGame() {
  5. this.currentLocation = "start";
  6. }
  7. public String getCurrentLocation() {
  8. return currentLocation;
  9. }
  10. public void handlePlayerInput(String input) {
  11. if (input.equalsIgnoreCase("go north")) {
  12. currentLocation = "north_location";
  13. } else if (input.equalsIgnoreCase("go south")) {
  14. currentLocation = "south_location";
  15. }
  16. }
  17. }

jsp文件:
game.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ page import="com.example.TextGame, com.example.GameServlet, java.util.Scanner" %>
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <meta charset="UTF-8">
  7. <title>Текстова гра - Гравець: ${sessionScope.playerName}</title>
  8. </head>
  9. <body>
  10. <h1>Вітаємо, ${sessionScope.playerName}!</h1>
  11. <p>Ви знаходитесь в ${sessionScope.game.currentLocation}.</p>
  12. <form action="GameServlet" method="post">
  13. <label for="playerInput">Введіть команду:</label>
  14. <input type="text" id="playerInput" name="playerInput" required>
  15. <input type="submit" value="Виконати">
  16. </form>
  17. </body>
  18. </html>

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Текстова гра</title>
  7. </head>
  8. <body>
  9. <form action="GameServlet" method="post">
  10. <label for="playerName">Введіть ваше ім'я:</label>
  11. <input type="text" id="playerName" name="playerName" required>
  12. <input type="submit" value="Почати гру">
  13. </form>
  14. </body>
  15. </html>


我尝试导入如下:

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


或者:

  1. <%@ page import="com.example.TextGame" %>
  2. <%@ page import="com.example.GameServlet" %>
  3. <%@ 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来存储会话中的玩家名。

相关问题