how to do data validation by java, not java script

Posted on 2007-12-06
Last Modified: 2012-05-05
I have data validation by JS. Now I want to write another set's data input validation by java, not java script - server-side validation. How can I do that?

Here are my input parameters

<form class="portlet-generic-container"  style="clear:left;" name="NetFlow" action="<portlet:actionURL/>" method="post" onSubmit="return ValidateForm()" >
                        <div class="portlet-generic-container" style="padding-top:5px;padding-bottom:5px; clear:left;" id="ICC">
                                <fieldset class="portlet-generic-container" style="padding-top:5px;padding-bottom:5px; clear:left;">
                                <legend class="portlet-generic-container" style="padding-top:5px;padding-bottom:5px; clear:left;">
                                    <label class="portlet-form-label" style="color:black; size:medium">Type</label>
                                <input name="VolContID" type="hidden" value="999" >
                                <input name="radio_button" type="radio" value="imbalance"  onclick="toggleBox('IC',1);"  >
                                <label class="portlet-form-label" style="color:black; size:medium">Imbalance</label>                              
                                <input name="radio_button" type="radio" value="volume" checked onclick="toggleBox('IC',0);" >
                                <label class="portlet-form-label"  style="color:black; size:medium">Volume</label>
                                <div style="padding-top:5px;padding-bottom:5px; visibility:hidden; clear:left;" id="IC">
                                <label class="portlet-form-label">Degree of Imbalance:</label>
                                <input name="DegImb" type="text" size="3" value="65" onkeyup="ValidateIntlNumber(this);">
                                <label class="portlet-form-label">( enter as 50% )</label>&nbsp; &nbsp;
                                <label class="portlet-form-label">Min. Flows Per Hour:</label>
                                <input name="TotalFlowImb" type="text" size="3" value="10" onkeyup="ValidateIntlNumber(this);" > 
                                <label class="portlet-form-label">( enter as 5, 10, 20... )</label>
        <div class="portlet-generic-container" style="clear:left;padding-top:5px;padding-bottom:5px;">
            <label class="portlet-form-label">
            # of Results: </label><input type="text" name="Num_Results" size="3" maxlength="3" value="10" onkeyup="ValidateIntlNumber(this);" >
        <div class="portlet-generic-container" style="clear:left; padding-top:5px;padding-bottom:5px;">
                <label class="portlet-form-label">Date Range:</label>
                <input type="text"  size="10" maxlength="20"  name= "BegDate" value="<%=y%>">
                <a href="javascript:showCal('Calendar1')">
                <img src="images/cal.jpg" alt="start date calendar icon" width="34" height="21" border="0"></a> -
                <input type="text" size="10" maxlength="20" name="EndDate" value=<%=s%> >
                <a href="javascript:showCal('Calendar2')">
                <img src="images/cal.jpg" alt="end date calendar icon" width="34" height="21" border="0"></a>
                <label class="portlet-generic-container" style="padding-top:5px;padding-bottom:5px; clear:left;" >
                <label class="portlet-form-label">( enter as mm/dd/yyyy)</label>  
                        Calendar cal2 =Calendar.getInstance();
                        Integer hr = new Integer( cal2.get(Calendar.HOUR_OF_DAY));
                        Integer min = new Integer(( cal2.get(Calendar.MINUTE)));
                        String MyNow = hr.toString()+":"+min.toString();          
        <div class="portlet-generic-container" style="clear:left;padding-top:5px;padding-bottom:5px;">
            <label class="portlet-form-label" style="color:black; size:medium">Time Period: </label>
                <select name="begtime"><option><%=MyNow%></option><option>01:00</option><option>02:00</option><option>03:00</option><option>04:00</option><option>05:00</option><option>06:00</option><option>07:00</option><option>08:00</option><option>09:00</option><option>10:00</option><option>11:00</option><option>12:00</option><option>13:00</option><option>14:00</option><option>15:00</option><option>16:00</option><option>17:00</option><option>18:00</option><option>19:00</option><option>20:00</option><option>21:00</option><option>22:00</option><option>23:00</option><option>24:00</option></select>
            - <select name="endtime"><option><%=MyNow%></option><option>01:00</option><option>02:00</option><option>03:00</option><option>04:00</option><option>05:00</option><option>06:00</option><option>07:00</option><option>08:00</option><option>09:00</option><option>10:00</option><option>11:00</option><option>12:00</option><option>13:00</option><option>14:00</option><option>15:00</option><option>16:00</option><option>17:00</option><option>18:00</option><option>19:00</option><option>20:00</option><option>21:00</option><option>22:00</option><option>23:00</option><option>24:00</option></select>
        <div class="portlet-generic-container" style="clear:left;padding-top:5px;padding-bottom:5px;">
            <label class="portlet-form-label">My Networks: </label>
                <select multiple id="networks" name="networks" type="text" class="portlet-form-input-field"  >
      <div class="portlet-document-footer-container" style="text-align:left;">    
          <input class="portlet-form-button" style="size:larger" type='submit' value="Adv.Fetch Results" name="Adv_Fetch_Results">
          <input class="portlet-form-button" style="size:larger" type="submit" value="Goto Simple Search" name="Goto_Sim_Search">

