0

Hi,

I would like to have a Combobox where the selected item is the value set in the session attribute.

For example:
HTML:

<select name='myCombo'>
                <option value='--Select Table--'>--Select Table--</option>
                <option value='OPTION1'>OPTION1</option>
                <option value='OPTION2'>OPTION2</option>
            </select>

Javascript:

document.getElementbyName('myCombo').selectedValue = session.getAttribute("choice");

Thanks,
-Ashton.

3
Contributors
10
Replies
14
Views
7 Years
Discussion Span
Last Post by jerin francis
Featured Replies
  • It is like I said. You lack the very basics and you should be ready to accept criticism: Java is executed at the server. Then the page is generated and then send to the client [U]pure[/U] html. Meaning that this: [CODE]onChange='[B][COLOR="Green"]<%session.setAttribute("currentTable", request.getParameter("tableSelect"));%>[/COLOR][/B]; testForm.submit();'[/CODE] is [B][U]not[/U][/B] executed then you change the … Read More

0

You cannot combine java and javascript that way. They are NOT the same:

document.getElementbyName('myCombo').selectedValue = '<%= session.getAttribute("choice") %>';

I see from your previous thread that you lack of lot basic knowledge about jsp and submitting requests.
I would suggest to take sometime to learn the basics, study a little and then continue.

Forgot the '' single quotes. It will not work without them

Edited by javaAddict: n/a

-1

Hi,

I asked if there was a way to select the value in the combobox that is equal to a session attribute. Not an opinion on my education level.

Thanks,
-Ashton.

Votes + Comments
No. He is right.
0

Given this: document.getElementbyName('myCombo').selectedValue = session.getAttribute("choice"); I simply advised you to study a little more before trying things that are out of your league. The above code is an example of not understanding how to integrate java code in html, which should be very basic. So I just said that you focus more on learning.

Also this is how is done according to your code:

<%
String choice = (String)session.getAttribute("choice");
%>

            <select name='myCombo'>
[B]<[/B]option value='' 
<%= ("".equals(choice))?("[B]selected='selected'[/B]"):""%> [B]>[/B]--Select Table--</option>

[B]<[/B]option value='OPTION1' 
<%= ("OPTION1".equals(choice))?("[B]selected='selected'[/B]"):""%> [B]>[/B]OPTION1</option>

[B]<[/B]option value='OPTION2' 
<%= ("OPTION2".equals(choice))?("[B]selected='selected'[/B]"):""%> [B]>[/B]OPTION2</option>
            </select>

Also if you give a better explanation on how you put the value in the session, I will be able to give a more efficient solution.
In your other post you pass a drop-down-list value through the request. Is this the same drop down list?
What does this value represent because we rarely put things in the session. Maybe you can send it through the request.

Can you provide a more descriptive explanation on what you are trying to achieve and what is your wider goal so we can provide some recommendations?

0

Hi,

Firstly, my example wasn't to show off my skill in javascript or jsp, it was merely a logical demonstration of what I'm trying to achieve.
Below is a description on what I'd like to do:
I have a page with 2 separate frames. On Frame 1's page, I have the drop down list and when I change the selection, it should refresh Frame 1 & 2's page accordingly.

For example: if I selected item 1 on frame 1, then it would refresh both frames and show the selected item's value on frame 2, as well as keeping the selected item SELECTED (in the combo box) in frame 1.

Thanks,
-Ashton.

0

I usually don't work with frame and I don't have time to give it much thought right now.
But, have you tried submitting the form to the jsp that is the second frame?

I assume you have a page where you declare the 2 frames and declare which jsps will be displayed.

My first guess would be to have jsp_1 to submit to the second jsp:

<form name="..." action="frame2.jsp" >
...

Firstly see if the drop down list in the first jsp stays unchanged, and if the value sent is printed.

0

Hi,

Is it possible to do that code of yours as a servlet instead of JSP by any chance?

Also this is how is done according to your code:

<%
String choice = (String)session.getAttribute("choice");
%>

            <select name='myCombo'>
<option value='' 
<%= ("".equals(choice))?("selected='selected'"):""%> >--Select Table--</option>

<option value='OPTION1' 
<%= ("OPTION1".equals(choice))?("selected='selected'"):""%> >OPTION1</option>

<option value='OPTION2' 
<%= ("OPTION2".equals(choice))?("selected='selected'"):""%> >OPTION2</option>
            </select>

PS: Your code for the drop down list worked but, for some reason, it is always one step behind. So when I change the selection, the drop down list's current selection's value is that of the previous change instead of the current change. i.e. If I select "OPTION 1", then nothing happens; then I select "OPTION 2" and the list shows "OPTION 1". Then if I choose "OPTION 1" again, it shows "OPTION 2". Hence, always 1 step behind...

