-
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
-
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
>
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks