Solved

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

Posted on 2004-04-05
18
1,095 Views
Last Modified: 2010-04-01
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
Comment
Question by:gdlp2004
  • 7
  • 7
  • 4
18 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10759789
you will have to submit your page to your servlet/jsp and then do a request.getParameter("varname"); to get these values
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10759806
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
 
LVL 14

Accepted Solution

by:
kennethxu earned 500 total points
ID: 10759990
0
 

Author Comment

by:gdlp2004
ID: 10760008
Where do you put the request.getParameter, and first off, how do you call the method to receive them?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760035
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
 

Author Comment

by:gdlp2004
ID: 10760183
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
 

Author Comment

by:gdlp2004
ID: 10760251
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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 10760265
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760289
>>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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:gdlp2004
ID: 10760375
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
 

Author Comment

by:gdlp2004
ID: 10760417
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 10760458
no problem. I hope the sample helps. :)
0
 

Author Comment

by:gdlp2004
ID: 10760576
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 10761650
let me find sometime to have a closer look at you code and get back to you.
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 10763011
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 10763018
frankly speak, the code you post is not making much sense to me, so it's hard for me to work on it.
0
 

Author Comment

by:gdlp2004
ID: 10765216
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
 
LVL 14

Expert Comment

by:kennethxu
ID: 10769454
did you solved your problem? any further enquires? is it working? I wish it is :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
library class in java 1 89
Lamba in java 8 9 235
servlet concurrency 13 84
difference between projection and projection list in hibernate 1 83
HOW TO: Install and Configure VMware vSphere Hypervisor 6.5 (ESXi 6.5), Step by Step Tutorial with screenshots. From Download, Checking Media, to Completed Installation.
Is your company's data protection keeping pace with virtualization? Here are 7 dynamic ways to adapt to rapid breakthroughs in technology.
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 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

13 Experts available now in Live!

Get 1:1 Help Now