Solved

how to reference new class in jsp function

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 59
servlet example 11 49
Configure a Bean in an XML file 4 42
ejb on wildfly 5 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

830 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