Solved

how to reference new class in jsp function

Posted on 2011-09-19
5
364 Views
Last Modified: 2012-08-13
I am defining a class in the body of my jsp program, and I have a list that uses this class:

class claimData {
    public String MemberId;
    public String groupId;
    public String claimNumber;
    public int count;

    public claimData(String m, String g, String c)
    {
        MemberId = m;
        groupId = g;
        claimNumber = c;
        count = 1;
    }
}

List<Object> claimList = new ArrayList<claimData>();

I also have a function that I want to use to search this list:

<%!
static void  addClaim(List<Object> claimList, String claimNumber, String memberId, String memberGroupId)
{
    // Search for retiree

    for (int i=0; i < claimList.size(); i++)
    {
        claimData cl = (claimList) list.get(i);

        if (dd.memberId.equals(memberId) && dd.groupId.equals(memberGroupId) &&
            dd.claimNumber.equals(claimNumber))
        {
            dd.count++;
            return;
        }

        claimList.add(new claimData(memberId, memberGroupId, claimNumber));
}
%>

but, the function can't see the class. I get the error: "claimData cannot be resolved to a type"

how do I fix this?
0
Comment
Question by:jmarkfoley
  • 3
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36563889

check the syntax in jsp - like the author of this question in the similar case (see his response below the solution):

http://stackoverflow.com/questions/2226218/defining-a-class-in-a-jsp
0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 36564691
Here is an example of working code.
<%@ page import="java.util.*" %>
<%!
  class ClaimData {
    public String MemberId;
    public String groupId;
    public String claimNumber;
    public int count;

    ClaimData(String m, String g, String c)
    {
        MemberId = m;
        groupId = g;
        claimNumber = c;
        count = 1;
    }
   public String toString() 
   { return MemberId;}
 }
 void  addClaim(List<ClaimData> claimList, String claimNumber, String memberId, String memberGroupId)
{
    // Search for retiree

    for (int i=0; i < claimList.size(); i++)
    {
        ClaimData cl = (ClaimData) claimList.get(i);
       // I don't know about dd, so I commented it out for test
      //  if (dd.memberId.equals(memberId) && dd.groupId.equals(memberGroupId) &&
       //     dd.claimNumber.equals(claimNumber))
      //  {
      //      dd.count++;
       //     return;
       // }
    }
    claimList.add(new ClaimData(memberId, memberGroupId, claimNumber));
 
}
%>
<%  
  List<ClaimData> claimList = new ArrayList<ClaimData>();
  addClaim(claimList,"123","123","123");
  addClaim(claimList,"456","456","456");
%>   
<%=claimList%>

Open in new window

0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 36566981
for_yan: your program does work fine. I tried it. But when I copy/paste your exact code into my program it doesn't work. I even tried putting the class and function as the first thing in my program (rather than at the end) in case that mattered. The "body" of my jsp program doesn't even reference this class or the addClaim() function yet. I can't get the function to compile. I get the message:

An error occurred at line: 31 in the jsp file: /ERRPclaims.jsp
No enclosing instance of type ERRPclaims_jsp is accessible. Must qualify the allocation with an enclosing instance of type ERRPclaims_jsp (e.g. x.new A() where x is an instance of ERRPclaims_jsp).
28:           return;
29:       }
30:     }
31:     claimList.add(new ClaimData(memberId, memberGroupId, claimNumber));
32: }
33: %>

I can't quite figure out this "enclosing instance" business, nor why I didn't need one with your code.
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 36567279
Ah ha! I figured out the problem. I had to remove the "static" from the function definition:

"void addClaim()", not "static void addClaim(). Not sure exactly why this makes a difference, but it does. Also, I meant to say that I tested using rrz@871311's code, not for_yan's.
0
 
LVL 1

Author Closing Comment

by:jmarkfoley
ID: 36567285
btw - the dd.'s were errors. They should have been cl.'s. I originally cloned the code from elsewhere.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question