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='<%session.setAttribute("currentTable", request.getParameter("tableSelect"));%>; 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>
<%
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>
This will be executed:
// choice will take the value that is at the session. (previous)
String choice = (String) session.getAttribute("currentTable");
// then you will get the new value submitted and put it in the session
session.setAttribute("currentTable", request.getParameter("tableSelect"));
session.setAttribute("currentTable", request.getParameter("tableSelect"));
// then you will print it
out.println(session.getAttribute("currentTable"));
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' <%= ("CONFIG_SOURCE".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%> >CONFIG_SOURCE</option>
<option value='CONFIG_PROCESS' <%= ("CONFIG_PROCESS".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%> >CONFIG_PROCESS</option>
<option value='CONFIG_PROCESS_SOURCE' <%= ("CONFIG_PROCESS_SOURCE".equals("CONFIG_SOURCE")) ? ("selected='selected'") : ""%> >CONFIG_PROCESS_SOURCE</option>
will be turned into this
<option value='CONFIG_SOURCE' selected='selected' >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() {
<% out.println("Hello world"); %>
}
</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:
<% out.println("Hello world"); %>
.....
<script>
function abc() {
}
</script>
PS. Don't put html code in servlets. It is best to have the above code in a jsp