jspInit()
method before servicing any requests.public void jspInit(){
// Initialization code...
}
HTML-like
tags that provide useful functionalities such as JSP:useBean
is to work with Java Bean, JSP:include
a resource, JSP:forward
is to forward the request or response, JSP:setProperty
is to set a property value in bean object. JSP action tag always starts with JSP : and we can use them in the JSP page directly without any need for configuration changes or importing any library. jspInit()
method to initialize the servlet instance. After this, JSP can handle the client request._jspService()
method. This method will have request and response object parameters. Each thread will process client requests the same as the servlet.jspDestroy()
method.request.getRequestDispatcher(path)
is used to create it we need to give the relative path of the resource whereas context.getRequestDispatcher(path)
to create it we need to give the absolute path of the resource. ServletContect
and PageContext
is, the ServletContext
is designed to provide information about the Container and on the other hand, the PageContext
is designed to provide information about the Request. doStartTag()
method. To write the data for JSP, we need to use the JspWriter class. The PageContext class provides getOut()
method which returns the instance of the JspWriter class. Later, the TagSupport class provides an instance of pageContext by default.<jsp.forward>
element forwards the request object from 1 JSP file to another. Target file can be HTML, servlet or another JSP file, but it should be in the same application context as forwarding JSP file.<%@page isThreadSafe=”false” %
> directive in the JSP page. jsp:
” and action name.<jsp:include/>
<jsp:getProperty/>
<jsp:forward/>
<jsp:setProperty/>
<jsp:usebean/>
<jsp:plugin/>
<servlet>
<servlet-name>Test</servlet-name>
<jsp-file>WEB-INF/test.jsp</jsp-file>
<init-param>
<param-name>test</param-name>
<param-value>Test Value</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/Test.do</url-pattern>
</servlet-mapping>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
<%@ include file=”file_name” %>
and <jsp:include page=”file_name” />
.<!–Comment–>
.<%–Comment–%>
.${Expression}
<% and ends with %>
. Any code written inside the scriptlet tags go into the _jspService()
method. For example;<%
Date d = new Date();
System.out.println("Current Date="+d);
%>
out.print()
method, there is a shortcut to do this through JSP Expressions. JSP Expression starts with <%= and ends with %>
.<% out.print("Learning"); %>
can be written using JSP Expression as <%= "Learning" %>
<%= %>
is sent as parameter to out.print()
method. Also notice that scriptlets can contain multiple java statements and always ends with semicolon (;) but expression doesn’t end with semicolon.<%! and ends with %>
.<%! public static int count=0; %>
.JSP EL
code for this will be ${pageContext.request.method}
. web.xml
because the TLD files are inside the META-INF directory of the JSTL jar files. When container loads the web application and finds TLD files inside the META-INF directory of the JAR file, it automatically configures them to be used directly in the application JSP pages. All we need to do it to include it in the JSP page using taglib directive. session.setAttribute("user", name);
String name= (String)session.getAttribute("user");
response.sendRedirect(
), By using this method, the server return back the response to the client, from where next request comes and it displays that url.sendRedirect()
from JSP #1 to JSP #2 the browser's location bar is changed to show JSP #2, since a sendRedirect()
is a new request,<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>JSP Redirect Example</title>
</head>
<body>
<%
String redirectURL = "http://www.javatips.net/";
response.sendRedirect(redirectURL);
%>
</body>
</html>
isErrorpage=TRUE
” in error JSP page. The reason for doing so is, whenever any error will occur in the JSP page, an error page will be automatically called. clone()
method of the Object class is used to create the clone of an existing object. As a prerequisite, The class whose object the user tries to clone is expected to implement the java.lang.Cloneable interface. class FullName implements Cloneable{
String Firstname;
String Lastname;
FullName(String Firstname,String Lastname){
this.Firstname=Firstname;
this.Lastname=Lastname;
}
public Object clone()throws CloneNotSupportedException
{
return super.clone();
}
public static void main(String args[])
{
Try
{
FullName s1=new FullName("Free Time","Learning");
FullName s2=(FullName)s1.clone();
System.out.println(s1.Firstname+" "+s1.Lastname);
System.out.println(s2.Firstname+" "+s2.Lastname);
}
catch(CloneNotSupportedException c)
{
}
}
}
public void removeAttribute(String name)
public void invalidate()
public void setMaxInactiveInterval(int interval)
Cookie mycookie = new Cookie("name","value");
response.addCookie(mycookie);
Cookie killcookie = new Cookie("mycookie","value");
killcookie . set MaxAge ( 0 );
killcookie . set Path (" / ");
killcookie . addCookie ( killcookie 1 );​
<c:out>
tag : It is used to display the content on client after escaping XML and HTML markup tags. Main attributes are default and escapeXML.<c:set>
tag : This tag is used to set up a variable value in a specified scope. It sets the result for a given expression in the given variable.<c: remove>
tag : It is used to remove an attribute from a specified scope, By default removes it from all scopes.<c: if>
tag : This JSTL core tag is used for testing conditions. There are two other optional attributes for this tag, var, and scope.<c:choose>
tag : It is used as a switch case.<c:when>
tag : It’s like a case statement in Java.<c:otherwise>
tag : It is used as the default attribute in switch-case statements.<c:catch>
tag : This tag is used in exception handling.<c:import>
tag : This is a core JSTL tag. It is used for importing the content from another file or page to the current JSP page. Attributes required are var, URL, and scope.<c:forEach>
tag : This tag in JSTL works as for look from java <c:forTokens>
tag : It is used for iteration but it only works with the delimiter.<c:param>
tag : This JSTL tag is mostly used with <c:url>
and <c:redirect>
tags. It adds parameters and their values to the output of these tags.<c:url>
tag : It is used for URL formatting or URL encoding. It converts a relative URL into an application context’s URL.<c:redirect>
tag : It is used, to redirect the current page to another URL, provide the relative address in the URL attribute of this tag and the page will be redirected to the URL.c:catch
and c:if
to catch exception inside the JSP service method and process it. c:catch
tag catches the exception and wraps it into the exception variable and we can use c:if
condition tag to process it. Below code snippet provide sample usage.<c:catch var ="exception">
<% int x = 5/0;%>
</c:catch>
<c:if test = "${exception ne null}">
<p>Exception is : ${exception} <br />
Exception Message: ${exception.message}</p>
</c:if>
c:if
condition.<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Mouse Drag</title>
</head>
<body bgcolor="red">
<h1>Mouse Drag Example</h1>
<jsp:plugin align="middle" height="450" width="450"
type="applet" code="mousedrag.class" name="clock" codebase="."/>
</body>
</html>