0

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.Error: Unresolved compilationproblem: Duplicate local variable cart.

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 The full stack trace of the root cause is available in the Apache Tomcat/7.0.53 logs.


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>

the 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
    }

}
11
  • At wich line you got the error? Commented Jun 14, 2014 at 22:07
  • Can you give the full stack trace? And which servlet container are you using? Tomcat? What version? Commented Jun 14, 2014 at 22:23
  • @AlvinThompson using Tomcat 7.0 Commented Jun 14, 2014 at 22:46
  • Please also say what class the doGet method above is in, and add the relevant sections of the web.xml file (servlet and servlet-mapping). Thanks. Commented Jun 15, 2014 at 17:30
  • So the class is Session, I presume. Let me think on it... Commented Jun 15, 2014 at 17:36

1 Answer 1

1

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.

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?

Sign up to request clarification or add additional context in comments.

8 Comments

+1 - Also look at the generated "showCart.java" file to see what the Java compiler is actually complaining about.
@StephenC?! Wow, I watch your show all the time! Good luck with The Late Show. :)
@StephenC: If you're not from the US, there's a famous person named Stephen Colbert.
Alvin - it might surprise you to know that people from outside the US know a lot about US culture.
s/we're/think we are/p :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.