Solved

how to reference new class in jsp function

Posted on 2011-09-19
5
359 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now