WT Unit-V
WT Unit-V
WT Unit-V
• The JSP container is responsible for intercepting requests for JSP pages.
• To process all JSP elements in the page, the container first turns the JSP
page into a servlet known as the JSP page implementation class
• all template text is converted to println( ) statements and all JSP elements
are converted to Java code that implements the corresponding dynamic
behavior.
• The container then compiles the servlet class.
• all template text is converted to println( ) statements and all
JSP elements are converted to Java code that implements the
corresponding dynamic behavior
• Since the translation phase takes a bit of time, the first user to
request a JSP page notices a slight delay.
• The translation phase can also be initiated explicitly; this is
referred to as precompilation of a JSP page
• request processing phase
• The JSP container is also responsible for invoking the
JSP page implementation class (the generated servlet)
to process each request and generate the response.
_jspService(request,
_jspInit() _jspDestroy()
response)
Initialise JSP Invoked by
Handle Requests:
Invoked only once container
invoked
to cleanup
for every request
• Contract between the JSP engine and JSP
– _jspInit() Corresponds to servlet init(), but has no parameters (use
config implicit object to obtain information regarding environment)
• The functions and variables defined are available to the JSP Page as well as to the
servlet in which it is compiled
Scriptlets
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
Welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
Expressions
• Used to write dynamic content back to the browser.
• If the output of expression is Java primitive the value is printed back to the
browser
• Embedded in <%= and %> delimiters
• Example:
– <%= new java.util.Date() %>
– I
Index.html
Welcme.jsp
<html>
<html>
<body>
<body>
<form action="welcome.jsp">
<%= "Welcome
<input type="text" name="uname"><br/>
"+request.getParameter("uname") %>
<input type="submit" value="go">
</body>
</form>
</html>
</body>
</html>
JSP Implicit Objects
• request: HttpServletRequest
– Reference to the current request
• response: HttpServletResponse
– Response to the request
• session: HttpSession
– session associated with current request
• application: ServletContext
– Servlet context to which a page belongs
• pageContext: PageContext
– Object to access request, response, session and application associated with a page
• config: ServletConfig
– Servlet configuration for the page
• out: JspWriter
– Object that writes to the response output stream
• page: Object
– instance of the page implementation class (this)
• exception: Throwable
– Available with JSP pages which are error pages
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);
%>
JSP Initialization and Context Parameters
web.xml
<web-app>
<servlet>
<servlet-name>init</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>init</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<context-param>
<param-name>url</param-name>
<param-value>jdbc:odbc:dbname</param-value>
</context-param>
</web-app>
<form action="welcome">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
===============
<%
out.print("Welcome "+request.getParameter("uname"));
String driver=config.getInitParameter("dname");
out.print("driver name is="+driver);
String url=config.getInitParameter(“url");
out.print("driver name is="+driver);
%>
<html>
<body>
<form action="welcome.jsp"> <html>
<input type="text" name="uname"> <body>
<input type="submit" value="go"><br/> <%
</form>
</body> String
</html> name=(String)session.getAttribute("user");
out.print("Hello "+name);
============================
<html> %>
<body> </body>
<% </html>
String name=request.getParameter("uname");
out.print("Welcome "+name);
session.setAttribute("user",name);
%>
</body>
</html>
• 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
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body> <html>
<body>
</html>
<%
<html> String
<body> name=(String)pageContext.getAttribute("us
er",PageContext.SESSION_SCOPE);
<%
out.print("Hello "+name);
%>
</body>
</html>
Directives
• The jsp directives are messages that tells the web container
how to translate a JSP page into the corresponding servlet.
– Syntax:
<%@ directive attribute="value" %>
JSP page directive
• The page directive defines attributes that apply to an entire JSP page
• Syntax:
– <%@ page attribute="value" %>
• Attributes of JSP page directive
– import
• Declares the packages and classes that need to be imported for using in the java
code
– <%@ page import="java.util.Date" %>
– Today is: <%= new Date() %>
– contentType
• Defines MIME type for the output response
• <%@ page contentType=application/msword %>
– extends
• Declares the class which the servlet compiled from JSP needs to extend
– info
• String returned by the getServletInfo() of the compiled servlet
<%@ page info=“Created by rajashekar" %>
– buffer
• defines buffer size of the jsp in kilobytes
• <%@ page buffer="16kb" %>
• language
• Defines server side scripting language
• isThreadSafe
• If false the compiled servlet implements SingleThreadModel interface
• <%@ page isThreadSafe="false" %>
• autoFlush
• When true the buffer is flushed when max buffer size is reached
• <% @ page autoFlush="true/false" %>
• session
• JSP page creates session by default.
• Boolean which says if the session implicit variable is allowed or not
• <%@ page session="true/false"%>
• errorPage
• Defines the relative URI of web resource to which the response should be
forwarded in case of an exception
• <%@ page errorPage="myerrorpage.jsp" %>
• isErrorPage
• True for JSP pages that are defined as error pages
<%@ page isErrorPage="true" %>
<%@ page
errorPage="myerrorpage.jsp" %>
</body>
</html>
• In JSP, exception is an implicit object of type java.lang.Throwable class.
• This object can be used to print the exception.
• But it can only be used in error pages.
myerrorpage.jsp
• The include directive includes the original content of the included resource
at page translation time
• The JSP taglib directive is used to define a tag library that defines many
tags.
• used to import custom actions defined in tag libraries
• We use the TLD (Tag Library Descriptor) file to define the tags.
• Syntax:
– <
%@ taglib uri="uriofthetaglibrary" prefix="prefixoftaglibrary" %>
JSP Action Tags
• There are many JSP action tags or elements.
• The action tags are used to control the flow between pages and to use Java
Bean.
• jsp:forward forwards the request and response to
another resource.
<
% out.print("Today is:"+java.util.Calendar.getIn
stance().getTime()); %>
<%= request.getParameter("name") %>
jsp:include
• The jsp:include action tag is used to include the content of
another resource it may be jsp, html or servlet.
• type: provides the bean a data type if the bean already exists in the scope. It is
mainly used with class or beanName attribute
● This is the data layer which consists of the business logic of the
system.
● It consists of all the data of the application
● It also represents the state of the application.
● The controller connects with model and fetches the data and
sends to the view layer.
● The model connects with the database as well and stores the
data into a database which is connected to it.
View Layer:
● It is used to display the data which is fetched from the controller which in
turn fetching data from model layer classes.
Controller Layer:
● It intercepts all the requests which are coming from the view layer.
● It receives the requests from the view layer and processes the
requests and does the necessary validation for the request.
● This requests is further sent to model layer for data processing, and
once the request is processed, it sends back to the controller with
required information and displayed accordingly by the view.
• Advantages
• 1. Setting Session :
• use session.setAttribute("ATTRIBUTE NAME","ATTRIBUTE VALUE")
method for setting value in session.
</body>
</html>
<%
// Create cookies for first and last names.
Cookie firstName = new Cookie("first_name", request.getParameter("first_name"));
Cookie lastName = new Cookie("last_name", request.getParameter("last_name"));