Module 4 JSP
Module 4 JSP
Definition
• JavaServer Pages is a scripting environment that allows you to
combine Java code with HTML code to generate content
dynamically.
• JSP pages are easier to maintain than a Servlet.
• JSP pages are opposite of Servlets as a servlet adds HTML code
inside Java code, while JSP adds Java code inside HTML using
JSP tags. Every JSP page is converted into a servlet.
• The conversion is done by the JSP engine.
• The JSP files should have extension .jsp
2) JSP scriptlets - allows you to write java code inside JSP page.
<% Java statements %>
e.g. a) <%
if(total >90) …………..
else if(total>80)…………
else……………
%>
b)
<% if (Math.random()>0.5) { %>
HTML CODE
<% } %>
<% else %>
HTML CODE
PMCA502L: Thilagavathi M, AP(Sr.), SCORE
Example
index.html
<form method="post" action="welcome.jsp">
Name <input type="text" name="user" >
<input type="submit" value="submit">
</form>
welcome.jsp
<html>
<%
String user = request.getParameter("user");
%>
<body>
Hello, <% out.println(user); %>
</body>
</html>
PMCA502L: Thilagavathi M, AP(Sr.), SCORE
JSP Elements
3) JSP declaration
<%! Declaration of field/method/class/interface %>
e.g.
a) <%! public static final double PI = 3.14; %>
Behind the scene a JSP page is converted into a servlet source file.
For conversion:
The JSP expressions go inside the _jspService( ) method replacing a
JSP expression by
out.print(JSPExpression)
The JSP scriptlets also go inside _jspService method after removing the JSP
scriptlet tag.
e.g. <%
if(total >90) …………..
else if(total>80)…………
else……………
%>
The JSP comments are ignored by the JSP engine and so they are not
found inside the generated servlet.
request HttpServletRequest
e.g. <%= request.getParameter(“item”) %>
response HttpServletResponse
out JspWriter
application ServletContext
config ServletConfig
exception Throwable
page Object
pageContext PageContext
If you are writing code that is not part of the _jspService method,
these variables are not available.
out=pageContext.getOut();
session=pageContext.getSession();
request=pageContext.getRequest();
response=pageContext.getResponse();
application=pageContext.getServletContext();
config=pageContext.getServletConfig();
5) JSP directive
• Directive Tag gives special instruction to
Web Container at the time of page
translation.
• There are three types of JSP directives:
– Page
– include
– taglib
Syntax:
<%@ page
attribute_1 = “list of values separated by comma”
attribute_2 = “list of values separated by comma”
Importing packages
<%@ page import =” java.sql.*, java.io.* ” %>
OR
<%@ page import =” java.sql.*” %>
<%@ page import =”java.io.*” %>
The content of the file (not its output) is incorporated into this JSP page
at the time of translating it into servlet.
Syntax:
<jsp:include page="relative-path-to-resource" />
OR
What is included?
Output of page Actual content of file
Request forwarding
<jsp:forward
page=” relative-path-to-resource” flush=”true”/>
OR
Example jsp:forward
<% String destination;
if (Math.random() > 0.5) {
destination = "/examples/page1.jsp";
} else {
destination = "/examples/page2.jsp";
}
%>
<jsp:forward page="<%= destination %>" />
Passing Parameters
<jsp:forward page=”name” flush=”true”>
<jsp:param name=”pname1” value=”value1” />
<jsp:param name=”pname2” value=”value2” />
</jsp:forward>
RequestDispatcher rd =
request.getRequestDispatcher("Registrationform.jsp");