First of all, what is
Java code doing in the
JSP? That's an obsolete technology from 12 years ago.
You should be writing JSPs using the JSTL and EL, not Java scriptlets.
Secondly, JSP is a server-side templating mechanism. The JSP runs on the server in order to create the HTML page to send to the browser. So you've got the cart ahead of the horse here -- the validation code is running before the page is even sent to the browser.
Also it appears that the JSP is submitting to itself. Validation code should be in the
servlet to which the form is submitted. Forms should never be submitted to a JSP.
It amy help to understand how JSPs operate, by reading
this article.
Modern web app structure is the subject of
this article.