views:

443

answers:

2

Ok its a continuation of my crap attempts of using client side scripts along with server side elements.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form name="test" action="test.jsp" method="post" enctype="multipart/form-data">
<select name="harish" onchange=callme();>
<option value="1">1</option>
<option value="2">2</option>
</select>
<script> 
var jsvar="Hello";
function callme()
{
    alert(jsvar);
    jsvar=document.getElementById("harish").value;
    alert(jsvar);
}
</script> 
 <% 
 String s=(String)("<script>document.writeln(jsvar)</script>").toString();
 out.println(s.equals("Hello"));
 if(!(s.equals("Hello")))
 {
 String jspvar="<script>document.writeln(jsvar)</script>";
 out.println("jspvar"+jspvar);
 session.setAttribute("test",jspvar);
 }
  %>
</form>


</body>
</html>

Now what I am trying is to set the selected value as a session variable.But my bad the value from javascript is not sitting properly on the jsp/java variable and therby my condition if(!(s.equals("Hello"))) fails.Can anyone help me here...

Update:

Can the below be the solution for this question

Have a HTML page with two frames. Let the first page contain all the javascript values you wish to populate. The second page(hidden) of the frame actually does the trick. That is actually a JSP. On click of a button (on any action on the first page) in the first page, point your location to the hidden frame (2nd page), perform checks / conversions and populate the variable of the first page using cross frame JAVASCRIPT.

A: 

Java is evaluated on the server side, so in variable s you will always find

<script>document.writeln(jsvar)</script>

Javascript is evaluated on the opposide side, that is on the client's browser, so this is why your method does not work (I've fallen many time into this also ^^)

You can POST the form on the same jsp where this code resides and take the result from the POSTed data, to do that you'll use a scriptlet. If I remember correctly you could use

request.getParameter("PARAMETER_NAME")

So just add the name of the jsp where this code is to the action of the form and the above code to retrieve the selected value.

Alberto Zaccagni
well actually the problem i am pushed to try this crap because of method="post" enctype="multipart/form-data" is not supporting any form element other than files.I am not sure why this problem occurs.
Harish
+2  A: 

my condition if(!(s.equals("Hello"))) fails

That is because this:

String s=(String)("<script>document.writeln(jsvar)</script>").toString();
out.println(s.equals("Hello"));

...is pretty much the same as writing:

out.println("this".equals("that"));

It will always be false.

Now what I am trying is to set the selected value as a session variable.

To set a variable in the session, you need to POST the form to the server (ignoring AJAX techniques, etc.). As I mentioned here, using multipart/form-data requires a MIME parser - the form below uses the default enctype.

This form will, when you select an option from the drop-down, post the form to the server. Every time the JSP is run, it uses a scriptlet <% ... %> tests to see if a "harish" parameter has been posted. If it has, it places it in the session. An expression <%= ... %> is used to display the current session value.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
<html>
<!-- header removed for clarity -->
<body>
<form id="test" name="test" action="test.jsp" method="post"><select
  name="harish" onchange="document.getElementById('test').submit();">
  <option value="select">select an option</option>
  <option value="1">1</option>
  <option value="2">2</option>
</select></form>
<%
  //see if a parameter was sent from page; "harish"==name attr on select
  String value = request.getParameter("harish");
  if (value != null) {
    //store it in session
    session.setAttribute("test", value);
  }
%>
<%="harish=" + session.getAttribute("test")%>
</body>
</html>

This assumes that the above page is test.jsp - that the page posts back to itself. If not, you'll need to move the scriptlet and the expression to test.jsp.

McDowell