Solved

Dispalying selected values in a jsp

Posted on 2004-09-15
8
622 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 52

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 52

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 52

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 52

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

634 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