• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 981
  • Last Modified:

how to do data validation by java, not java script

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>
                                </legend>
                                <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>
            </fieldset>
           </div>
           
        <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>
       
        <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>  
        </div>
                  <%
                        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>
        <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"  >
                 </select>  
               
        </div>
   
      <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">
      </div>
       
    </form>

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

try....


cath....


Can anyone give me sample code about above?



Thanks
0
wasabi3689
Asked:
wasabi3689
  • 2
2 Solutions
 
KuldeepchaturvediCommented:
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.
try{
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.
}
0
 
andythedestroyerCommented:
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 http://static.springframework.org/spring/docs/2.0.x/reference/validation.html ( note that it can be used without the rest of the framework) and the one from apache commons. http://commons.apache.org/validator/

Once you get used to them you end up saving time not writing the same validation code over and over again
0
 
wasabi3689Author Commented:
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");
         
         try{
            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)
            {
               
                e.getMessage();
     %>        
             
           
       <%   } %>
       



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?
 
0
 
KuldeepchaturvediCommented:
it can easily be done by making individual try catch statements...

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

and so on..
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now