我正在尝试遵循 JSP 教程,在教程中编写相同的代码后,我仍然收到以下错误。
java.lang.Error: Unresolved compilationproblem: Duplicate local variable cart.
我正在尝试使用以下代码运行 servlet。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Cart cart = (Cart)session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
}
cart.setTotalItems(7);
session.setAttribute("cart", cart);
getServletContext().getRequestDispatcher("/showcart.jsp").forward(request,response);
}
HTTP Status 500 - Servlet execution threw an exception
--------------------------------------------------------------------------------
type Exception report
message Servlet execution threw an exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.Error: Unresolved compilation problem:
Duplicate local variable cart
demo.Session.doGet(Session.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
注意 Apache Tomcat/7.0.53 日志中提供了根本原因的完整堆栈跟踪。
<小时/>Apache Tomcat/7.0.53
showcart.jsp:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<%@ page import= "demo.*" %>
<% Cart cart= (Cart)session.getAttribute("cart");%>
Items in cart : <%= cart.getTotalItems() %>
</body>
</html>
web.xml:
<servlet>
<description></description>
<display-name>Session</display-name>
<servlet-name>Session</servlet-name>
<servlet-class>demo.Session</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Session</servlet-name>
<url-pattern>/Session</url-pattern>
</servlet-mapping>
</web-app>
session .java
package demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class Session
*/
public class Session extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Session() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
Cart cart = (Cart)session.getAttribute("cart");
if (cart == null) {
cart = new Cart();
}
cart.setTotalItems(7);
session.setAttribute("cart", cart);
getServletContext().getRequestDispatcher("/showcart.jsp").forward(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
最佳答案
由于您在运行时而不是编译时收到错误,因此我认为该问题与您所显示的代码没有直接关系(如果有的话)。该代码是在打包 WAR 文件时编译的,而不是在部署 WAR 文件时编译的,并且我认为如果您有 WAR,它编译得很好。
我猜测这个重复变量是在 JSP 文件中定义的,该文件恰好也有一个名为“cart”的变量。 JSP 文件在运行时“即时”编译,通常是在第一个请求时编译。如果您查看 showcart.jsp
,您应该会看到真正的原因。堆栈跟踪似乎支持这一点,您可以通过将 doGet
方法中的 cart
重命名为其他内容来验证我所说的 - 错误仍然会显示“cart”。如果您在 showcart.jsp
中没有看到原因,您可以发布其内容吗?
关于java.lang.错误: Unresolved compilation error occuring in JSP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24224703/