Jspexample1.Jsp: 1. JSP Example Simple Print On Browser
Jspexample1.Jsp: 1. JSP Example Simple Print On Browser
jspExample1.jsp
<html>
<body>
<%
out.print("Hello World!");
%>
</body>
</html>
jspExample2.jsp
<html>
<body>
<%="Hello World!"%>
</body>
</html>
jspExample3.jsp
<%@ page language="java" import="java.util.*" errorPage="" %>
<html>
<body>
Current Date time: <%=new java.util.Date()%>
</body>
</html>
jspString.jsp
<%@ page language="java" errorPage="" %>
<%
String stVariable="this is defining String variable";
%>
<html>
<body>
String variable having value : <%=stVariable%>
</body>
</html>
jspInt.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
int iVariable=5;
%>
<html>
<body>
int variable having value : <%=iVariable%>
</body>
</html>
3. Long variable is same as int variable just long can store more bytes than int.
jspFloat.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%
float fVariable=567.345f;
%>
<html>
<body>
float variable having value : <%=fVariable%>
</body>
</html>
jspBoolean.jsp
<html>
<body>
<%
if(checkCondition==true)
{
out.print("This condition is true");
}
else
{
out.print("This condition is false");
}
%>
</body>
</html>
simpleArray.jsp
<html>
<body>
<%
int i=0;
for(i=0;i<stArray.length;i++)
{
out.print("stArray Elements :"+stArray[i]+"<br/>");
}
%>
</body>
</html>
stringArray.jsp
<html>
<body>
<%
int i=0;
for(i=0;i<stArray.length;i++)
{
out.print("stArray Elements :"+stArray[i]+"<br/>");
}
%>
</body>
</html>
intArray.jsp
<html>
<body>
<%
int i=0;
for(i=0;i<intArray.length;i++)
{
out.print("intArray Elements :"+intArray[i]+"<br/>");
}
%>
</body>
</html>
vectorArray.jsp
<html>
<body>
<%
int i=0;
for(i=0;i<vc.size();i++)
{
out.print("Vector Elements :"+vc.get(i)+"<br/>");
}
%>
</body>
</html>
ArrayList.jsp
<html>
<body>
<%
int i=0;
for(i=0;i<ar.size();i++)
{
out.print("ArrayList Elements :"+ar.get(i)+"<br/>");
}
%>
</body>
</html>
for.jsp
<body>
<%
for(int i=0;i<=10;i++)
{
out.print("Loop through JSP count :"+i+"<br/>");
}
%>
</body>
</html>
while.jsp
<body>
<%
int i=0;
while(i<=10)
{
out.print("While Loop through JSP count :"+i+"<br/>");
i++;
}
%>
</body>
</html>
doWhile.jsp
<body>
<%
int i=0;
do{
out.print("While Loop through JSP count :"+i+"<br/>");
i++;
}
while(i<=10);
%>
</body>
</html>
ifelse.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>while loop in JSP</title>
</head>
<body>
<%
String sName="joe";
String sSecondName="noe";
if(sName.equals("joe")){
out.print("if condition check satisfied JSP count :"+sName+"<br>");
}
radioInput.jsp
checkboxInput.jsp
</body>
</html>
formValidation.jsp
applicationObject.jsp
getServletContext().setAttribute("ApplicationValue","This is my Name");
String
getValueApplcation=(String)getServletContext().getAttribute("ApplicationValue"
);
application.setAttribute("ApplcationVariable",parameterValue);
String
getApplicationVariable=(String)application.getAttribute("ApplcationVariable");
%>
<html>
<head>
<title>Applcation object in JSP</title>
</head>
<body>
The value of getting getServletContext application object <
%=getValueApplcation%><br>
This is application object :<%=getApplicationVariable%>
</body>
</html>
Request object
queryString.jsp
<body>
<a href="queryString.jsp?id=9&name=joe">This is data inside query string</a>
<%
String queryString=request.getQueryString();
out.print("<br>"+queryString);
%>
</body>
</html>
queryStringParameter.jsp
<%@ page language="java" import="java.sql.*" %>
<html>
<head>
<title>Query String in JSP</title>
</head>
<body>
<a href="queryString.jsp?id=9&name=joe">This is data inside query string</a>
<%
String queryStringVariable1=request.getParameter("id");
String queryStringVariable2=request.getParameter("name");
out.print("<br>"+queryStringVariable1);
out.print("<br>"+queryStringVariable2);
%>
</body>
</html>
formGetMethod.jsp
<%@ page language="java" import="java.sql.*" %>
<%
String queryStringVariable1=request.getParameter("name");
String queryStringVariable2=request.getParameter("className");
%>
<html>
<head>
<title>Query String in JSP</title>
</head>
<body>
<%
out.print("<br>Field 1 :"+queryStringVariable1);
out.print("<br>Field 2 :"+queryStringVariable2);
%>
formGetParameterValues.jsp
<body>
<%
try{
out.print("<br>Field 1 :"+queryStringVariable1);
for(int i=0;i<=queryStringVariable2.length;i++){
out.print("<br>Check box Field "+i+" :"+queryStringVariable2[i]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<br>
<br>
<form method="get" name="form1">
Name <input type="text" name="name"> <br><br>
class 1
<input type="checkbox" name="className" value="c1">
class 2 <input type="checkbox" name="className" value="c2"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
formGetRadio.jsp
<body>
<%
try{
out.print("<br>Field 1 :"+inputVariable);
for(int i=0;i<=radioVariable.length;i++){
out.print("<br>Check box Field "+i+" :"+radioVariable[i]);
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<br>
<br>
<form method="get" name="form1">
Name <input type="text" name="name"> <br><br>
class 1
<input type="radio" name="className" value="c1">
class 2 <input type="radio" name="className" value="c2"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Cookies
Cookies use request object to get values through in cookie file. It uses request.getCookies()
method to get cookie and return in cookie[] array.
Server variable
Server variable give server information or client information. Suppose we need to get remote client IP
address, browser, operating system, Context path, Host name any thing related to server can be get
using request object in JSP.
serverVariable.jsp
<body>
Character Encoding : <b><%=request.getCharacterEncoding()%></b><br />
setContentType.jsp
<body>
This is setting content type of JSP page
</body>
</html>
<%
response.setContentType("application/msword");
%>
<%
response.setContentType("application/vnd.ms-excel");
%>
or
cacheControl.jsp
<%@ page language="java" %>
<%
response.setHeader("Cache-Control","no-cache");
/*--This is used for HTTP 1.1 --*/
response.setHeader("Pragma","no-cache");
/*--This is used for HTTP 1.0 --*/
response.setDateHeader ("Expires", 0);
/*---- This is used to prevents caching at the proxy server */
%>
<html>
<head>
<title>Response object in cache controlling</title>
</head>
<body>
This is no cache, Can test this page by open this page on browser and
then open any other website after press back button on browser,
if cacheControl.jsp is reload, it means it is not cached
</body>
</html>
sendRedirect.jsp
<%@ page language="java" %>
<%
response.sendRedirect("http://yahoo.com");
/// response.sendRedirect("YouCanSpecifyYourPage.jsp");
%>
<html>
<head>
<title>Response object Send redirect</title>
</head>
<body>
This page redirects to specified URL location
</body>
</html>
JSP Session Object
session.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="sessionSetRetrieve.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>Session value Set </td>
<td><input type="text" name="sessionVariable" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
sessionSetRetrieve.jsp
<%@ page language="java" import="java.util.*"%>
<%
String sessionSet=request.getParameter("sessionVariable");
session.setAttribute("MySession",sessionSet);
/// String getSessionValue= (String)session.getAttribute("sessionSet");
//this is use for session value in String data
%>
<html>
<head>
<title>Cookie Create Example</title>
</head>
<body>
Session : <%=(String)session.getAttribute("MySession")%>
</body>
</html>
session.setAttribute("MySession",sessionSet) this is use to set new session variable. If we need
to retrieve session variable, have to use session.getAttribute. In this we have to get it by session
variable name here we are using MySession is session variable as key.
session.setMaxInactiveInterval(2700);
When session is no long needed, should be removed forcefully by user. This can be done by
calling session method of invalidate method
session.invalidate();
session.invalidate();
This method expires session for current user, who request for log out.
New session can be find by isNew() method of session, when first time session is created, that is
new session.
session.isNew();
JSP Cookies
createCookie.jsp
<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<body>
<form name="frm" method="get" action="createNewCookie.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%"> </td>
<td width="78%"> </td>
</tr>
<tr>
<td>Cookie value Set </td>
<td><input type="text" name="cookieSet" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
createNewCookie.jsp
<html>
<head>
<title>Cookie Create Example</title>
</head>
<body>
<%
Cookie[] cookies = request.getCookies();
This will expire cookie.,with 0 second. We can set cookie age who long is reside for user.
fileRead.jsp
<body>
<%
String fileName=getServletContext().getRealPath("jsp.txt");
while(din.available()>0)
{
out.print(din.readLine());
}
in.close();
bin.close();
din.close();
%>
</body>
</html>
Make a text file name jsp.txt and put in same folder where this jsp file is exists.
createNewFile.jsp
<body>
<%
String fileName=getServletContext().getRealPath("test.txt");
f.createNewFile();
%>
</body>
</html>
deleteFile.jsp
<body>
<%
String fileName=getServletContext().getRealPath("test.txt");
//// If you know path of the file, can directly put path instead of
///filename e.g c:/tomcat/webapps/jsp/myFile.txt
File f=new File(fileName);
boolean flag=f.delete();
///return type is boolean if deleted return true, otherwise false
%>
</body>
</html>
fileType.jsp
<body>
<%
String fileName=getServletContext().getRealPath("jsp.txt");
%>
</body>
</html>
renameFile.jsp
<body>
<%
String fileName=getServletContext().getRealPath("jsp.txt");
File f=new File(fileName);
boolean flag=f.renameTo(new
File(getServletContext().getRealPath("myJsp.txt")));
%>
</body>
</html>