Solved

how to do data validation by java, not java script

Posted on 2007-12-06
6
972 Views
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>
                                </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
Comment
Question by:wasabi3689
  • 2
6 Comments
 
LVL 19

Accepted Solution

by:
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.
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
 
LVL 1

Assisted Solution

by:andythedestroyer
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 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
 

Author Comment

by:wasabi3689
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");
         
         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
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 20450857
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
JAVA API design with micro service cloud in mind 1 77
IntelliJ and Eclipse Neon 2 installations 4 36
Html split(text) 2 28
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

749 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