• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1111
  • Last Modified:

How to call a method and send values from a JSP page

I am trying to get values from a dropdown menu to dynamically populate the values in the next dropdown depending on the previous drop down's selection.  I have all of the dropdowns with all of their values populated from a jsp page.  In the select name tag, I have a call of an onChange event, but am confused about how to get values to pass to a method that will handle a query to use those values.  

First, I tried to pass these values into an onChange function in a separate javascript snippet, but then I found out javascript functions cannot accept referenced variables, only values.  What are my other options?  The only option I can think of is to create a seperate java servlet or a bean to handle this, but I know very little of how to set that up.  I am using Jdeveloper 9i, so creating the servlet or bean is easy to do, it's the calls and returns that are the problem.  Jdeveloper automatically creates a servlet with the doGet() method that is used to specifically receive information, so for test purposes, let's presume that is the name of the method I want to call, in java class Logic.

Now, given the onChange event, how do I call the method (say, from a java servlet) and send the values to it?  In addition, the servlet will be apart of the same package, but do I need to specify a path to send the values to the method, or will JSP do that automatically?

This is a snippet from my JSP page that includes the form and select dropdowns information.  The query listed as rs2 is the query that populates the dropdowns.  The onChange call is in the <Select> tag.  Thanks.

while (rs.next())
{
while (index < 14) { %>
<form action="POC.jsp?formname=<%= index %>&tabs=<%=tabimage%>" method="post" name="<%= index %>">
<%
for(question = 0; question <= 163; question++){
    breakApartSql = "Select Description, udcseq, udc FROM public_gdlp.udc where treeid= "+index+" and udcseq= "+question+"";
 rs2 = stmt.executeQuery(breakApartSql);
while (rs2.next())
{
%>  
 <tr>
    <td align="right">
<%out.println(rs2.getString("UDC")+"-"+ rs2.getString("Description"));%>

    </td>
    <td><select name="<%= rs2.getString("UDCSeq")%>" onChange="onChange()" style="font-family: Arial; font-size: 8pt;">"
0
gdlp2004
Asked:
gdlp2004
  • 7
  • 7
  • 4
1 Solution
 
KuldeepchaturvediCommented:
you will have to submit your page to your servlet/jsp and then do a request.getParameter("varname"); to get these values
0
 
KuldeepchaturvediCommented:
i.e. you are posting the above form to POC.jsp
now this jsp can read the values from the request and can do what ever you want to do with it
0
 
kennethxuCommented:
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gdlp2004Author Commented:
Where do you put the request.getParameter, and first off, how do you call the method to receive them?
0
 
KuldeepchaturvediCommented:
doPost will get called automatically so you don't have to specify any method to get called explicitly..
although you can use your methods inside doPost if you want to
0
 
gdlp2004Author Commented:
We tried to use request.getParameter before, but the problem with that approach is that the selections of the dropdowns would be reset if the page refreshed.  There are 163 dropdowns total, and some of these dropdowns are constrained by the selection of other dropdowns.  For example, the selections on dropdown "6" can affect the selections on dropdown "72".  We have these constraints hardcoded into our database, but it is a matter of getting the parameters passed into the logic query and repopulating all of the dropdowns with the correct picks.  At the same time, we have to build a string that holds all of the selections for an individual session.

Here is the query that we need the parameters to be passed into:

childSql = "SELECT av.optid, op.description FROM public_gdlp.options op INNER JOIN public_gdlp.availoptions av ON op.optid = av.childid WHERE av.optid = request.getParameter("x")

Where x is the dynamically named dropwown in the select statment listed in the intial post.
0
 
gdlp2004Author Commented:
Kenneth,

Your code would have helped, but this jsp page is going to be dynamic, so using functions don't work considering they don't accept references to variables, only values when they are called.  If anyone can help construct a method that will be sent the index and question variables, that would be most helpful.  Thanks.
0
 
KuldeepchaturvediCommented:
here i am assuiming that you have got your page appearing with all the dropdowns populated and also have that hidden field which contains all the selection..

if you have all this in place..
you can create another form in your page which posts to some other jsp..

like
<form action="process.jsp?formname=final>
<input type=text value="yourhiddenstring">
<input type=submit value="submit">
</form>

now hitting this submit button will go to process.jsp where you can do request.getParameter(1 to 163) and have all the values.
0
 
kennethxuCommented:
>>but this jsp page is going to be dynamic
the sample i gave you is dynamic, there are 4 dropdown list are inter connected and dynamically populated. did you spend time on the example? it is a working example if you have database table connect to it.
0
 
gdlp2004Author Commented:
Honestly, we don't have a hidden field for our selections...in actuality, we're not experts on this, though we have a good understanding of java applets/applications and sql querying.  We do have the dropdown populated, and the whole purpose of getting the values into the query is to create a dynamic parent child relationship for each one.  What exactly does a hidden string do and where would we post the above code into our jsp?  Also, what does that question mark after process.jsp mean?  Setting up the string on an 'answers' page was actually what we were trying do further down the line, but for right now, we are trying to get our parent/child relationships to work (our ERD structure works great, as we tested it in Access), so the dynamic changing of the dropdowns is the main problem.  Here is the most of the code of our working jsp page (as of now, that does populate the dropdowns and minus the imports):


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link rel="stylesheet" href="styles.css" type="text/css">
<title>Engineering Test</title>
</head>

<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">


<%//CONNECTION STRING BEGINS
  Connection conn = null;
  try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    conn = DriverManager.getConnection(
        "jdbc:oracle:thin:@divodb002:1521:salest01",
         "public_gdlp",
        "public");

      }

catch(SQLException e) {
   out.println("SQLException: " + e.getMessage() + "<BR>");
   while((e = e.getNextException()) != null)
      out.println(e.getMessage() + "<BR>");
   }
   //CONNECTION STRING ENDS
%>  


<!---HEADER TABLE--->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
            <td width="162" height="90" background="images/header_bkg_black.gif"><img src="images/GDTlogo2.gif" width="162" height="90" border="0"></td>
            <td width="100%" height="90" align="right" background="images/header_bkg_black.gif"><img src="images/OSSlogo2.gif" width="195" height="90" border="0"></td>
      </tr>
      <tr>
            <td width="162" height="21" background="images/header_bkg_red.gif"><img src="images/header_curve.gif" width="162" height="21" border="0"></td>
            <td width="100%" height="21" background="images/header_bkg_red.gif"><img src="images/header_bkg_red.gif" width="1" height="21" border="0"></td>
      </tr>
</table>
<!---END OF HEADER TABLE--->


<!---MAIN BODY TABLE--->
<table width="100%" border="0" cellspacing="0" cellpadding="0">      
      <tr>
            <td width="145" height="369" valign="top" bgcolor="#990000">
            
                  <!---NESTED TABLE FOR NAVBAR--->
                  <table width="145" border="0" cellspacing="0" cellpadding="5" align="center" class="whitetext">
                        <tr>
                              <td>&nbsp;<a href="#">Home Page</a></td>
                        </tr>
                        <tr>
                              <td>&nbsp;<a href="#">Log Out</a></td>
                        </tr>
                  </table>
                  <!---NESTED TABLE FOR NAVBAR--->
                  
            </td>
            <td width="15" height="369" background="images/gutter.gif"><img src="images/gutter.gif" width="15" height="1" border="0"></td>
            <td width="100%" height="369" valign="top">

<!--NESTED TABLE FOR MAIN CONTENT--->      
<table width="95%" border="0" cellspacing="0" cellpadding="0" align="center">
      <tr>
            <td>
<!---CONTENT STARTS HERE--->


<div class="h1">Outside Sales</div>

<br>
<!---BOX--->              
<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
            <td width="11" height="11" valign="bottom"><img src="images/box1x1.gif" width="11" height="11" border="0"></td>
            <td height="11" align="center" background="images/box1x2.gif"><img src="images/box1x2.gif" width="1" height="11" border="0"></td>
            <td width="15" height="11" valign="bottom"><img src="images/box1x3.gif" width="15" height="11" border="0"></td>
      </tr>
      <tr>
            <td width="11" background="images/box2x1.gif"><img src="images/box2x1.gif" width="11" height="1" border="0"></td>
            <td valign="top" bgcolor="#EFEFEF">


<!---TAB TABLE--->
<%
String tabimage = "";
tabimage = request.getParameter("tabs");

if( tabimage == null) { tabimage = "0";}
%>
<table border="0" cellspacing="0" cellpadding="0" align="center">
      <tr>
            <td><img src="images/tab_<%=tabimage%>.gif" width="525" height="30" border="0" usemap="#tab_<%=tabimage%>"></td>
      </tr>
</table>
<br>
<!---END TAB TABLE--->


<!---JSP STARTS HERE----->

<!--form method="Post" action="POC2.jsp"--->

<table width="100%" border="0" cellspacing="0" cellpadding="3" class="bodytext">

 <%//start loop
int index=0;
int question=0;
int testcounter = 0;
ResultSet rs,rs2,rs4,rs3;
String UdcDescSql,
       BaseSql,
       breakApartSql,
       fillSql,
       childSql;
     

UdcDescSql = "Select Description FROM public_gdlp.Options";
BaseSql = "Select op.OptionUDC, op.OptionValue, op.description FROM public_gdlp.DefaultBase db, public_gdlp.options op public_gdlp.UDC u"
+"where db.optid = op.optid and u.treeid ="+index+" AND u.UDCSeq ="+question+"";
  Statement stmt = conn.createStatement();    
  rs = stmt.executeQuery(UdcDescSql);
//Loop through results of query.

while (rs.next())
{
while (index < 14) { %>
<form action="POC.jsp?formname=<%= index %>&tabs=<%=tabimage%>" method="post" name="<%= index %>">
<%
for(question = 0; question <= 163; question++){
    breakApartSql = "Select Description, udcseq, udc FROM public_gdlp.udc where treeid= "+index+" and udcseq= "+question+"";
 rs2 = stmt.executeQuery(breakApartSql);
while (rs2.next())
{
%>  
 <tr>
    <td align="right">
<%out.println(rs2.getString("UDC")+"-"+ rs2.getString("Description"));%>

    </td>
    <td><select name="<%= rs2.getString("UDCSeq")%>" onChange="onChange()" style="font-family: Arial; font-size: 8pt;">"
    <!--<td><select name="<%= rs2.getString("UDCSeq")%>"  size="1" style="font-family: Arial; font-size: 8pt;">"-->
    <%//out.println("<select name=" + rs2.getString("UDC") + " " + "size=" + "1" + " style=font-family: Arial; font-size: 8pt;>");%>
   
<%
fillSql = "Select op.description, op.optid from public_gdlp.UDC u, public_gdlp.options op where u.treeID = "+index+" and u.udc = op.optionudc and udcseq="+question+"";
rs4 = stmt.executeQuery(fillSql);
while (rs4.next()){
out.println("<option value=" + rs4.getString("optid") + ">" +rs4.getString("description")+"</option>");

//Test Child Logic Query

childSql = "SELECT av.optid, op.description FROM public_gdlp.options op INNER JOIN public_gdlp.availoptions av ON op.optid = av.childid WHERE av.optid = "+rs4.getString("optid")+"";  
rs3 = stmt.executeQuery(childSql);
while (rs3.next()) {
out.println("<option value=" + rs3.getString("optid") + ">" +rs3.getString("description")+"</option>");
}   //End Test Child Query

}      

%>

The main problem we have is what to put into the onChange call.  Any more help with this will be greatly appreciated!!

0
 
gdlp2004Author Commented:
Kenneth,

I apologize, I didn't realize your code was dynamic...we don't understand javascript very well, and when I saw actual values being sent to your functions, I presumed it was static.  
0
 
kennethxuCommented:
no problem. I hope the sample helps. :)
0
 
gdlp2004Author Commented:
Actually, since we don't know javascript very well, we're having a hard time translating your example.  Could you help us out with our code that we posted?  

*This question has now breached into the 500 point category. ;)
0
 
kennethxuCommented:
let me find sometime to have a closer look at you code and get back to you.
0
 
kennethxuCommented:
you said your code is working exception dynamic drop down. but there is an dead loop in it:
int index=0;
...
while( index < 14) {

in addition, I only see one select tag there, that mean it has only one dropdown box. is not like what you said 2 dropdown boxes.

Can you just spend sometime to test out the example I gave to you, it is straightforward, you an easily modify the sample code for your need by reducing dropdown box to 2 and modify the sql statement.
0
 
kennethxuCommented:
frankly speak, the code you post is not making much sense to me, so it's hard for me to work on it.
0
 
gdlp2004Author Commented:
I appreciate everyone's help...let me do some more testing with that code, and I'll get back to you on this one.  Thanks!
0
 
kennethxuCommented:
did you solved your problem? any further enquires? is it working? I wish it is :)
0

Featured Post

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!

  • 7
  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now