Java Servlet Post Variable Problems


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Java Servlet Post Variable Problems

Hybrid View

  1. #1
    nathan Guest

    Java Servlet Post Variable Problems


    I have beggining to develop web based apps utilizing Java Servlets, in
    order to expand my development areas to J2ee platforms. and I have come
    across an issue when retrievimg parameter values using the HttpServlets Request
    object. When I have a input field being retrieved, the data will be written
    to the database, using jdbc as expected. However, comparison of the retrieved
    values seems to fail miserably. Below attempts to show the problems I am
    having:

    public class customerprocess extends HttpServlet
    {
    public void doPost(HttpServletRequest req,
    HttpServletResponse resp)
    throws ServletException, java.io.IOException
    {

    // where "ActionType" is a Hidden Input Field
    // within the posted form
    String lActionType = req.getParameter("ActionType") ;


    // Now the idea here is to make a comparison on the
    // the field to determine what type of processing to
    // do, as I am trying to make generic servlets that will
    // will conduct a varitey of processes

    if (lActionType == "GetCustomer")
    {

    // Even if "GetCustomer" is the hidden INput
    // fieled value does does not evaluate to true


    // processing for GetCustomer request

    }



    // unfortionately , the field will hit the null condition
    // below

    if (lActionType == null)
    {

    // null processing hits here

    }


    }

    I am hitting the right page request, in that for data insert operations,
    the req.getParameter() populates the data in my database
    as expected. It is the comparision results that seem to be going haywire.
    Is there something else I should be doing here?

    thanks on any help here

    nathan


  2. #2
    Sharma Guest

    Re: Java Servlet Post Variable Problems


    Hi,
    try with java.lang.String.equalsIgnoreCase(java.lang.String) method to compare
    two strings.
    Sharma.

    "nathan" <nreiter@state.nm.us> wrote:
    >
    > I have beggining to develop web based apps utilizing Java Servlets, in
    >order to expand my development areas to J2ee platforms. and I have come
    >across an issue when retrievimg parameter values using the HttpServlets

    Request
    >object. When I have a input field being retrieved, the data will be written
    >to the database, using jdbc as expected. However, comparison of the retrieved
    >values seems to fail miserably. Below attempts to show the problems I am
    >having:
    >
    >public class customerprocess extends HttpServlet
    >{
    >public void doPost(HttpServletRequest req,
    >HttpServletResponse resp)
    >throws ServletException, java.io.IOException
    >{
    >
    > // where "ActionType" is a Hidden Input Field
    > // within the posted form
    > String lActionType = req.getParameter("ActionType") ;
    >
    >
    > // Now the idea here is to make a comparison on the
    > // the field to determine what type of processing to
    > // do, as I am trying to make generic servlets that will
    > // will conduct a varitey of processes
    >
    > if (lActionType == "GetCustomer")
    > {
    >
    > // Even if "GetCustomer" is the hidden INput
    > // fieled value does does not evaluate to true
    >
    >
    > // processing for GetCustomer request
    >
    > }
    >
    >
    >
    > // unfortionately , the field will hit the null condition
    > // below
    >
    > if (lActionType == null)
    > {
    >
    > // null processing hits here
    >
    > }
    >
    >
    >}
    >
    > I am hitting the right page request, in that for data insert operations,
    >the req.getParameter() populates the data in my database
    >as expected. It is the comparision results that seem to be going haywire.
    >Is there something else I should be doing here?
    >
    >thanks on any help here
    >
    >nathan
    >



  3. #3
    Ruchi Dhar Guest

    Re: Java Servlet Post Variable Problems


    equalsIgnoreCase is expensive than equals unless really required avoid the
    usage of equalsIgnoreCase. If you want best way of comparision use the following
    String lActionType = req.getParameter("ActionType") ;
    if((lActionType != null) && (lActionType.trim().equals("GetCustomer"))
    {
    //Do processing.
    }

    Even trim is memory expensive but useful in trailing out the white spaces
    which in no.of times useful from the parameter values.

    "Sharma" <meenush_chd@excite.com> wrote:
    >
    >Hi,
    >try with java.lang.String.equalsIgnoreCase(java.lang.String) method to compare
    >two strings.
    >Sharma.
    >
    >"nathan" <nreiter@state.nm.us> wrote:
    >>
    >> I have beggining to develop web based apps utilizing Java Servlets,

    in
    >>order to expand my development areas to J2ee platforms. and I have come
    >>across an issue when retrievimg parameter values using the HttpServlets

    >Request
    >>object. When I have a input field being retrieved, the data will be written
    >>to the database, using jdbc as expected. However, comparison of the retrieved
    >>values seems to fail miserably. Below attempts to show the problems I

    am
    >>having:
    >>
    >>public class customerprocess extends HttpServlet
    >>{
    >>public void doPost(HttpServletRequest req,
    >>HttpServletResponse resp)
    >>throws ServletException, java.io.IOException
    >>{
    >>
    >> // where "ActionType" is a Hidden Input Field
    >> // within the posted form
    >> String lActionType = req.getParameter("ActionType") ;
    >>
    >>
    >> // Now the idea here is to make a comparison on the
    >> // the field to determine what type of processing to
    >> // do, as I am trying to make generic servlets that will
    >> // will conduct a varitey of processes
    >>
    >> if (lActionType == "GetCustomer")
    >> {
    >>
    >> // Even if "GetCustomer" is the hidden INput
    >> // fieled value does does not evaluate to true
    >>
    >>
    >> // processing for GetCustomer request
    >>
    >> }
    >>
    >>
    >>
    >> // unfortionately , the field will hit the null condition
    >> // below
    >>
    >> if (lActionType == null)
    >> {
    >>
    >> // null processing hits here
    >>
    >> }
    >>
    >>
    >>}
    >>
    >> I am hitting the right page request, in that for data insert operations,
    >>the req.getParameter() populates the data in my database
    >>as expected. It is the comparision results that seem to be going haywire.
    >>Is there something else I should be doing here?
    >>
    >>thanks on any help here
    >>
    >>nathan
    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center