1600 PMP mock questions 1400 CAPM mock questions 800 SCJP 6 mock questions 600 OCAJP 7 mock questions 590 OCPJP 7 mock questions 556 SCWCD 5 mock questions 500 OCEJWCD 6 mock questions pdfDownload (java,struts, hibernet etc)

JSP Tutorials

Q.How to create and retrive a multiple selections list in jsp/servlet?

This is the code to display multiple selections list in jsp.
List medList = DAO.getallMedium(); // this method retun list of Medium objects.
medList list contains list of Medium objects.

Java :
bean class.
public class Medium {
    int medId;
    String medName;

    /**
     * @return Returns the medId.
     */
    public int getMedId() {
        return medId;
    }
    /**
     * @param medId The medId to set.
     */
    public void setMedId(int medId) {
        this.medId = medId;
    }
    /**
     * @return Returns the medName.
     */
    public String getMedName() {
        return medName;
    }
    /**
     * @param medName The medName to set.
     */
    public void setMedName(String medName) {
        this.medName = medName;
    }
}

DAO Clas to retrive mediums in data base.
public static List getMediums(){
        PreparedStatement pStmt = null;
     Connection conn = null;
     boolean success = false;
     ResultSet rs = null;
     List medList = new ArrayList();
    
     try{
         conn = getConnection();
         
         String sql = " select * from MEDIUM ";
         pStmt = conn.prepareStatement(sql);
                  
         rs = pStmt.executeQuery();
         while(rs.next()){
             Medium med = new Medium();
             med.setMedId(rs.getInt("MED_ID"));
             med.setMedName(rs.getString("MEDIUM_NAME"));
             medList.add(med);
             }
        
        
        
     }catch(Exception e){
         e.printStackTrace();
         
     }finally{
         closeConnectionProp(conn,pStmt,rs);
     }
        
     return medList;
    
    }

JSP :

<select name="Medium" id="medium" multiple=true>
<option value="0">Choose A Medium</option>

<%
     for(int i=0; i<medList.size();i++){
         Medium med = (Medium)medList.get(i);
     %>
<option value="<%=med.getMedId()%>"><%=med.getMedName()%></option>
<%}%>
</select>


Servlet :
Retrive the multiple selections from jsp
String[] mediums = request.getParameterValues("medium");

for(int i=0;i<mediums.length;i++)
{
System.out.println(mediums[i]);
}

Reviews and Comments


PMP, CAPM, PMI is a registered certification mark of the Project Management Institute, Inc

Copyright © www.techfaq360.com 2016


About US | Contact US | Privacy Policy | Terms and Conditions  | Website disclaimer  | Cancellation and Refund Policy  | Shipping & Delivery Policy