Solved

Dynamically loading HTML select without using a form

Posted on 2004-04-21
13
544 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:PaulS_III
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
13 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10883209
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.

http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_20953755.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
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 10887615
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
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 10887857
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10888219
:) 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
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 10888272
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
0
 
LVL 13

Expert Comment

by:copyPasteGhost
ID: 10888363
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
0
 
LVL 27

Expert Comment

by:rrz
ID: 10889416
>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
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 10890487
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?
0
 
LVL 27

Expert Comment

by:rrz
ID: 10891745
> 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
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 10891786
thanks for the input rrz
0
 
LVL 27

Expert Comment

by:rrz
ID: 10891801
>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 .
0
 
LVL 13

Accepted Solution

by:
copyPasteGhost earned 250 total points
ID: 10891997
>>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.

I would do this.. have a javascript function to reload the page and pass along the currently selected item from the listbox

to do that you would have to redirect to..

myJsp.jsp?currState='formName.selectBoxName.TheMethodToGetTheCurrentValue'

hope that helps,
Ghost
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 10902291
Thanks for all the help. I think I have this figured out.

Many Thanks

Paul
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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