Updated April 12, 2023
Introduction to JSP Alert
Whenever the user validation process the alert box is mainly used in that time because the user will understand easily for the credentials or registration data is valid or not. The same process is done on the JSP alert box also wherever we need the alert or triggers we will use the alert function in the javascript and we also used windows.alert() method also performed the same scenario wherever we have the alert message we can use default methods in that page basically we used the forms for creating the html ui tags and navigate into the jsp web page then using submit or any other user-customized tags it calls directly to the javascript functions.
Syntax:
Every jsp tags have their own features and syntax of every tag elements. The JSP Alert feature have the syntax for using the web pages.
<html>
<head>
<script>
function functionname()
{
alert("");
---some java script logic codes------
}
</script>
</head>
<body>
The above codes are the basic syntax of the jsp alert box it will be called upon the javascript function so it will display the browser itself on the same web page.
How does JSP Alert Work?
The jsp alert box will be used more often to make sure the user data will come to the browser after validation it will show to the user screen. The alert box will take the focus away from the current window and forces to the browser for reading the message. I suppose the user will not enter any values into the ui elements like textbox etc it will display the pop message like “enter the values or value is required” it will highlight the values in the ui elements. The alert() method will always be displayed with the alert box have some specified messages after clicking ok button or any other ui tags it will display in the user browser screens whenever the alert box is most often used to make sure the information data comes through to the particular user and also the alert box it takes the focus away from the current user window and it forces to the user browsers to read the message. When we open any kind of web sites we might see alert pop-up box will appear at a lot of times that boxes will show the alert for the users to do something in the current web page process when the script alert box is triggered once the screen shows the small dialog box up and display with some text values which is already inserted it in codes. Based on the above scenario javascript differs with three kinds of pop-up or alert boxes 1. Alert box,2.Confirm-box,3.Prompt box. Each kind of boxes will be shown with the user browser is in normal behavior but some functionality wise it may differ from one with another Alert box is mostly used for alerting purpose for the user authentication, confirm box is checked whether the user will accept or not for the particular functionality changes of the web page screen changes. The reflected changes will be reflected in particular web pages after the user confirmation.
The prompt boxes are used by the users in the browsers for getting the user input values while entering the web page before in the browser screen. The same like confirm box users also check whether the prompt box text is accepted or not based on the user input it may reflect the changes in the user browser web pages. Whenever the jsp codes used for the pop-up box like the alert box the user activity and also the sessions will be tracked and stored in the log files and cookies. So the user will be more careful and alerted for clicking the alert or some other pop-up boxes in the web browsers. Using window.alert(“) or simply alert(“) window keyword is not mandatory in the jsp codes not only in the alert type of boxes it will be applicable for confirm-box and prompt boxes, so the window prefix is not mandatory for this kind of pop-up boxes.
Examples to Implement JSP Alert
Below are the examples mentioned:
Example #1
Code:
<html>
<body>
<form action = "second.jsp" method = "POST">
Name: <input type = "text" name = "n">
<br/><br/>
Password: <input type = "text" name = "pass" /><br/><br/>
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function sample()
{
for(var i=0; i < document.f.shirt.length; i++)
{
if(!document.f.shirt[i].checked)
{
alert("Please Select Your favorite tshirts size");
return false;
}
else
{
alert("Click OK to display your choice");
return true;
}
}
}
</script>
<title>Demo</title>
</head>
<body>
<form name="f" onsubmit="sample()">
<h3>Select your favorite T-Shirts Size</h3>
<p><input type="checkbox" name="shirt" value="XXXL"/>XXXL</p>
<p><input type="checkbox" name="shirt" value="Cotton"/>Cotton</p>
<p><input type="checkbox" name="shirt" value="Fabric"/>Fabric</p>
<p><input type="checkbox" name="shirt" value="Milange"/>Milange</p>
<p><input type="checkbox" name="shirt" value="Yarn"/>Yarn</p>
<p><input type="submit" value="submit"/>
</form>
<%
String s[]= request.getParameterValues("shirt");
if(s != null)
{
%>
<h4>My Favorite t-shirt size</h4>
<ul>
<%
for(int i=0; i<s.length; i++)
{
%>
<li><%=s[i]%></li>
<%
}
%>
</ul>
<%
}
%>
</body>
</html>
Output:
Examples #2
Code:
<html>
<head>
<script>
function demo(){
if (document.f.userID.value == ""){
alert ( "Please enter the User ID." );
document.loginform.userID.focus();
return false;
}
if (document.f.password.value == ""){
alert ( "Please enter the password." );
document.userform.password.focus();
return false;
}
alert ( "Welcome User thanks for login" );
return true;
}
</script>
</head>
<body>
<form name="f" method="post" onsubmit="return demo();">
<table width="253px" border=2 style="background-color:green;">
<tr><td colspan=3 align="center" style="font-weight:bold;font-size:22pt;" align="center">Login Form</td>
</tr>
<tr><td colspan=4> </td>
</tr>
<tr><td style="font-size:13pt;" align="center">User ID:</td>
<td><input type="text" name="userID" value=""></td>
</tr>
<tr><td style="font-size:13pt;" align="center">Password:</td>
<td><input type="password" name="password" value=""></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>
Output:
Example #3
Code:
<!DOCTYPE html>
<html>
<body>
<p>Welcome To My Domain.</p>
<button onclick="demo()">Click it</button>
<script>
function demo() {
alert("Welcome To My Domain!");
}
</script>
</body>
</html>
Output:
Conclusion
The jsp alert box has one of the features for ui functionality on the web page. It is mostly used in the web security level procedures and the security codes are written like javascript model it has compatible with jsp.
Recommended Articles
This is a guide to JSP Alert. Here we discuss an introduction to JSP Alert with appropriate syntax, how does it works with respective examples for understanding. You can also go through our other related articles to learn more –