Solved

Dispalying selected values in a jsp

Posted on 2004-09-15
8
615 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

759 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

24 Experts available now in Live!

Get 1:1 Help Now