Solved

Dispalying selected values in a jsp

Posted on 2004-09-15
8
616 Views
Last Modified: 2010-04-06
I have a jsp that has 5 values for checkbox, 3 values in a drop down box. The user is allowed to check any of the 5 check boxes and select one from the drop down. Upon hitting submit we need to diplay the same page with what the user has selected.That is if he has checked 2 check boxes those two should be shown checked and the one selected from the drop down should be diplayed as it is. We would like not to do a reirect or forward. We can use session beans and servlets as part of the MVC pattern.


Any solution is appreciated.No javascripts. Only server side solution.
0
Comment
Question by:ramani081399
8 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 12071389
Is your checkboxes have the same name or they're named differently?

To get what you had posted, try use either:

request.getParameter(String name)

or

request.getParameterValues(String name) 'Use this when there is more than 1 form object with the same name

regards
0
 

Author Comment

by:ramani081399
ID: 12073075
Hello,
Could you put a small piece of code to show what you are explaining and on hitting save how do i return to the same page displaying those selected options.
Ramani
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 12074509
If you are submitting the form, then the script you are submitting to just needs to return the same page as its response.  The script has the values so all it has to do is generate the response with the the values set by putting a checked property on the checkboxes and a selected property on the correct select option.

Cd&
0
 
LVL 5

Expert Comment

by:apparition
ID: 12074852
This should get you started:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
%>
<TITLE>test.jsp</TITLE>
<%

            String chk1 = request.getParameter("chk1");
            String chk2 = request.getParameter("chk2");
            String chk3 = request.getParameter("chk3");
            String chk4 = request.getParameter("chk4");

%>
</HEAD>
<BODY>
<form method="post">
      <input type="checkbox" name="chk1" value="yes" <%if (chk1 != null){%>checked<%}%>> CheckBox 1<BR>
      <input type="checkbox" name="chk2" value="yes" <%if (chk2 != null){%>checked<%}%>> CheckBox 2<BR>
      <input type="checkbox" name="chk3" value="yes" <%if (chk3 != null){%>checked<%}%>> CheckBox 3<BR>
      <input type="checkbox" name="chk4" value="yes" <%if (chk4 != null){%>checked<%}%>> CheckBox 4<BR>
      <input type="Submit" name="submit" value="Save">
</form>
</BODY>
</HTML>
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Expert Comment

by:Ryan Chong
ID: 12080793
If your checkboxes have same names, here is a bit different story, where you need to assign the values from request.getParameterValues(String name) to an array, then compare with your list..

Sorry, nowsaday don't use JSP anymore, will find the exact code for you when i back home, or apparition can help instantly here? ;-) cheers
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 12090134
For checkboxes with same name, try something like:

<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*" errorPage="" %>
<%!
      public static String chkopt(String v, String[] values, String selected){
            for (int i=0; i<values.length; i++){
                  if (values[i].equals(v)) {
                        return selected;      
                  }                  
            }
            return "";
      }
%>
<%
      String[] chks = request.getParameterValues("checkboxname");
      if (chks == null) chks = new String[0];
%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="">
  <p>You select <%=chks.length%> items</p>
  <p>
    <input name="checkboxname" type="checkbox" id="checkbox" value="1" <%=chkopt("1",chks," checked")%>>
    <input type="checkbox" name="checkboxname" id="checkbox" value="2" <%=chkopt("2",chks," checked")%>>
    <input type="checkbox" name="checkboxname" id="checkbox" value="3" <%=chkopt("3",chks," checked")%>>
    <input type="checkbox" name="checkboxname" id="checkbox" value="4" <%=chkopt("4",chks," checked")%>>
    <input type="checkbox" name="checkboxname" id="checkbox" value="5" <%=chkopt("5",chks," checked")%>>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
    <input name="Reset" type="reset" id="Reset" value="Reset">
  </p>
</form>
</body>
</html>


      
0
 

Author Comment

by:ramani081399
ID: 12091117
Ryancys,
 Where will the action go in the form.You have said actio=""
Ramani
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 12091660
Ya.. you need to put a page on form action there.. if you want to post the request back to submit page, you can set the form action as your submit page.

Hope this is clear, cheers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now