Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
51 views28 pages

University Institute of Computing: Master of Computer Applications

The document discusses Java Server Pages (JSP) technology which is used to create dynamic web applications. It describes the key components of JSP including the lifecycle of a JSP page, scripting elements like scriptlet tags, expression tags and declaration tags, and implicit objects like request, response, out and others that are available in JSP. Examples are provided to illustrate how to use scripting elements and implicit objects in simple JSP pages.

Uploaded by

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

University Institute of Computing: Master of Computer Applications

The document discusses Java Server Pages (JSP) technology which is used to create dynamic web applications. It describes the key components of JSP including the lifecycle of a JSP page, scripting elements like scriptlet tags, expression tags and declaration tags, and implicit objects like request, response, out and others that are available in JSP. Examples are provided to illustrate how to use scripting elements and implicit objects in simple JSP pages.

Uploaded by

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

UNIVERSITY INSTITUTE OF

COMPUTING
Master of Computer Applications
ADVANCED INTERNET PROGRAMMING
(CAT-714/20CAT721)

DISCOVER . LEARN . EMPOWER


Java Servlet UNIT II

and JSP • Java Servlets: Server-Side Programming, Web


Server, Java Server side components, Servlet
Architecture, Web Container, Servlet Life
Cycle, Tomcat Interface, Servlet interface,
Types of Servlet, HttpServletRequest and
HttpServletResponse, GET and POST request
Course Outcome methods, Retrieving data from database to
CO Title Level Servlet, Servlet Collaboration:
Number RequestDispatcher and sendRedirect,
ServletConfig and ServletContext, Session
CO1 Remember
To understand the basic concepts of Java, Tracking.
Exceptions, JDBC and Swings.
 
CO2 Understanding and work with Servlet, JSP and Understand
web services.   • JSP: Introduction to JSP and its advantages
CO3 Understanding the Java Hibernate & Spring Understand over Servlet, Architecture of JSP, Elements of
frameworks. JSP, Scripting elements, Directives and
actions, JSP configuration, implicit objects.

2
JSP(JAVA SERVER PAGES)
• JSP technology is used to create web application just like Servlet technology.
• 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
• Translation of JSP Page
• Compilation of JSP Page
• Classloading (the classloader loads class file)
• Instantiation (Object of the Generated Servlet is created).
• Initialization ( the container invokes jspInit() method).
• Request processing ( the container invokes _jspService() method).
• Destroy ( the container invokes jspDestroy() method).
The Lifecycle of a JSP Page

Figure No. 1 Life cycle of JSP [1]


SIMPLE JSP PAGE
• To create the first JSP page, write some HTML code and save it by .jsp extension.
• index.jsp
html>  
<body>  
<% out.print(2*5); %>  
</body>  
</html>
• It will print 10 on the browser.
  
SCRIPTING ELEMENTS
• The scripting elements provides the ability to insert java code inside the jsp. There are
three types of scripting elements:
• scriptlet tag
• expression tag
• declaration tag
SCRIPTING ELEMENTS
• scriptlet tag: A scriptlet tag is used to execute java source code in JSP.
• Syntax : <%  java source code %> 
• Example
• <html>  
• <body>  
• <% out.print("welcome to jsp"); %>  
• </body>  
• </html>  
SCRIPTING ELEMENTS
• Examples
Index.html Welcome. jsp
<html>   <html>   <body>  
<body>   <%  
<form action="welcome.jsp">   String name=request.getParameter("uname"); 
 
