Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to add multiple selection from dropdown

Status
Not open for further replies.

ketandba

Programmer
Nov 15, 2004
38
US
i am new in jsp. can any body give me an idea how i can add multiple selection from dropdown and added this multiple selection in one int field of database...

e.g. drop down contens 0|1|2|3|4|5|6|7....

user has selected 3|5|6|7
added into database as 3|5|6|7 as per multiple selection.

Thanks in advanced...

ketan
 
Hi,

Use request.getParameterValues which returns String[] of selected values.

Ex:
Code:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    if(null != request.getParameter("multiChoice"))
    {
       String[] multiChoiceArray = request.getParameterValues("multiChoice");
       for(int i=0; i< multiChoiceArray.length;i++)
       {
           out.println("Array ["+ i +"]" + multiChoiceArray[i] + "");
           // call the SQL query here to instert the multiChoiceArray[i] into database
       }
    }
%>
<html>
  <head><title>Simple jsp page</title></head>
  <body>
  <form method="POST" action="test.jsp">
    <p>
    <select size="1" name="multiChoice" multiple>
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
    </p>
    <input type="submit" value="Submit" name="B1">
    <input type="reset" value="Reset" name="B2">
   
  </form>
  
  </body>
</html>

Cheers
Venu
 
hi,
Venu,
Thank you very much for your reply.
But Still i have some confusion..

actually i am developing web application using struts/servlet/jsp ..

here is my jsp page for your ready reference.
Code:
*****
<%@ page import="
	app.util.Profile,
	app.database.HospitalDAO,
	net.omalas.ots.util.Constants,
	app.database.UserDAO,
    	net.omalas.struts.actions.LogonAction" 
    %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/application" prefix="app" %>

<%
int profileMode;
String profileUsername;
int profileHosId;
Profile profile = (Profile)request.getSession().getAttribute(Constants.USER_PROFILE);
if( profile != null ) {
  profileMode = profile.getMode();
  profileUsername = profile.getUsername();
  System.out.println("profileUsername from AddMedicalTranscriptionist.jsp ==> " + profileUsername);
  profileHosId = profile.getHosId();
  System.out.println("profileHosId from AddMedicalTranscriptionist.jsp ==> " + profileHosId);
}
else {
  profileMode = 0;
}
%>
<html:form action="/AddMedicalTranscriptionistSubmit"  focus="userId">
<TABLE width="100%" align="center" border="0" cellpadding="5" cellspacing="0">
<TR>
<TD colspan="2"><html:errors/></TD>
</TR>
<TR>
<TH align="right">User Name:</TH>
<TD align="left"><html:text property="userId"/></TD>
</TR>
<TR>
<TH align="right">Password:</TH>
<TD align="left"><html:password property="password"/></TD>
</TR>
<TR>
<TH align="right">First Name:</TH>
<TD align="left"><html:text property="firstName"/></TD>
</TR>
<TR>
<TH align="right">Middle Name:</TH>
<TD align="left"><html:text property="middleName"/></TD>
</TR>
<TR>
<TH align="right">Last Name:</TH>
<TD align="left"><html:text property="lastName"/></TD>
</TR>
<TR>
<TH align="right">Email:</TH>
<TD align="left"><html:text property="emailAddress"/></TD>
</TR>
<TR>
<TH align="right">Phone:</TH>
<TD align="left"><html:text property="phone"/></TD>
</TR>
<TR>
<TR>
<TH align="right">Hospital :</TH>
<TD align="left">
<app:hospitalsCombobox multiple="multiple" userId="<%=profile.getUsername()%>" />
</TD>
</TR>
<%
   // This is i have added as per your instruction..........(am i right?)
   StringBuffer hospitalStringBuffer;
    if(null != request.getParameter("multiple"))
    {
       String[] multiChoiceArray = request.getParameterValues("multiple");
       for(int i=0; i< multiChoiceArray.length;i++)
       {
           if (i = 0)
              {hospitalStringBuffer = new StringBuffer();}
              else 
              {hospitalStringBuffer.append("|");}
           
           out.println("Array ["+ i +"]" + multiChoiceArray[i] + "");
           // call the SQL query here to instert the multiChoiceArray[i] into database
         hospitalStringBuffer = hospitalStringBuffer.append(multiChoiceArray[i]);	

       }
       String hosString = hospitalStringBuffer.toString();
    }
%>
<TD align="right"><html:submit value="Submit"/></TD>
<TD align="left"><html:reset/></TD>
</TR>
</TABLE>
</html:form>
*****
in above code, i am getting string for multiple selection of hospital..which is to be converted in int and append into database..

like 1|2|3|4|5 as a int into database field...
Here is my Action class..

*****
Code:
public class AddMedicalTranscriptionistAction extends Action {
		
