Link to home
Start Free TrialLog in
Avatar of PaulS_III
PaulS_III

asked on

Dynamically loading HTML select without using a form

Hi all,
I'm diving into JSP head first without really knowing what I am doing to much, so please bear with me.

I am developing an small web app to be used within the company. The app will allow a user to select a state. From that selection the next drop down will be populated, from which the user can select a county within that state. This selection will then populate a text box with the directories that make up that county.

My welcome screen is a JSP page that populates the state drop down as it loads.  What I can't figure out is how to populate the county dropdown based on the state selected. I am using a bean to do all my database work, each function returns a Vector. I tried to create a scriptlet to call the next function from the bean to populate the county Vector but everytime I try to compile the JSP page I get the wonderful "Can't Resolve Symbol" error with the cute little carrot right under the beginning of my bean name.

I don't really want to use a form for this, simply because I am not sure how. If I did would my method be POST or GET?

At any rate, could someone please help me out here?

Thanks

Paul
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

post and get and the same.
only that get adds the information to the url whereas post hides it.

I go over how to use a bean in detail during this question, take a look at it.

https://www.experts-exchange.com/questions/20953755/jsp-to-bean-bean-to-database-bean-to-jsp-confirming-insert.html

I would use javascript for your state within country. I think that's what you meant ????

>>From that selection the next drop down will be populated, from which the user can select a county within that state.

you mean state within a country right?

the java script to do that...

var states = new Array("Choose a state","Alabama","Arizona","Arkansas","Alaska",
"California","Colorado","Connecticut","Delaware","Florida",
 "Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky",
 "Louisiana ","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri",
 "Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina",
 "North Dakota","Ohio","Oklahoma ","Oregon","Pennsylvania","Rhode Island","South Carolina",
 "South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington",
 "West Virginia","Wisconsin","Wyoming");

var provinces = new Array("Choose a province or territory","Alberta","British","Manitoba",
"New Brunswick","Newfoundland","Nova Scotia","Ontario","Prince Edward Island","Quebec",
"Saskatchewan","Northwest Territories","Nunavut","Yukon");

function canadaBox() {
  var i;
  document.client.provSta.options.length = 0;
  for(var i = 0;i < provinces.length;i++){
    var myNewOption = new Option(provinces[i],provinces[i]);
    document.client.provSta.options[i] = myNewOption;
  }
  selRadio = "canada";
  postalCode = true;
}

function usaBox() {
  var i;
  document.client.provSta.options.length = 0;
  for(i = 0;i < states.length;i++){
    var myNewOption = new Option(states[i],states[i]);
    document.client.provSta.options[i] = myNewOption;
  }
  selRadio = "usa";
  postalCode = false;
}

you can call the canadaBox function if you want the provinces to be loaded
you can call the usaBox function if you want to load the states.

I am accessing my select box like this.. document.client.provSta.METHOD
that should get you started.
If you need anything else
let me know,
Ghost
Avatar of PaulS_III
PaulS_III

ASKER

Hi Ghost.

No I do mean county with in the state. I'm sure you are aware, but each state is then subdivided into smaller political units. We compile phone book and land data based on these political subdivisions.

I understand what you are saying, about using javascript to help. However, when I try that I need to call a function in my bean to query the database for the counties of the selected state. The problem is when I compile my JSP page I get the Can't resolve Symbol error pointing to my bean name.

I'll keep plugging away. Any further information would be greatly appreciated.

Thanks for your reply

Paul
Let me explain a bit further.

I have my JSP page starting off like this:

<%@page contentType="text/html"%>
<%@page import="java.util.Vector"%>

<jsp:useBean id="bSelect" scope="session" class="CMPUpdate.SelectCriteria" />
<jsp:setProperty name="bSelect"  property="*" />

<--Declaration to use later on in the page -->
<%!
    String selectedState;
    Vector cState = new Vector();
    Vector cCounty = new Vector();
   
    public void collectCountyNames(String selState)
    {
        if (selState != null)
        {
            cCounty = (Vector)bSelect.getCountyName(selState); <- - - This is the problem line
        }
    }
%>

If I comment out the declaration, and do please excuse my use of scriptlet, is was using the wrong terminology, then my JSP works and populates the states select box. However, I cannot go on becasue I need the selected state to populate the county select box.

When I uncomment the declaration and try to compile or run then I get the "Cannot resolve symbol" and the carrot points to the "b" in bSelect.

I have tried changing the scope to page and application, but it still does not work.  I have gone over the code and I don't see anything that looks like it is causing a problem.  Are beans not global to the whole JSP?

