Professional Documents
Culture Documents
https://www.w3schools.in/jsp/tutorials/
A JSP page consists of HTML tags and JSP tags. The JSP pages are
easier to maintain than Servlet because we can separate designing and
development. It provides some additional features such as Expression
Language, Custom Tags, etc.
The Lifecycle of a JSP Page
• For creating a dynamic web project click on File Menu -> New ->
dynamic web project -> write your project name e.g. first -> Finish.
2) Create the JSP file in eclipse IDE
• For creating a jsp file explore the project by clicking the + icon ->
right click on WebContent -> New -> jsp -> write your jsp file name
e.g. index -> next -> Finish.
• 3) Start the server and deploy the project:
JSP Scriptlet tag (Scripting elements)
Java provides various scripting elements that allow you to insert Java
code from your JSP code into the servlet. Scripting elements have
different components that are allowed by JSP.
Scripting Elements in JSP
• Scripting elements in JSP must be written within the <% %> tags. The
JSP engine will process any code you write within the pair of the <%
and %> tags, and any other texts within the JSP page will be treated as
HTML code or plain text while translating the JSP page.
different scripting elements
• Comment <%-- Set of comment statements --%>
• Directive <%@ directive %>
• Declaration <%! declarations %>
• Scriptlet <% scriplets %>
• Expression <%= expression %>
• Comment
• Comments are marked as text or statements that are ignored by the JSP
container. They are useful when you want to write some useful
information or logic to remember in the future.
<%-- This comment will not be visible in the page source --%>
</body>
Declaration
• As the name suggests, it is used to declare methods and variables you
will use in your Java code within a JSP file. According to the rules of
JSP, any variable must be declared before it can be used.
• Jsp initializer scans the code and find the declaration tag and initializes
all the variables, methods, and classes. JSP container keeps this code
outside of the service method (_JSPService()) to make them class
level variables and methods.
<body>
<!--declaration of username variable.... -->
<%! String username=“abc"; %>
</body>
JSP Scriplet Tag
• This tag allow user to insert java code in JSP. The statement which is
written will be moved to jspservice() using JSP container while
generating servlet from JSP. When client make a request, JSP service
method is invoked and after that the content which is written inside the
scriptlet tag executes.
• html
<!-- Example of JSP code which prints the Username -->
<html>
<body>
<form action=“abc.jsp">
<!-- move the control to Geeks.jsp when Submit button is click -->
Enter Username:
<input type="text" name="username">
<input type="submit" value="Submit"><br/>
</form>
</body>
</html>
• abc.jsp
<body>
<%
String name=request.getParameter("username");
out.print("Hello "+name);
%>
</body>
Expressions
• Expression tag is one of the scripting elements in JSP. Expression Tag
in JSP is used for writing your content on the client-side. We can use
this tag for displaying information on the client’s browser.
<html>
<body>
<%= Hello jsp %> <!-- Expression tag -->
</body>
</html>
JSP Directives
• Directives supply directions and messages to a JSP container. The
directives provide global information about the entire page of JSP.
Hence, they are an essential part of the JSP code.
• Directives can contain several attributes that are separated by a comma
and act as key-value pairs. In JSP, directives are described with a pair
of <%@ .... %> tags.
• The syntax of Directives looks like:
• To enable JSTL features, we’d have to add the library to our project.
For a Maven project, we add the dependency in pom.xml file:
• With the library added to our project, the final setup will be to add
the core JSTL tag and any other tags’ namespace file to our JSP
using the taglib directive like this:
• <%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
• JSTL Core Tags
• The JSTL core tag provides variable support, URL management,
flow control etc. The syntax used for including JSTL core library in
your JSP is:
• <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
c:out It display the result of an expression, similar to the way <%=...%> tag
work.
c:remove It is used for removing the specified scoped variable from a particular
scope.
c:catch It is used for Catches any Throwable exceptions that occurs in the body.
c:if It is conditional tag used for testing the condition and display the body
content only if the expression evaluates is true.
c:choose, c:when, c:otherwise It is the simple conditional tag that includes its body content if
the evaluated condition is true.
c:forEach It is the basic iteration tag. It repeats the nested body content
for fixed number of times or over collection.
c:redirect It redirects the browser to a new URL and supports the context-
relative URLs.
1.<form action="process.jsp">
2.No1:<input type="text" name="n1" /><br/><br/>
3.No1:<input type="text" name="n2" /><br/><br/>
4.<input type="submit" value="divide"/>
5.</form>
•Process.jsp
1. <%@ page errorPage="error.jsp" %>
2. <%
3.
4. String num1=request.getParameter("n1");
5. String num2=request.getParameter("n2");
6.
7. int a=Integer.parseInt(num1);
8. int b=Integer.parseInt(num2);
9. int c=a/b;
10.out.print("division of numbers is: "+c);
11.
12.%>
•error.jsp
1. <%@ page isErrorPage="true" %>
2.
3. <h3>Sorry an exception occured!</h3>
4.
5. Exception is: <%= exception %>