【发布时间】:2021-09-12 20:17:45
【问题描述】:
这是我第一次尝试使用 Tomcat 使用 Java Servlet 创建网页,但每当我尝试将参数传递给 calculator.jsp 文件时,我都会将其变为空白,仅显示“名称: " 没有 ${abc} 变量的calculator.jsp 中的文本我试图从java 类传输(“字符串”文本)。我不知道这是怎么回事。
如果有人可以帮助我,因为我陷入困境并且找不到任何解决方案
这是 index.jsp
的代码<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="com.example.*"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %>
</h1>
NAME: <%= ((String)request.getAttribute("abc")) %>
ll : ${requestScope.abc}
<br/>
<form action="calculator.jsp" method="post">
<button type="submit">Button</button>
</form>
</body>
</html>
calculator.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" import="com.example.AirlineReservation.HelloServlet" %>
<html>
<head>
<title>Title</title>
</head>
<body>
NAME: <%= ((String)request.getAttribute("abc")) %>
ll : ${requestScope.abc}
</body>
</html>
HelloServlet.java
package com.example.AirlineReservation;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
@WebServlet(name="/calculator")
public class HelloServlet extends HttpServlet {
private String message;
public void init() {
message = "Testing!";
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String s="string";
request.setAttribute("abc",s);
request.getRequestDispatcher("/calculator.jsp").forward(request,response);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
}
public void destroy() {
}
}
【问题讨论】: