0% found this document useful (0 votes)
11 views3 pages

JSP - Complete

Uploaded by

Koena Sadhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

JSP - Complete

Uploaded by

Koena Sadhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

JAVA SERVER PAGES (JSP)

(refer to durgest tiwari youtube tutorial or javatpoint website)

1. Declarative tag --> Used to declare variables and methods inside a JSP page.
Example:
<%!
int a = 5;
int b = 15;
String name = “Koena”;

public int doSum()


{
return a+b;
}
%>

2. Scriptlet Tag --> This tag is used to write java source code inside JSP Page
Example:
<%
out.println(a);
out.println(b);
out.println(“The summation is: ” + doSum());
%>

3. Expression Tag --> This is used to print any kind of information inside the JSP Page.
Example:
<h1> Sum is <%= doSum() %> </h1>
<h1> <%= a %> </h1>
<%= name %>

4. Page directive: This directive tag is used to apply any kind of content which will be applicable to the
entire JSP page. Suppose we want to import the Random package, or the ArrayList package, etc then
we have to do the following:

<%@page import=”java.util.Random, java.util.ArrayList, java.io.*” %>


More examples:
<%@page isErrorPage = “true” %>
<%@page session=”false” %>

5. Include directive: This directive tag is used inside a JSP Page if we want to include any particular
content or content of some other page inside our current page. Basically, using this tag we can
include any sort of content we wish to.
Example:
Suppose we have an jsp page --> header.jsp
Now we want to include this header in a particular portion within our JSP Page, so we do the
following:

<%@ include file = “header.jsp” %>

6. Taglib Directive: This directive tag is used when we want to use the functionalities of some other
pre-made libraries, be it the taglib or other custom made libraries. For example, we have JSTL (JSP
Standard Tag Library) which has the core tags, function tags, sql tags. We can use these tags using the
taglib directive.
Suppose we want to use the core tags of JSTL using the taglib directive:

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>


<html>
<body>
<h1> Taglib Directive Tutorial </h1>
<c:set var=”name” value=”Koena”></c:set>
<c:out value=”${name}”></c:out>
</body>
</html>

So as we can see above, using ‘c’ as the prefix for core tags, we can use all the functions of the core
tag library. Similarly, we can also use taglib directives to use with custom-made or user-made
libraries.

7. JSTL (JSP Standard Tag Library)

(a) The JSTL represents a set of tags to simplify JSP development.


(b) The JSTL has five main tags:
-> Core Tags - url: http://java.sun.com/jsp/jstl/core
-> Function Tags - url: http://java.sun.com/jsp/jstl/functions
-> Formatting Tags - url: http://java.sun.com/jsp/jstl/fmt
-> SQL Tags - url: http://java.sun.com/jsp/jstl/sql
-> XML Tags - url: http://java.sun.com/jsp/jstl/xml

These tags have various functionalities which helps in easing JSP development. For the functionalities
of each of the tag refer to -> javatpoint website

8. Exception Handling
Steps:
(a) We design or create an error page which should pop up whenever any error occurs.
Suppose we create error.jsp which is the page where the designing is done-
<%@ page isErrorPage = “true” %>

(b) The next step, is to mention in whichever page we want to display the error page. Suppose in
process.jsp something goes wrong, in order to handle it we will display the error.jsp, so for that we
will use the below page directive:
<%@ page error-page=”error.jsp” %>

9. Expression Language: ${expression}

10. Custom Tags


The steps to create custom tags:
(i) Create the tag handler class(a simple java class) and perform the action at the start or end of the
tag. That is, this class will simply contain the business logic that we would want to perform.

(ii) Create the Tag Library Descriptor File(TLD) which will contain the mapping of the custom tag which
we have created.
Example:
Suppose the TLD file name is: mytags.tld
Inside this-

<tag>
<name>today</name>
<tag-class>com.jsp.myTagHandler</tag-class>
</tag>
(iii) Step 3 would be to create a jsp file and use the uri of the path where the tld file is kept and use a
prefix to use the tag we created.

11. Fore redirection to another page we can use the scriptlet tag and use
response.sendRedirect(“pageUrl”)
Example:
<%
response.sendRedirect(“www.google.com”);
%>

12. Filter - It is used to perform any task either before or after the execution of a particular servlet.
Steps:
(i) First create the class(ex: MyFilter.java) which will implement the Filter interface and inside the
doFilter() method we write the code for the filter.

(ii) Next we need to map the filter in the web.xml


Example:

<filter>
<filter-name>filter1</filter-name>
<filter-class>com.jsp.MyFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/ProfileServlet</url-pattern> //this url-pattern states that the filter will execute either
befor or after running the ProfileServlet.java servlet
</filter-mapping>

************************************ COMPLETED ************************************

You might also like