Example 1:
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="[Link] %>
<html>
<head>
<title>JSP is Easy</title>
</head>
<body bgcolor="white">
<h1>JSP is as easy as ...</h1>
1 + 2 + 3 = <c:out value="${1 + 2 + 3" />
</body>
</html>
Example 2:
<%@ page language="java" contentType="text/html"%>
<%@ page import="[Link].*,[Link].*" %>
<html>
<head>
<title>Date JSP</title>
</head>
<% SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy"); %>
<body>
<h1>Welcome to Tomcat! Today is <%= [Link](new Date()) %></h1>
</body>
</html>
Example 3:
<html>
<head><title>First JSP</title></head>
<body>
<%
double num = [Link]();
if (num > 0.95) {
%>
<h2>You'll have a luck day!</h2><p>(<%= num %>)</p>
<%
} else {
%>
<h2>Well, life goes on ... </h2><p>(<%= num %>)</p>
<%
}
%>
<a href="<%= [Link]() %>"><h3>Try Again</h3></a>
</body>
</html>
Example 4:
import [Link].*;
import [Link].*;
import [Link].*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
private String message;
public void init() throws ServletException {
message = "Hello World";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
[Link]("text/html");
PrintWriter out = [Link]();
[Link]("<h1>" + message + "</h1>");
}
public void destroy() {
// do nothing.
}
}
Example 5
<%@ page language="java" contentType="text/html" %>
<%!
int globalCounter = 0;
%>
<html>
<head>
<title>A page with a counter</title>
</head>
<body bgcolor="white">
This page has been visited: <%= ++globalCounter %> times.
<p>
<%
int localCounter = 0;
%>
This counter never increases its value: <%= ++localCounter %>
</body>
</html>
Example 6:
<html>
<body>
<p>This or that
<%!int x=50;%>
<%!"the val is"+x%>
</p>
</body>
</html>
Example 7:
<html>
<body>
<% for (int i = 0; i < 5; i++) { %>
<p>
<% if (i%2==0) { %>
Even number
<% } else { %>
Odd number
<% } %>
</p>
<% } %>
</body>
</html>
Example 8:
<!DOCTYPE html>
<html>
<head>
<title>Printing 10 Even and Odd numbers</title>
</head>
<body>
<%
[Link]("<b>First 10 Even numbers are</><br>");
for( int i=1;i<=20;i++)
{
if(i%2==0)
{
[Link]("<br><b>"+i+"</b>");
}
}
[Link]("<br><br><b> First 10 odd numbers are</b><br>");
for( int i=1;i<=20;i++)
{
if(i%2!=0)
{
[Link]("<br><b>"+i+"</b>");
}
}
%>
</body>
</html>
Example 9:
<% String name = [Link]("name"); %>
<html>
<body>
<p>
<% if (name != null) { %>
Hello, <%= name %>.
<% } else { %>
Welcome, anonymous user.
<% } %>
How are you?
</p>
</body>
</html>
Example 10:
<html>
<body>
<%!
int cube(int n){
return n*n*n;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
Example 11:
<%@ taglib uri = "[Link] prefix = "c" %>
<c:out value="${1 + 2 + 3}" />
<html>
<head>
<title><c:if> Tag Example</title>
</head>
<body>
<c:set var = "salary" scope = "session" value = "${2000*2}"/>
<c:if test = "${salary > 2000}">
<p>My salary is: <c:out value = "${salary}"/><p>
</c:if>
</body>
</html>
Example 12:
<%@ taglib uri = "[Link] prefix = "c" %>
<html>
<head>
<title> Tag Example</title>
</head>
<body>
<c:import var = "data" url = "[Link]
<c:out value = "${data}"/>
</body>
</html>
Example 13:
<%@taglib uri="[Link] prefix ="c"%>
<html>
<head><title>gg</title></head>
<body>
${11-2}<br>
${'gf'}<br>
${'star'>'gold'}<br>
${1 eq 2}<br>
${10 mod 3}<br>
${4 div 2}
</body>
</html>
Example 14:
[Link]
<%@taglib uri="[Link] prefix ="c"%>
<html>
<body>
<c:set var="test" scope="session" value="50"/>
<a href="[Link]">click here</a>
</body>
</html>
[Link]
<%@taglib uri="[Link] prefix ="c"%>
<html>
<body>
<c:out value="${[Link]}"/>
</body>
</html>
Example 15:
<%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title>c:param Example2</title>
</head>
<body>
<c:redirect url="[Link]" >
<c:param name="UserId" value="5"/>
<c:param name="UserName" value="script"/>
</c:redirect>
</body>
</html>
[Link]
${[Link]}
${[Link]}
Example 16:
%@ taglib uri="[Link] prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="x" scope="session" value="${30}"/>
<c:if test="${x > 80}">
<p>My val is: <c:out value="${income}"/><p>
</c:if>
</body>
</html>
Example 17:
<html>
<head>
<title>add</title>
</head>
<body>
<%String s=[Link]("name");
String q=[Link]("t1");%>
<%=s%>
</body>
</html>
Example 18:
<html>
<body>
<%!
int fact(int n){
int i,f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
%>
<%= "Fact is:"+fact(5) %>
</body>
</html>
Example 19:
<html>
<head>
<title>JSP include with parameters example</title>
</head>
<body>
<jsp:forward page="[Link]">
<jsp:param name="fname" value='<%=[Link]("fname")%>'/>
<jsp:param name="lname" value='<%=[Link]("lname")%>'/>
</jsp:forward>
</body>
</html>
[Link]
${[Link]}
${[Link]}
Example 20:
<%
int n=[Link]([Link]("val"));
[Link]([Link]*n*n);
%>
Example 21:
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="[Link] %>
<html>
<head>
<title>User Info Entry Form</title>
</head>
<body bgcolor="white">
<form action="[Link]" method="post">
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>Birth Date:</td>
<td>
<input type="text" name="birthDate">
</td>
<td>(Use format yyyy-mm-dd)</td>
</tr>
<tr>
<td>Email Address:</td>
<td>
<input type="text" name="emailAddr">
</td>
<td>(Use format name@[Link])</td>
</tr>
<tr>
<td>Gender:</td>
<td>
<input type="radio" name="gender" value="m"
checked>Male<br>
<input type="radio" name="gender" value="f">Female
</td>
</tr>
<tr>
<td>Lucky number:</td>
<td>
<input type="text" name="luckyNumber">
</td>
<td>(A number between 1 and 100)</td>
</tr>
<tr>
<td>Favorite Foods:</td>
<td>
<input type="checkbox" name="food" value="z">Pizza<br>
<input type="checkbox" name="food" value="p">Pasta<br>
<input type="checkbox" name="food" value="c">Chinese
</td>
</tr>
<tr>
<td colspan=2>
<input type="submit" value="Send Data">
</td>
</tr>
</table>
</form>
You entered:<br>
Name: <c:out value="${[Link]}" /><br>
Birth Date: <c:out value="${[Link]}" /><br>
Email Address: <c:out value="${[Link]}" /><br>
Gender: <c:out value="${[Link]}" /><br>
Lucky Number: <c:out value="${[Link]}" /><br>
Favorite Food:
<c:forEach items="${[Link]}" var="current">
<c:out value="${current}" />
</c:forEach>
</body>
</html>
Example 22:
<%@ taglib prefix="c" uri="[Link] %>
<html>
<body>
<c:set var="x" value="4"/>
<c:set var ="y" value="8"/>
<c:out value="${x+y}"/>
${'script'}
${4>8}
${3 div 4}
${10 mod 4}
</body>
</html>
Example 23:
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="[Link] %>
<html>
<head>
<title>Request Info</title>
</head>
<body bgcolor="white">
The following information was received:
<ul>
<li>Request Method:
<c:out value="${[Link]}" />
<li>Request Protocol:
<c:out value="${[Link]}" />
<li>Context Path:
<c:out value="${[Link]}" />
<li>Servlet Path:
<c:out value="${[Link]}" />
<li>Request URI:
<c:out value="${[Link]}" />
<li>Request URL:
<c:out value="${[Link]}" />
<li>Server Name:
<c:out value="${[Link]}" />
<li>Server Port:
<c:out value="${[Link]}" />
<li>Cookies:<br>
<c:forEach items="${[Link]}" var="c">
<b><c:out value="${[Link]}" /></b>:
<c:out value="${[Link]}" /><br>
</c:forEach>
<li>Headers:<br>
<c:forEach items="${headerValues}" var="h">
<b><c:out value="${[Link]}" /></b>:
<c:forEach items="${[Link]}" var="value">
<br>
<c:out value="${value}" />
</c:forEach>
<br>
</c:forEach>
</ul>
</body>
</html>
Example 24:
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="[Link] %>
<html>
<head>
<title>Us Ino Entry Form</title>
</head>
<body>
<form action="[Link]" method="post">
<input type="text" name="userName"/>
<input type="submit" value="Send Data">
</form>
<c:if test="${[Link] ==''}">
<p> Please enter your Name </p>
</c:if>
<c:if test="${!empty [Link]}">
<p>not empty </p>
</c:if>
Name :<c:out value="${[Link]}"/>
</body>
</html>
Example 25:
<html>
<body>
<form action="[Link]">
No1:<input type="text" name="n1" /><br/><br/>
No1:<input type="text" name="n2" /><br/><br/>
<input type="submit" value="divide"/>
</form>
</body>
</html>
[Link]
<%@ page errorPage="[Link]" %>
<%
String num1=[Link]("n1");
String num2=[Link]("n2");
int a=[Link](num1);
int b=[Link](num2);
int c=a/b;
[Link]("division of numbers is: "+c);
%>
[Link]
<%@ page isErrorPage="true" %>
<h3>Sorry an exception occured!</h3>
Exception is: <%= exception %>
Example 26:
<%@ taglib prefix = "c" uri = "[Link] %>
<%@ taglib prefix = "fmt" uri = "[Link] %>
<html>
<body>
<c:set var="x" value="<%=new [Link]()%>"/>
<font color="magenta"> <h3> <c:out value="${x}"/> </h3></font>
<font color="green">
<fmt:formatDate type="time" value="${x}"/><br>
<fmt:formatDate timeStyle="short" type="time" value="${x}"/><br>
<fmt:formatDate timeStyle="long" type="time" value="${x}"/><br>
<fmt:formatDate timeStyle="medium" type="time" value="${x}"/><br> </font>
Date <font color="red"><fmt:formatDate type="date" value="${x}"/></font><br>
<fmt:formatDate dateStyle="short" type="date" value="${x}"/><br>
<fmt:formatDate pattern = "YYYY-MM-dd" value = "${x}" />
</body>
</html>
Example 27:
<%@ taglib prefix = "c" uri = "[Link] %>
<%@ taglib prefix = "fmt" uri = "[Link] %>
<html>
<body>
<c:set var="x" value="hello , "how are you "?"/>
<c:out value="${x}"/>
</body>
</html>
Example 28:
<%@ taglib prefix="c" uri="[Link] %>
<html>
<body>
<c:set var ="para" scope="session" value="22"/>
<a href="[Link]"> cclick </a>
</body>
</html>
[Link]
<%@ taglib prefix="c" uri="[Link] %>
<html>
<body>
<c:out value="${[Link]}"/>
</body>
</html>
Example 29:
<%@ taglib uri="[Link] prefix="c"%>
<%@ taglib uri="[Link] prefix="sql"%>
<html>
<head>
<title>sql:quy Tag</title>
</head>
<body>
<sql:setDataSource var="db" driver="[Link]"
url="jdbc:mysql://localhost/mysql1"
user="root" password=""/>
<c:set var = "CustomerId" value = "30"/>
<!--<sql:update dataSource = "${db}" var = "count">
DELETE FROM customers WHERE CustomerId = 8
</sql:update>-->
<sql:query dataSource="${db}" var="rs" >
SELECT * from customers;
</sql:query>
<table border="2" width="100%">
<tr>
<th>Student ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<c:forEach var="table" items="${[Link]}">
<tr>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
</tr>
</c:forEach>
</table>
</body>
</html>
Example 30:
<%@ taglib uri = "[Link] prefix = "c"%>
<%@ taglib uri = "[Link] prefix = "sql"%>
<jsp:useBean id="tt" class="[Link]"/>
<html>
<body>
<form action="[Link]" >
<input type="text" name="txtUsername" />
<input type="submit" name="click me"/>
<%int studentID=35;%>
<sql:setDataSource var="db" driver="[Link]"
url="jdbc:mysql://localhost/mysql1" user="root" password=""/>
<sql:update dataSource="${db}" var="count">
update students set dob=? where first=?
<sql:param value="${tt}"/>
<sql:param value="${[Link]}"/>
</sql:update>
<sql:query dataSource="${db}" var ="result">
SELECT * from students
</sql:query>
<table border="1">
<tr> <th>ID </th>
<th>first</th>
<th>last</th>
<th>dob</th>
<tr>
<c:forEach items="${[Link]}" var="row">
<tr>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
<td><c:out value="${[Link]}"/></td>
</tr>
</c:forEach>
</body>
</html>
Example 31:
<%@ taglib uri = "[Link] prefix = "c"%>
<%@ taglib uri = "[Link] prefix = "sql"%>
<jsp:useBean id="tt" class="[Link]"/>
<html>
<body>
<%int sid=104;%>
<sql:setDataSource var ="db" driver="[Link]"
url="jdbc:mysql://localhost/mysql1" user="root" password=""/>
<sql:transaction dataSource="${db}">
<sql:update>
UPDATE bank SET balance = balance - 1000
WHERE accno = 678
</sql:update>
<sql:update>
UPDATE bank SET balance = balance + 1000
WHERE accno = 563
</sql:update>
</sql:transaction>
<sql:query dataSource="${db}" var="results">
select * from bank
</sql:query>
<table border="2">
<tr>
<th>id</th>
<th>first</th>
<th>last</th>
<th>dob</th></tr>
<c:forEach items ="${[Link]}" var="row1">
<tr>
<td> <c:out value="${[Link]}"/></td>
<td> <c:out value="${[Link]}"/></td>
<td> <c:out value="${[Link]}"/></td>
</tr>
</c:forEach>
</body>
</html>
Example 32 :
Java beans:
package [Link];
public class Greatest{
public int great(int ,int b,int c)
{
if(a>b&&a>c)
{
return a;
}
else if(b>a&&b>c)
{
return b;
}
else
{
return c;
}
}
}
----------------------------------------------------------
package [Link];
public class Operations{
public int factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++){
fact=fact*i;
}
return fact;
}
}
package [Link];
import [Link].*;
public class Emailadd
{
public boolean emailval(String email)
{
if(email!=null)
{
Pattern p = [Link]("^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,3})$");
Matcher m = [Link](email);
if([Link]())
{
return true;
}
else
{
return false;
}}
else
{
return false;
}
}
}
package [Link];
public class Tester
{
public boolean valid(String username)
{
if(username==null || [Link]()==0)
{
return false;
}
if([Link]()<5 || [Link]()>10)
{
return false;
}
return true;
}
}
[Link]
<html>
<body>
<form action="[Link]">
<input type="text" name="txtUsername"><br><br>
<input type="text" name="txtEmail"><br><br>
<input type ="submit" value="click me"><br>
</form>
<jsp:useBean id="obj" class="[Link]"/>
<jsp:useBean id="obj1" class="[Link]"/>
<jsp:useBean id="obj2" class="[Link]"/>
<jsp:useBean id="obj3" class="[Link]"/>
<%
int m=[Link](5);
[Link]("cube of no 5 is "+m);
[Link]("<br>");
int x=[Link](5,17,8);
[Link]("Greatest is "+x);
[Link]("<br>");
String s=[Link]("txtUsername");
boolean y=[Link](s);
if(y==true)
{
[Link]("Username valid");
}
else
{
[Link]("Username not valid");
}
String email=[Link]("txtEmail");
boolean y1=[Link](email);
if(y1==true)
{
[Link]("Email valid");
}
else
{
[Link]("Email not valid");
}
%>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
[Link]
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]
version="3.1"
metadata-complete="true">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>Factorial</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet>
<servlet-name>Greatest</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet>
<servlet-name>Username</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet>
<servlet-name>Emailadd</servlet-name>
<servlet-class>[Link]</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Factorial</servlet-name>
<url-pattern>/hello/sri</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
</web-app>