Here are the inputs I need to do server-side validation in java

1. Degree of Imbalance:<input name="DegImb" type="text">, must be an integer value
2. Min. Flows Per Hour:    <input name="TotalFlowImb" type="text">, must be an integer value
3.  # of Results: <input type="text" name="Num_Results">, must be an integer value
4. Begin Date <input type="text"  size="10" maxlength="20"  name= "BegDate"> must be a date value
5. End Date <input type="text" size="10" maxlength="20" name="EndDate" > must be a date value
6.  begtime <select name="begtime">, must be time value
7. <select name="endtime">, must be a time value

I want to do validation above by java. in the code, if not the value, throw out an exception. I want to do the



Can anyone give me sample code about above?

Question by:wasabi3689
  • 2
LVL 19

Accepted Solution

Kuldeepchaturvedi earned 50 total points
ID: 20421519
1. Degree of Imbalance:<input name="DegImb" type="text">, must be an integer value
2. Min. Flows Per Hour:    <input name="TotalFlowImb" type="text">, must be an integer value
3.  # of Results: <input type="text" name="Num_Results">, must be an integer value
4. Begin Date <input type="text"  size="10" maxlength="20"  name= "BegDate"> must be a date value
5. End Date <input type="text" size="10" maxlength="20" name="EndDate" > must be a date value
6.  begtime <select name="begtime">, must be time value
7. <select name="endtime">, must be a time value

in your action url servlet.
String  degImp = request.getParameter("DegImb");
int degImpint = Integer.parseInt(degImp);
// Same code for your other two variables.

Date begDate = DateFormat.getInstance().parse(request.getParamater("BegDate");
//same code for your second date.
Catch ( Exception e)
//validation failed.

Assisted Solution

andythedestroyer earned 50 total points
ID: 20421620
I would say that the cleanest way to do your validation is to use a validation framework. I know it requires a bit of reading but It will help in the long run.

Two such packages I suggest are the one from the SpringFramework ( note that it can be used without the rest of the framework) and the one from apache commons.

Once you get used to them you end up saving time not writing the same validation code over and over again

Author Comment

ID: 20450347
I got a solution but it's not perfect. Can anyone help me this solution become perfect. I create a file exact the same the one. Here it's my struture.

view.jsp ----a form for user to input parameters as above code

AdvView.jsp --- output result from view.jsp. If the output is not correct, go to ValidateView.jsp and ask input again.

<%@ page errorPage="ValidateView.jsp" %>

    <%@ page isErrorPage="true" %>
     <%//= exception %>
        String Num_Rslt = request.getParameter("Num_Results");
        String MyBegDate = request.getParameter("BegDate");
        String MyEndDate = request.getParameter("EndDate");
        String MyBegTime = request.getParameter("begtime");
        String MyEndTime = request.getParameter("endtime");
        String MyDegImb = request.getParameter("DegImb");
        String MyTotalFlowImb = request.getParameter("TotalFlowImb");
            int MyNumRstInt = Integer.parseInt(Num_Rslt);  
            int MyDegImbInt = Integer.parseInt(MyDegImb);    
            int MyTotalFlowImbInt = Integer.parseInt(MyTotalFlowImb);
            Date MyBegDateV = DateFormat.getInstance().parse(MyBegDate);
            Date MyEndDateV = DateFormat.getInstance().parse(MyEndDate);
            catch (Exception e)
       <%   } %>

ValidateView.jsp  --- the same as view.jsp but below

<%@ page errorPage="AdvView.jsp" %>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page isErrorPage="true" %>

    <%=exception %>

Now the problem is

When I input a wrong input, it has the fellowing

java.lang.NumberFormatException: For input string: "pp"

My questions are

1. I want to remove the words "      java.lang.NumberFormatException:"
2. Indicate which which input parameter such as # of Result: pp
3. so far this method is only can catch one input wrong. If I want to catch all wrong such as # of Result, date....

How to fix them?
LVL 19

Expert Comment

ID: 20450857
it can easily be done by making individual try catch statements...

            int MyNumRstInt = Integer.parseInt(Num_Rslt);  
}catch( NumberFormatException nf)
//put code to indicate error in rstint
Another try catch block here..

and so on..

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
advertisement module in core php 4 144
eclipse formatting 6 71
jar file executable 12 39
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now