Examples of JSP
Example of First JSP Page
Simple JSP: Print Current Time
Example of JSP Variable
Example of Custom Page
Example of Array in JSP
Example Validation in JSP
Example of Login Form in JSP
Example of First JSP Page
<%@ 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>JSP </title> </head> <body> Sum of 2+2 is <%= 2+2 %> </body> </html>Output
Simple JSP: Print Current Time
Simple JSP: Print Current Time
<%@ 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>
Current Date time: <%=new java.util.Date()%>
</body>
</html>
Output
Example of JSP Variable
Example of JSP Variable
Variable.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">
<%
String str="str is String variable";
%>
<%
int i=5; %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>JSP Varible</title>
</head>
<body>
<b>
int value : <%=i%>
String value : <%=str%>
</b>
</body>
</html>
Output
Example of Custom Page
Example of Custom Page
JSPExample.java
package com.learn;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class JSPExample extends SimpleTagSupport {
private String message;
public void setMessage(String msg) {
this.message = msg;
}
StringWriter sw = new StringWriter();
public void doTag() throws JspException, IOException {
if (message != null) {
/* Use message from attribute */
JspWriter out = getJspContext().getOut();
out.println( message );
}
else {
/* use message from the body */
getJspBody().invoke(sw);
getJspContext().getOut().println(sw.toString());
}
}
}
custom.tld
msg.jsp1.0 2.0 Example TLD with Body Hello com.learn.JSPExample scriptless message
<%@ taglib prefix="s" uri="/WEB-INF/custom.tld"%>
<html>
<head>
<title>A sample custom tag</title>
</head>
<body>
<s:Hello message="This is custom tag" />
</body>
</html>
Output
Example of Array in JSP
Example of Array in JSP
Array.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">
<%
String[] array={"jtechies","jtechies","jtechies","jtechies","jtechies","jtechies"};
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Variable</title>
</head>
<body>
<%
int i=0;
for(i=0;i<array.length;i++)
{
out.print(" Array Elements :-"+array[i]+"<br/>");
}
%>
</body>
</html>
Output
Example Validation in JSP
Example Validation in JSP
Validation.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>JSP Variable
<script>
function validateForm()
{
if(document.frm.username.value=="")
{
alert("User Name should be left blank");
document.frm.username.focus();
return false;
}
else if(document.frm.pwd.value=="")
{
alert("Password should be left blank");
document.frm.pwd.focus();
return false;
}
}
</script>
</head>
<body>
<form name="frm" method="get" action="validateInput.jsp"
onSubmit="return validateForm()">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="32%"> ></td>
<td width="98%"> ></td>
</tr>
<tr>
<td>UserName </td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="pwd" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td7gt; </td>
</tr>
</table>
</form>
</body>
</html>
Output
Example of Login Form in JSP
Example of Login Form in JSP
index.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>Index Jsp</title>
<link href="css/common/bootstrap.css" rel="stylesheet">
</head>
<body>
<h1>Login Form</h1>
<form class="well" action="login.jsp" method ="post">
<label<Userid</label>
<input type="text" name="t1" class="span3"
placeholder="Type Userid">
<br>
<label>Password</label>
<input type="password" name="t2" class="span3"
placeholder="Type Password">
<br>
<button type="submit" class="btn">Submit</button>
</form>
</body>
</html>
Login.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>
<%
String userid = request.getParameter("t1");
String password = request.getParameter("t2");
if(userid.equals(password))
{
%>
<table border="1">
<tr>
<th>Userid</th>
<th>Password</th>
</tr>
<tr>
<td><%=userid %></td>
<td><%=password %></td>
</tr>
</table>
<%
}
else
{
%>
Invalid Userid and Password
<%
}
%>
</body>
</html>
Output