Here is the source:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test Page</title>
    </head>
    <body>
        <%
            String choice = (String) session.getAttribute("currentTable");
        %>
        <form method="post" name="testForm" action="Test.jsp">
            <select name='tableSelect' onChange='<%session.setAttribute("currentTable", request.getParameter("tableSelect"));%>; testForm.submit();' style="text-align:center">
                <option value='--Select Table--' <%= ("--Select Table--".equals(choice)) ? ("selected='selected'") : ""%> >--Select Table--</option>
                <option value='CONFIG_SOURCE' <%= ("CONFIG_SOURCE".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' <%= ("CONFIG_PROCESS".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_PROCESS</option>
                <option value='CONFIG_PROCESS_SOURCE' <%= ("CONFIG_PROCESS_SOURCE".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_PROCESS_SOURCE</option>
                <option value='CONFIG_SAL_ROUTING' <%= ("CONFIG_SAL_ROUTING".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_SAL_ROUTING</option>
            </select>
        </form>
        <%
            session.setAttribute("currentTable", request.getParameter("tableSelect"));
        %>
        <%
            out.println(session.getAttribute("currentTable"));
        %>
    </body>
</html>

Thanks again!
-Ashton

Edited by AshtonHogan: n/a

2

It is like I said. You lack the very basics and you should be ready to accept criticism:

Java is executed at the server. Then the page is generated and then send to the client pure html.

Meaning that this:

onChange='[B]<%session.setAttribute("currentTable", request.getParameter("tableSelect"));%>[/B]; testForm.submit();'

is not executed then you change the drop down list.

Java and javascript ARE NOT THE SAME

Jave is executed at the server, the javascript at the client and it has absolutely NO access to java classes or objects.

When you submitted, you send to the server the request, then this is executed: session.setAttribute("currentTable", request.getParameter("tableSelect")); Then you receive at the client this:

<select name='tableSelect' onChange='testForm.submit();' style="text-align:center">

Only the 'testForm.submit();' will be executed at the onchange because it is javascript not java. The java part has already been executed when you submitted.

This works:

document.formName.inputName.value = '<%= (String)session.getAttribute("..")%>'

because first the java scriplet is evaluated and then sent to the client. What you actually see to your browser is this.

document.formName.inputName.value = 'theValueReturned';

No matter what you do, you cannot change a java variable if you don't submit.
So if you look only at your java code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test Page</title>
    </head>
    <body>
        <%
            [B]String choice = (String) session.getAttribute("currentTable");[/B]
        %>
        <form method="post" name="testForm" action="Test.jsp">
            <select name='tableSelect' onChange='<%[B]session.setAttribute("currentTable", request.getParameter("tableSelect"));[/B]%>; testForm.submit();' style="text-align:center">
                <option value='--Select Table--' <%= ("--Select Table--".equals(choice)) ? ("selected='selected'") : ""%> >--Select Table--</option>
                <option value='CONFIG_SOURCE' <%= ("CONFIG_SOURCE".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' <%= ("CONFIG_PROCESS".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_PROCESS</option>
                <option value='CONFIG_PROCESS_SOURCE' <%= ("CONFIG_PROCESS_SOURCE".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_PROCESS_SOURCE</option>
                <option value='CONFIG_SAL_ROUTING' <%= ("CONFIG_SAL_ROUTING".equals(choice)) ? ("selected='selected'") : ""%> >CONFIG_SAL_ROUTING</option>
            </select>
        </form>
        <%
            [B]session.setAttribute("currentTable", request.getParameter("tableSelect"));[/B]
        %>
        <%
            [B]out.println(session.getAttribute("currentTable"));[/B]
        %>
    </body>
</html>

This will be executed:

[B]//[/B] choice will take the value that is at the session. ([U]previous[/U])
[B]String choice = (String) session.getAttribute("currentTable");[/B]

[B]//[/B] then you will get the [U]new[/U] value submitted and put it in the session
[B]session.setAttribute("currentTable", request.getParameter("tableSelect"));
session.setAttribute("currentTable", request.getParameter("tableSelect"));[/B]

[B]//[/B] then you will print it
[B]out.println(session.getAttribute("currentTable"));[/B]

BUT 'choice' has already taken value before you put into the session the old one. Then the html code will be generated:
Assuming the 'choice' is "CONFIG_SOURCE", the java code will be executed first and the scriplets will return their value.

This

<option value='CONFIG_SOURCE' [B]<%= ("CONFIG_SOURCE".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%>[/B] >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' [B]<%= ("CONFIG_PROCESS".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%>[/B] >CONFIG_PROCESS</option>
                <option value='CONFIG_PROCESS_SOURCE' [B]<%= ("CONFIG_PROCESS_SOURCE".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%>[/B] >CONFIG_PROCESS_SOURCE</option>

will be turned into this

<option value='CONFIG_SOURCE' [U]selected='selected'[/U] >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' __ >CONFIG_PROCESS</option>
                <option value='CONFIG_PROCESS_SOURCE' __ >CONFIG_PROCESS_SOURCE</option>

And then send and displayed to your browser.


This:

<script>
  function abc() {
     [B]<% out.println("Hello world");  %>[/B]
  }
</script>

Will not be executed when you call the method via javascript because it has been executed at the server. It is as if you wrote this:

[B]<% out.println("Hello world");  %>[/B]

.....

<script>
  function abc() {
     
  }
</script>

PS. Don't put html code in servlets. It is best to have the above code in a jsp

Edited by javaAddict: n/a

Votes + Comments
Great explanation.
Very truth, nice code provided
0

Hi,

I don't think you should be so quick to judge all the time, I think you should rather try and be a little patient instead...

Thanks for all your help, your solution is working really well, there is only one more thing that I don't understand... After a selection is made, it does not stick to the selection chosen; it's always just the same item selected, no matter which one you choose...

Below is the code:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Test Page</title>
    </head>
    <body>
        <%
            String choice = (String) session.getAttribute("currentTable");
        %>
        <form method="post" name="testForm" action="Test.jsp">
            <select name='tableSelect' onChange='<%session.setAttribute("currentTable", request.getParameter("tableSelect"));%>; testForm.submit();' style="text-align:center">
                <option value='--Select Table--' <%= ("--Select Table--".equals("--Select Table--")) ? ("selected='selected'") : ""%> >--Select Table--</option>
                <option value='CONFIG_SOURCE' <%= ("CONFIG_SOURCE".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%> >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' <%= ("CONFIG_PROCESS".equals("CONFIG_PROCESS")) ? ("selected='selected'") : ""%> >CONFIG_PROCESS</option>
                <option value='CONFIG_PROCESS_SOURCE' <%= ("CONFIG_PROCESS_SOURCE".equals("CONFIG_PROCESS_SOURCE")) ? ("selected='selected'") : ""%> >CONFIG_PROCESS_SOURCE</option>
                <option value='CONFIG_SAL_ROUTING' <%= ("CONFIG_SAL_ROUTING".equals("CONFIG_SAL_ROUTING")) ? ("selected='selected'") : ""%> >CONFIG_SAL_ROUTING</option>
            </select>
        </form>
        <%
            session.setAttribute("currentTable", request.getParameter("tableSelect"));
        %>
        <%
            out.println(session.getAttribute("currentTable"));
        %>
    </body>
</html>

Thanks a lot,
-Ashton

Edited by AshtonHogan: n/a

0

That's because you don't read what I say to you. You just copy paste:

I changed this:

...
<option value='CONFIG_SOURCE' <%= ("CONFIG_SOURCE".equals([B][B]choice[/B][/B])) ? ("selected='selected'") : ""%> >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' <%= ("CONFIG_PROCESS".equals([B]choice[/B])) ? ("selected='selected'") : ""%> >CONFIG_PROCESS</option>
...

To this just to show you how the java is executed. (choice is replaced by the value taken from session)

<option value='CONFIG_SOURCE' <%= ("CONFIG_SOURCE".equals("[B]CONFIG_SOURCE[/B]")) ? ("selected='selected'") : ""%> >CONFIG_SOURCE</option>
                <option value='CONFIG_PROCESS' <%= ("CONFIG_PROCESS".equals("[B]CONFIG_PROCESS[/B]")) ? ("selected='selected'") : ""%> >CONFIG_PROCESS</option>

What you written will return true for all of them that is why you get always the same.

Put back 'choice' again.

Also why do you use the session. From your code whenever you submit, you change the value of the session.

If, and only if, the "currentTable" will be used for communication between these 2 pages then you can do this:

String choice = request.getParameter("tableSelect")

// rest of the code that displays the drop down list

I don't know the rest of your code, but if you want just to submit the value from one page to another then use only the request.
If you want that value to be seen from all the pages without having to submit; For example you went to a page from a complete different "path" and you want that value, then yes, put it in the session.

0

hi
i want to load one of my table column into a combobox and ,also i want to delete the data correspondant to that selected data

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.