I would really like to get this wrapped up by tomorrow afternoon, for no other reason than to move on. This is just the first phase of a huge project I am undertaking.

I do appreciate all comments.

Thanks
:) sorry I read country not county my bad


>> I have gone over the code and I don't see anything that looks like it is causing a problem.  Are beans not global to the whole JSP?
no not useless you set them so..

you must include this line..
<jsp:useBean id="bSelect" scope="session" class="CMPUpdate.SelectCriteria" />
at the top of all your jsp pages that use the bean

also are you sure that your .class files are in the right place?

WEB-INF/classes/CMPUpdate/SelectCriteria.class

that should be the location of your .class file.
Hope that helps,
Ghost
Yep I am sure. I use a method of the bean early on in the JSP to create the state select box.  But when I call it from my declaration is where I run into problems.  

What I am exploring now is simple loading my containers (Vectors, Maps) at the beginning of the JSP and then manipulate them from there using javascript.

what are your thoughts on that?

Paul
it's this "Cannot resolve symbol" thing that is disturbing...

does that method exist? are you sure it's returning the right datatype?
I noticed that you are casting to Vector...does that mean you are returning an object?

>What I am exploring now is simple loading my containers (Vectors, Maps) at the beginning of the JSP and then manipulate them from there using javascript.

That could work but does that mean that you are going to build arrays for all the county's within a state so that you have all the available options?

Another thing to keep in mind... JSP is a sever side language and as such you can't make calls to the server once the page is already severed, I think that is your problem. a work around would be to reload the page everytime a user clicks on a state. When the user clicks on the state you would have the page call itself and pass the statename as a parameter and then load the counties into the select box based on the state.

Cheers,
Ghost
Avatar of rrz
>Are beans not global to the whole JSP?
No, beans are not global. They are not instance variables. When the <jsp:useBean> tag is translated, a local variable is created within the  _jspService method.  If you are using Tomcat, then you can look at your translated file (look for yourpagename_jsp.java)  in it's work folder.    

>What I am exploring now is simple loading my containers (Vectors, Maps) at the beginning of the JSP and then manipulate them from there using javascript.  
Do you mean to say that you will download all the info for all counties in all states  when the page is initially called by the browser ? If you do that, then you have to make all the info available to the javascript.  Using  Ghost's last suggestion  would be easier.      rrz
Ghost,

Yes now I am understanding. I do need to reload the JSP after every selection. So when the user selects a state I want to go back and pull all the counties for that state. So in the onchange method of the "SELECT" how do I do that, without have the user click a submit button.

Yes the function does exist. as I type in the bean name and type the dot my methods come up and I can select the getCountyName method. I am returning vectors containing the data I need, hence the vector cast.

rrz: So what your saying is that when I use the <jsp:usebean> I should be able to reference it anywhere with in the JSP page I use that tag?

Again, the Cannot Resolve Symbol error only occurs within the declaration I have listed above. When I use it the bean method in another part of the page outside of the context of the declaration it works just fine. Can I not call bean methods inside of a declaration within the same JSP page where I create the bean?
> Can I not call bean methods inside of a declaration within the same JSP page where I create the bean?  
No, you can't. The bean is a local variable.
Please consider the following file called beanTest.jsp    

<%!   String instanceVar = "Hello Everybody"; %>
<jsp:useBean id="doc" class="pack1.testBean"/>
<%=doc.getDocumentType()%>        

The translated file (which is the source code for the class) has these lines ( I removed everything else.  

public final class beanTest_jsp {
      String instanceVar = "Hello Everybody";
      public void _jspService(HttpServletRequest request, HttpServletResponse response)
                                              throws java.io.IOException, ServletException {
         pack1.testBean doc = null;
         doc = (pack1.testBean) _jspx_page_context.getAttribute("doc", PageContext.SESSION_SCOPE);
         if (doc == null){
                          doc = new pack1.testBean();
                         _jspx_page_context.setAttribute("doc", doc, PageContext.SESSION_SCOPE);
         }
         out.print(doc.getDocumentType());
      }    
}


The container (Tomcat) will make a servlet object from beanTest.class .
The bean is created within the service method.
rrz
thanks for the input rrz
>The container (Tomcat) will make a servlet object from beanTest.class .    
I meant to say      
The container (Tomcat) will make a servlet object from beanTest_jsp.class .
ASKER CERTIFIED SOLUTION
Avatar of copyPasteGhost
copyPasteGhost
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all the help. I think I have this figured out.

Many Thanks

Paul