Unit 11: JSP programming Java Programming II BIM 5TH SEMESTER
Unit 11: JSP programming
JSP technology is used to create web application just like Servlet technology.
It can be thought of as an extension to Servlet because it provides more functionality than
servlet.
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.
Java Server Pages (JSP) is a technology which is used to develop web pages by inserting Java
code into the HTML pages by making special JSP tags. The JSP tags which allow java code to
be included into it are <% java code %>.
JSP Scriptlet tag
A scriptlet tag is used to execute java source code in JSP.
Syntax is as follows
<% java source code %>
Example: JSP page to display message “Welcome to jsp”
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
Example of JSP scriptlet tag that prints the user name
File: index.html
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
Compiled By: Tara Bahadur Thapa pg. 1
Unit 11: JSP programming Java Programming II BIM 5TH SEMESTER
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
JSP expression tag
<%= statement %>
JSP Declaration Tag
<%! field or method declaration %>
Example
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
JSP directives Tags
Example
<html>
<body>
<%@ page import="java.util.Date" %> //directive tags
………………………..
………………………..
</body>
</html>
Compiled By: Tara Bahadur Thapa pg. 2
Unit 11: JSP programming Java Programming II BIM 5TH SEMESTER
JSP Implicit Objects
There are 9 jsp implicit objects. These objects are created by the web container that are available
to all the jsp pages.
Object Type
Out JspWriter
Request HttpServletRequest
Response HttpServletResponse
Config ServletConfig
Application ServletContext
Session HttpSession
pageContext PageContext
Page Object
Exception Throwable
JSP out implicit object
For writing any data to the buffer, JSP provides an implicit object named out. It is the object of
JspWriter. In case of servlet you need to write:
PrintWriter out=response.getWriter();
But in JSP, you don't need to write this code.
<html>
<body>
<% out.print("JSP Programming”)%>
</body>
</html>
JSP request implicit object
Compiled By: Tara Bahadur Thapa pg. 3
Unit 11: JSP programming Java Programming II BIM 5TH SEMESTER
//index.html
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
//welcome.jsp
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
Compiled By: Tara Bahadur Thapa pg. 4