• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Urgent Help Needed in JSP

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a problem with checkboxes in JSP. I am displaying the checkboxes on one Jsp page.The number of checkboxes are not fixed they come from the database entries. When the user ticks on some checkboxes and clicks on the "BURROW" button, control is transferred to the next JSP page. where i can get the values of the ticked checkboxes. but the problem is that i want to restrict the user to tick only two checkboxes. How do i perform the validation in the second JSP page. I can get the number of checkboxes and pass the number to the next JSP page ..is there any mean by which i can use the isChecked() function to verify the checkbox? I have appended the code below ...Neglect the initial part ..just see the FORM part .

<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="login" class="WebSphereSamples.donttouchthis.Loginclass" scope="session" />
<jsp:useBean id="allow" class="WebSphereSamples.donttouchthis.Borrow" />
<% String thisuser= good.getUser();%>
<% boolean check= good.getGrant();%>
<%! ResultSet res;%>
<% if(check)
{ %>
<% String author=request.getParameter("aname");%>
<%res= allow.getAuthorBooks(author);%>
<b> The books written by </b><font color="pink" size=5><%= author%></font><b> are</b>

<form method="POST" action="Authornext.jsp">
<% int i=0;%>
<% while(res.next())


     <input type="checkbox" name="<%= i%>" value="<%= res.getString(1)%>"><%= res.getString(1)%><br>

<% i=i+1;
} %>
<input type="hidden" name="hide" value="<%= i%>" >
<input type="submit" value="Borrow">

<b>Tick the Books you want to borrow</b><br>
<b> Note: </b> you can have atmost 2 books only<br>

<% }

Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Posts which contravene the naming convention are not eligible to win books! Please log in with a new name which meets the requirements.

Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use JavaScript to validate the contents of the checkboxes. See any good book on JavaScript for details.
If you want to stick with pure Java, you can retrieve the values of the checkboxes in the second JSP with request.getParameter() in a loop of i=0 to hide.
You have a problem, though, with your declaration of the result set:
<%! ResultSet res; %>
What happens when two people request your JSP at the same time? Remember, servlets are single instances with multiple threads, so any instance variables are shared - your second request will stomp on your first. You need to put this variable inside a scriptlet
<% ResultSet res; %>
so that it is a local method variable. If it needs to be shared between multiple pages in a session, you need to store it in the HttpSession:
session.setAttribute("MyResultSet", res);
ResultSet res = (ResultSet) session.getAttribute("MyResultSet");
Also, just out of curiosity, in what sense is this "urgent"?
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets
Website: http://www.philhanna.com
[This message has been edited by Phil Hanna (edited April 26, 2001).]
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Phil
thanx for sharing your knowledge with me ... the mentioned problem was urgent cause i was not able to move ahead with my work .I was stuck with this thing for sometime ..but couldnt get the solution .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic