Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to reference new class in jsp function

Posted on 2011-09-19
5
Medium Priority
?
374 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 28

Accepted Solution

by:
rrz earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

783 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