	 	private String userId          = null;
		private String password        = null;
		private String firstName       = null;
		private String middleName      = null;
		private String lastName        = null;
		private String emailAddress    = null;
		private String hosName           = null;
		private String userPhone       = null;	

		
	public ActionForward execute(ActionMapping mapping, ActionForm actionForm,
   			 HttpServletRequest request,
			 HttpServletResponse response)throws Exception
	{
								 	
	
	  	  if (actionForm instanceof AddMedicalTranscriptionistForm) 
	  	  {

 	AddMedicalTranscriptionistForm addMedicalTranscriptionistForm = (AddMedicalTranscriptionistForm) actionForm;	

	 
	 		userId          = addMedicalTranscriptionistForm.getUserId();		
  	 		password      	= addMedicalTranscriptionistForm.getPassword();
	 		firstName 	  	= addMedicalTranscriptionistForm.getFirstName();
	 		middleName 	  	= addMedicalTranscriptionistForm.getMiddleName();
	 		lastName	  	= addMedicalTranscriptionistForm.getLastName();
		 	hosName         	= addMedicalTranscriptionistForm.getHosName(); 		 
		 	emailAddress  	= addMedicalTranscriptionistForm.getEmailAddress();
		                      userPhone       = addMedicalTranscriptionistForm.getPhone();
		   
	boolean addedMedicalTranscriptionist = false;
	 		UserDAO usrDao = new UserDAO();
		 	addedMedicalTranscriptionist = usrDao.addUser(userId,4, 
       	                                          Integer.parseInt(hosName),      // here i am converting String to int and add into database
                                                                password,firstName,middleName,
		                      lastName,1,emailAddress,userPhone,null);

				if (!addedMedicalTranscriptionist) 
				{
					ActionErrors errors = new ActionErrors();
					errors.add(ActionErrors.GLOBAL_ERROR,
					new ActionError("error.addMedicalTranscriptionist.failed"));
					saveErrors(request,errors);
					return (new ActionForward(mapping.getInput()));
				}
	  		}
				return (mapping.findForward(Constants.SUCCESS));
		}		
}
*****

here is my comboboxtag.java file...

**
Code:
public class HospitalsComboboxTag extends TagSupport
{
	
	private String hospitalName = null;
	private int hospitalId = 0;
	private String userId = null;

	public ArrayList getHospitals() {
		return new HospitalDAO().getAllHosName();
			
	}
	public String getHospitalName( int hospitalId ) {
		return new HospitalDAO().getHosName( hospitalId );
	}
	
	public int getHospitalId( String hosName ) {
		HospitalDAO hosDao = new HospitalDAO();
		return hosDao.getHosId( hosName );
	}

    public final int doStartTag() throws JspException
    {	    	
		ArrayList hospitalsArray;
		String hosString;
		
		hospitalsArray = getHospitals();
		
		StringBuffer hospitalsCombobox = 
			new StringBuffer().append( "<select name=hosName>" );
		
		for( int i=0; i<hospitalsArray.size(); i++ ) {
			
			hospitalName = (String)hospitalsArray.get(i);
			
		    hospitalId = getHospitalId( hospitalName );
					
			hospitalsCombobox
				.append( "<option value="  )
				.append( hospitalId )
				.append( ">" )
				.append( getHospitalName( hospitalId ) )
				.append( "</option>" );
		}

		hospitalsCombobox.append("</select>");
		System.out.println("hospitalsCombobox values ==> " + hospitalsCombobox.toString())	;		
		try {
    		pageContext.getOut().print( hospitalsCombobox.toString() );
    	}
    	catch (IOException e) {
    		e.printStackTrace();	
    	}
		
		return SKIP_BODY;
    }
    
	

    public final void sethosName( String hosName )
	  { hosName = hospitalName; }   
    
    public final void sethosId( int hosId )
	  { hosId = hospitalId; } 

	public final void setUserId( String userId )
		{ this.userId = userId; }

}
**
it is my understanding that when end user try to insert new record then hospital dropdown list is display and
he is doing multiple selection when try to insert hospital field.

Convert this stringbuffer to String and finally as an int , it is stored into database...

what is wrong...and all procedure(steps) for this type of function is correct?
If possible change my code and explain me...
Your help is appriciated...

ketan

 
Hi,

I dont see HospitalsComboboxTag generating multipule select option list. So at any give time you will receive only one hospitalId for the hosName in the action class. The code what you have should be fine.

Remove the code which I had posted earlier from your jsp page. It is not needed there.

In case if you are getting multipule values from the JSP page then change your AddMedicalTranscriptionistForm to return String[] for getHosName(). And change the method Signature for usrDao.addUser() to accept String insted of int for hospitalId coz "|" the operator cannot be coverted to string. Below code will build the String 0|1|2|3| similar to this.

Code:
if(null != request.getParameter("hosName"))
    {
       String[] hosNameArray = request.getParameterValues("hosName");
       StringBuffer buffer = new StringBuffer();
       for(int i=0; i< hosNameArray.length;i++)
       {
           buffer.append(hosNameArray[i]);
           buffer.append("|");
       }
    }

Cheers
Venu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top