<input type="text" name="uname">  
out.print("welcome "+name);  
<input type="submit" value="go"><br/>   %>  
</form>   </form>  
</body>   </body>  
</html>   </html>  
SCRIPTING ELEMENTS
• expression tag: The code placed within JSP expression tag is written to the output stream of
the response. So you need not write out.print() to write data. It is mainly used to print the
values of variable or method.
• Syntax 
• <%=  statement %> 
• Example
• <html>  
• <body>  
• <%= "welcome to jsp" %>  
• </body>  
• </html>  
SCRIPTING ELEMENTS
Examples
Index.jsp Welcome.jsp
<html>   <html>  
<body>   <body>  
<form action="welcome.jsp">   <
<input type="text" name="uname"><br/ %= "Welcome "+request.getParameter("
>   uname") %>  
<input type="submit" value="go">  
</form>   </body>  
</body>  
</html>  
</html>  
SCRIPTING ELEMENTS
• Declaration Tag: The JSP declaration tag is used to declare fields and methods.
• The code written inside the jsp declaration tag is placed outside the service() method of auto
generated servlet.
• Syntax : <%!  field or method declaration %>  
• Example index.jsp
• <html>  
• <body>  
• <%! int data=50; %>  
• <%= "Value of the variable is:"+data %>  
• </body>  
• </html>  
JSP declaration tag that declares method

• <html>  
• <body>  
• <%!   
• int cube(int n){  
• return n*n*n*;  
• }  
• %>  
• <%= "Cube of 3 is:"+cube(3) %>  
• </body>  
• </html>  
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.
out JspWriter
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
session HttpSession
pageContext PageContext
page Object
exception Throwable
JSP Implicit Objects

• out 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.
• Example
• <html>  
• <body>  
• <% out.print(“hello”); %>  
• </body>  
• </html>  
JSP Implicit Objects
Request: The JSP request is an implicit object of type HttpServletRequest i.e. created for each jsp
request by the web container. It can be used to get request information such as parameter. It can
also be used to set, get and remove attributes from the jsp request scope.
Example
<form action="welcome.jsp">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>  
<%    String name=request.getParameter("uname");  
out.print("welcome "+name);  
%> 
JSP Implicit Objects

Response: In JSP, response is an implicit object of type HttpServletResponse. The instance of


HttpServletResponse is created by the web container for each jsp request.
It can be used to add or manipulate response such as redirect response to another resource,
send error etc.
Example: index.html
<form action="welcome.jsp">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form> 
Welcome.jsp 
<%    response.sendRedirect("http://www.google.com");   %>  
JSP Implicit Objects

Config: In JSP, config is an implicit object of type ServletConfig. This object can be used to get
initialization parameter for a particular JSP page. The config object is created by the web
container for each jsp page.
Generally, it is used to get initialization parameter from the web.xml file.
Example index.html
<form action="welcome">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>   </form> 
welcome.jsp 
<%    out.print("Welcome "+request.getParameter("uname"));  
  String driver=config.getInitParameter("dname");  
out.print("driver name is="+driver);   %>
web.xml file
<web-app>   <servlet>  
<servlet-name>Dr Anand</servlet-name>  
<jsp-file>/welcome.jsp</jsp-file>  
<init-param>  
<param-name>dname</param-name>  
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
</init-param>    
</servlet>   <servlet-mapping>  
<servlet-name>Dr Anand</servlet-name>  
<url-pattern>/welcome</url-pattern>  
</servlet-mapping>   </web-app>  
JSP Implicit Objects
• Application: In JSP, application is an implicit object of type ServletContext.
• The instance of ServletContext is created only once by the web container when
application or project is deployed on the server.
• This object can be used to get initialization parameter from configuaration file (web.xml).
It can also be used to get, set or remove attribute from the application scope.
JSP Implicit Objects
Example index.html
<form action="welcome">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>  

<%    Welcome.jsp
out.print("Welcome "+request.getParameter("uname"));  
  String driver=application.getInitParameter("dname");  
out.print("driver name is="+driver);    
%> 
web.xml file

<web-app>  
  <servlet>  
<servlet-name>sonoojaiswal</servlet-name>  
<jsp-file>/welcome.jsp</jsp-file>  
</servlet>  
  
<servlet-mapping>  
<servlet-name>sonoojaiswal</servlet-name>  
<url-pattern>/welcome</url-pattern>  
web.xml file Cont.

</servlet-mapping>  
  
<context-param>  
<param-name>dname</param-name>  
<param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>  
</context-param>  
  
</web-app>  
JSP Implicit Objects

Session : In JSP, session is an implicit object of type HttpSession.The Java developer can use this
object to set,get or remove attribute or to get session information.
Example
<html>  
<body>  
<form action="welcome.jsp">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>  
</body>  
</html>  
JSP Implicit Objects Examples

Welcome.jsp Second.jsp
<html> <body>   <html>  
<%    <body>  
String name=request.getParameter("unam <%   
e");   String name=(String)session.getAttribute("user");  
out.print("Welcome "+name);   out.print("Hello "+name);  
session.setAttribute("user",name);     
<a href="second.jsp">second jsp page</a>   %>  
%>   </body>  
</body>   </html>   </html>  
JSP Implicit Objects

PageContext  : In JSP, pageContext is an implicit object of type PageContext class. The


pageContext object can be used to set, get or remove attribute from one of the following scopes:
Page request session Application Example
<html>  
<body>  
<form action="welcome.jsp">  
<input type="text" name="uname">  
<input type="submit" value="go"><br/>  
</form>  
</body>   </html>  
References
• javatpoint.com
• tutorialspoint.com
• Thinking in java by Bruce Eckel
• A programmer’s Guide to Java Certification by khalid Mughal
• Head first Java by Kathy Sierra
• Iterator javadoc
• ListIterator javadoc

27

27/06/2022
THANK YOU

For queries
Email: [email protected]

You might also like