Solved

Dynamically loading HTML select without using a form

Posted on 2004-04-21
13
529 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
  • 6
  • 4
  • 3
13 Comments
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:copyPasteGhost
Comment Utility
:) 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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 27

Expert Comment

by:rrz
Comment Utility
>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
Comment Utility
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
Comment Utility
> 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
Comment Utility
thanks for the input rrz
0
 
LVL 27

Expert Comment

by:rrz
Comment Utility
>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
Comment Utility
>>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
Comment Utility
Thanks for all the help. I think I have this figured out.

Many Thanks

Paul
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
method inner class 6 80
servlet init param and contect param 3 53
dateFashion java challenge 9 128
TreeSet comparator example 7 87
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Find out what Office 365 Transport Rules are, how they work and their limitations managing Office 365 signatures.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

19 Experts available now in Live!

Get 1:1 Help Now