Solved

Dispalying selected values in a jsp

Posted on 2004-09-15
8
618 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 51

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 51

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 51

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 51

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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).

679 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