The following examples shows a program to create a JSP page called AlertMessage.JSP that displays alert messages if username & password fields are blank and displays welcome user otherwise. As can be seen in the folowing code, the file AlertMessage.jsp contains an HTML form. Also, there is a validate() function in JavaScript. Basically, the validate function() cjecks whether the username or password fields are not blank. In such a case, it displays an alert message. Otherwise, it transfers the control to the Welcome.jsp file.

AlertMessage.jsp

<form name="frm1" method="post" onsubmit="validate()">
	Username <input type="text" name="name" id="name"><br>
	password <input type="password" name="pswd" id="pswd"><br>
	<input type="submit" value="show">
</form>
<script language="Javascript">
	function validate()
	{
	var un = document.getElementById("name").value;
	var pswd = document.getElementById("pswd").value;
	if(!un)
	{
		alert("Username is required");
		return;
	}
	else if(!pswd)
	{
		alert("password is required");
		return;
	}
	else
	{
		document.frm1.setAttribute("action","Welcome.jsp");
	}
}
</script>

Welcome.jsp

<%@ 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">
<title>Insert title here</title>
</head>
<body>
	<H1>
		<%
			out.print("Welcome "+request.getParameter("name"));
		%>
	</H1>
</body>
</html>

Output

The Output of User Welcome Message in JSP
The Output of User Welcome Message in JSP

Once the user clicks on the Show button, a welcome message will be displayed. The following output shows it.

Welcome Message
Welcome Message