Using conditional statement in JSP

Java Server Pages Code
What is a conditional statement and when to use it? I define conditional statement as a statement that makes the computer to decide on what to do base on the input coming from user or another program. Conditional statement is use when the program needs to decide on which code in the program is to be executed. To understand more about conditional statement I have here a simple example that will show on how to use the conditional statement. Let us take this machine problem as our example and the machine problem I “Write a program in Java Server Pages or JSP that will determine if the input age is qualified or not qualified to vote the qualifying age is 18 years old and above.”

Before creating our program we need first to analyze by identifying what is our input, what to be process and how to process it and what will be our output. Base on the machine problem we need only one input and that is the age of a person, after that our program will determine wither the age is qualified or not to vote and the criteria in order that a person can vote is that the age should be 18 years old and above. If the program finds out that the age is 18 years old and above it will display or print the words “you are qualified to vote” otherwise it will display the words “you are not qualified to vote” in a web browser.

Our interface needs to have a label that will inform the user to enter or type the age and a text box so that the user can type. A button is also needed in our interface and it will be use to trigger the event. The source code for our interface is shown below. You need to save the code with a filename “frmage.jsp”.

<html>
<form action="determinage.jsp" method="post">
<table border="0" bgcolor="green">
<tr>
<td>
Type your age:
</td>
<td>
<input type="text" name="txtage" size="20">
</td>
</tr>
<tr colspan="2">
<td>
<input type="submit" value="Determine">
</td>
</tr>
</table>
</form>
</html>

After creating the user interface that will be use by user to type their age we need to create the source code in Java Server Pages or JSP. The if-else conditional statement will be use because this statement will execute the code if the condign is true and if not the code in the else clause will be executed. The Java Server Pages sour code is shown below.

<%
String strage= request.getParameter("txtage");
int intage = Integer.parseInt(strage);
if(intage >= 18){
out.println("You are qualified to vote");
}else{
out.println("You are not qualified to vote");
}
%>