java.lang.Error:JSP中发生无法解决的编译错误 [英] java.lang.Error: Unresolved compilation error occuring in JSP

查看:82
本文介绍了java.lang.Error:JSP中发生无法解决的编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教程中编写了相同的代码后,我试图遵循有关JSP和eve的教程,我仍然收到以下错误.

I am trying to follow a tutorial on JSP and eve after writing the same code in the tutorial i still receive following error.

java.lang.错误:未解决的编译问题:本地重复 可变购物车.

java.lang.Error: Unresolved compilationproblem: Duplicate local variable cart.

我正在尝试使用以下代码运行servlet.

I am trying to run the servlet with the following code.

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)

note可以在Apache Tomcat/7.0.53日志中找到根本原因的完整堆栈跟踪.

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.53 logs.

Apache Tomcat/7.0.53

Apache Tomcat/7.0.53

showcart.jsp:

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

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,我认为它可以很好地编译.

Since you're getting the error at runtime and not compile time, I don't think the issue has to do directly (if at all) with the code you've shown. That code was compiled when packaging the WAR file and not while the WAR file was deployed, and I assume it compiled fine if you got a WAR.

我猜这个重复的变量是在JSP文件中定义的,该文件恰好还有一个名为"cart"的变量. JSP文件通常在第一个请求时在运行时即时"编译.如果查看showcart.jsp,应该会找到真正的原因.堆栈跟踪似乎支持此操作,您可以通过将doGet方法中的cart重命名为其他内容来验证我的说法-错误仍将显示"cart".如果您在showcart.jsp中看不到原因,可以张贴其内容吗?

I'm guessing this duplicate variable is defined in the JSP file, which happens to also have a variable named "cart". JSP files are compiled "on the fly" at runtime, normally upon the first request. If you look in showcart.jsp you should see the true cause. The stack trace seems to support this, and you can verify what I say by renaming cart in the doGet method to something else--the error will still say "cart". If you don't see the cause in showcart.jsp, can you post its contents please?

这篇关于java.lang.Error:JSP中发生无法解决的编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