Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dispalying selected values in a jsp

Posted on 2004-09-15
8
Medium Priority
?
624 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 53

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 53

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 53

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 53

Accepted Solution

by:
Ryan Chong earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.

715 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