Solved

how to do data validation by java, not java script

Posted on 2007-12-06
6
973 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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 second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 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