1

I have the following code in a JSP file:

rs=stmt.executeQuery("select * from routemaster");
if(rs!=null){
   %>
   <table class="notebook" align="center">
   <tr class=row_title>
   <th class=row_title>RouteID</th>
   <th class=row_title>RouteCode</th>
   <th class=row_title>BusNo</th>
   <th class=row_title>InBoundTime</th>
   <th class=row_title>OutBoundtime</th>
   <th class=row_title>Location</th></tr>
   <%
   while(rs.next()){
        RouteID=rs.getString(1);
        RouteCode=rs.getString(2);
        InBoundtime=rs.getString(4);
        OutBoundtime=rs.getString(5);
        BusNo=rs.getString(6);
        Location=rs.getString(7);
        DisRow++;

       %><tr class= <%=(DisRow%2!=0)? "row_even" : "row_odd"%>>
       <td><%=RouteID%></td>
       <td><a onclick="sendInfo('<%=RouteCode%>') " ><%=RouteCode%></a></td>
       <td><%=BusNo%></td>
       <td><%=InBoundtime%></td>
       <td><%=OutBoundtime%></td>
       <td><%=Location%></td>
   </tr><%
   }
   %></table><% 
}

And the following JS code:

sendInfo(txt){
    var txt = window.opener.document.addbus0.RouteCode;
    txt.value = txtVal;
    window.close();
}

Whenever a link with the RouteCode is clicked, then the window needs to be closed and the selected RouteCode needs to be stored in the session. How can I achieve this?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
RAVITEJA SATYAVADA
  • 2,503
  • 23
  • 56
  • 88
  • When you say session, are you talking about `HttpSession` on the server side? There is no session in the browser.. – Aravindan R May 20 '11 at 17:14
  • 2
    Also, executing SQL from a scriptlet inside a JSP is not a recommended. you might at the least want to use JSTL for looping. – Aravindan R May 20 '11 at 17:16

1 Answers1

0

To the point, you need to send the value from the client to the server. The normal way to achieve this is to let the client click a link or submit a form to an URL on the server side and send the value along as request parameter in the URL or as a hidden input in the form.

Since you're using a link, here's an example with a link:

<a href="sendInfo.jsp?routeCode=<%=routeCode%>"><%=routeCode%></a>

Then in sendInfo.jsp just do

<% 
  String routeCode = request.getParameter("routeCode");
  session.setAttribute("routeCode", routeCode);
%>
<script>
  window.opener.document.addbus0.RouteCode.value = '<%=routeCode%>';
  window.close();
</script>

Unrelated to the concrete problem, this code style is not the best practice. Java code belongs in Java classes, not in JSP files. JSP files should only contain HTML, JSP tags and EL. See also How to avoid Java code in JSP files? Also, the Java coding conventions mandate that variable names should start with lowercase. Read them carefully. You've a long way to go, good luck.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555