0

How to check if the dropdown list has no value with this code?

<td width="168"><select name="OrgType" class="txtbox" tabindex="1">
                            <option value="1">Please choose...</option>
                            <%
                                Iterator it2 = Org_Items2.iterator();
                                while (it2.hasNext())
                                {
                                    OrganizationItems orgItem2 = (OrganizationItems) it2.next();
                                    //EMSItems newsItem4 = new EMSItems();
                            %>
                            <option value=<%=orgItem2.getOrgTypeId()%>><%=orgItem2.getOrgTypeName()%></option>
                            <%
                                }
                            %>

                    </select></td>
                    <td width="120">
                        <%
                            if (errors.containsKey("OrgType"))
                            {
                                out.println("<span class=\"warning\">" + errors.get("OrgType") + "</span>");
                            }
                        %>
                    </td>

Then validation code

long OrgType = Integer.parseInt(req.getParameter("OrgType"));
    if ("1".equals(OrgType))
    {
        errors.put("OrgType", "Required");
    }
    else if(req.getParameter("OrgType") != null && !"".equals(req.getParameter("OrgType")))
    {
        long OrgTypeId = Integer.parseInt(req.getParameter("OrgType"));
        emsItem.setOrgTypeId(OrgTypeId);
    }

I am using in my header page the usebean tag

<%@ page import="java.util.Iterator"%>
<%@ page import="data_registration_items.OrganizationItems"%>
<jsp:useBean id="Org_Items2" scope="request" type="java.util.List" />

but when I tried to leave the value of the drop down list as "Please Select..." I got the error

java.lang.InstantiationException: bean Org_Items not found within scope
    org.apache.jsp.WEB_002dINF.jsp.organization_002dregistry_jsp._jspService(organization_002dregistry_jsp.java:79)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    web_registration.OrganizationServlet.doPost(OrganizationServlet.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    web.SecurityFilter.doFilter(SecurityFilter.java:55)

How can I correcltly verified if the dropdown list pass the correct value?


I tried to delete the usebean in the header. In my servlet I already have the

List<OrganizationItems> orgItems2 = new SetOrganizationType().OrgTypeList();
req.setAttribute("Org_Items2", orgItems2). 

under my doGet method...

in my jsp page I put the same code and the error says

Org_Items2 cannot be resolved

In the Option 2 I tried also but an error occured

java.lang.ClassCastException: java.util.LinkedList cannot be cast to java.util.ArrayList
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
PeterJohn
  • 589
  • 3
  • 8
  • 15

1 Answers1

0

java.lang.InstantiationException: bean Org_Items not found within scope

The <jsp:useBean> will throw this exception when you use the type attribute instead of the class attribute and the desired bean instance is not available in the specified scope.

So there are basically 2 solutions:

  1. Put the bean in the desired scope beforehand, so that <jsp:useBean> can find it.

    request.setAttribute("Org_Items", Org_Items);
    

    But, actually, this way you don't need the whole <jsp:useBean> at all. It would just be available directly in EL by ${Org_Items}.

  2. Use the class attribute instead so that <jsp:useBean> will autocreate one if it is not found in the scope. You should only change the FQN to java.util.ArrayList, because java.util.List has as being an interface no default constructor.

    <jsp:useBean id="Org_Items2" scope="request" class="java.util.ArrayList" />
    

See also:


Unrelated to the concrete problem. This piece of code contains some poor practices (using scriptlets, not respecting Java naming conventions, using <jsp:useBean>). I recommend to learn how to separate the concerns and look at our servlets wiki page. It also contains an example with validation and error handling.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I tried to delete the usebean in the header. In my servlet I already have the req.setAttribute("Org_Items2", orgItems2); – PeterJohn Jan 17 '13 at 08:22
  • So you're using a `LinkedList` instead of `ArrayList`? Change the `class` attribute to `java.util.LinkedList`. – BalusC Jan 17 '13 at 11:35