Java Full Stack Development Program
Java Full Stack Development Program
Java Full Stack Development Program
DEVELOPMENT PROGRAM
JSP
OUTLINE
• Introduction to JSP
• JSP Elements
• JSP Action
• MVC
INTRODUCTION TO JSP
• JSP — Java Server Pages 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----%>
• Dynamic content includes some fields like dropdown, checkboxes, etc. whose value
will be fetched from the database.
• It can be used for separation of the view layer with the business logic in the web
application.
INTRODUCTION TO JSP
• JSP is processed on the server
• Declaration
• Scriptlet
• Expression
• Comment
• Directive
JSP DECLARATION
• A declaration tag is a piece of Java code for declaring variables, methods and
classes.
• Syntax
• For each request of the client, service method of the JSP gets invoked
hence the code inside the Scriptlet executes for every request.
• A Scriptlet contains java code that is executed every time JSP is invoked.
• Syntax
• Syntax
• Syntax
• JSP directives are used to give special instruction to a container for translation of JSP to servlet
code.
• In JSP life cycle phase, JSP has to be converted to a servlet which is the translation phase.
• They give instructions to the container on how to handle certain aspects of JSP processing
• Syntax
• Syntax
• <%@ page…%>
JSP PAGE DIRECTIVE
• Attribute language value is Java which is the underlying language in this case.
Hence, the code in expression tags would be compiled using java compiler.(defines
scripting language to be used in the page.)
• The import is set as java.util.Date — we are importing Date class from java.util
package (all utility classes), and it can use all methods of the following class.(defines
the set of classes and packages that must be imported in servlet class definition)
JSP INCLUDE DIRECTIVE
• JSP "include directive” is used to include one file to the another file
• It is also useful in creating templates with the user views and break
the pages into header&footer and sidebar actions.
• Syntax
• JSP taglib directive is used in the JSP pages using the JSP standard tag
libraries
• It uses a set of custom tags, identifies the location of the library and
provides means of identifying custom tags in JSP page.
• Syntax
• JSP actions use the construct in XML syntax to control the behavior of the
servlet engine.
• We can dynamically insert a file, reuse the beans components, forward user to
another page, etc. through JSP Actions like include and forward.
• Unlike directives, actions are re-evaluated each time the page is accessed.
• Syntax
• https://www.guru99.com/jsp-action-tags.html
• List of commonly used Action Tags
JSP ACTION
JSTL
• JSTL — JSP Standard Tag Library
• It is a collection of custom JSP tag libraries that provide common web development
functionality.
JSTL CORE
JSTL CORE
JSTL CORE
JSTL CORE
HTTP SESSION
• The problem with HTTP
• HTTP is stateless — we don’t know if two HTTP requests comes from the same
user or not.
• In order to associate a request to any other request, we need a way to store user data
between HTTP requests
• Session — Storing the credential on server side, give it an "id", and let the client only
know (and pass back at every http request) that id. That is SESSION!
SESSION MANAGEMENT
• Several way to maintain a session between client and server
• HTML Hidden Field — Set a hidden filed to the HTML through Servlet. Only
works with form submitting.
• Cookie — When client make further request, it adds the cookie to the request
header and we can utilize it to keep track of the session
•
HttpSession getSession() – This method always returns a HttpSession object. It returns the session object
attached with the request, if the request has no session attached, then it creates a new session and return it.
•
HttpSession getSession(boolean flag) – This method returns HttpSession object if true return a
new Session; if false return current session or null.
•
void invalidate() – Invalidates this session then unbinds any objects bound to it.
•
Object getAttribute(String name) – Returns the object bound with the specified name in this session, or
null if no object is bound under the name. Some other methods to work with Session attributes
getAttributeNames(), removeAttribute(String
are
• long getCreationTime() – Returns the time when ths session was created, measured in
milliseconds since midnight January 1, 1970 GMT. We can get last accessed time
with getLastAccessedTime() method.
• setMaxInactiveInterval(int interval) – Specifies the time, in seconds, between client requests
before the servlet container will invalidate this session. We can get session timeout value
from getMaxInactiveInterval() method.
• boolean isNew() – Returns true if the client does not yet know about the session or if the client
chooses not to join the session.
•
String getId() – Returns a string containing the unique identifier assigned to this session.
HOW IT WORKS?
• i.e. its entry is de ned in the web.xml le, if we remove the entry of lter
from the web.xml le, lter will be removed automatically and we don't
need to change the servlet.
• Filter
• FilterChain
• FilterCon g
fi
fi
FILTER INTERFACE
• For creating any lter, you must implement the Filter interface. Filter interface provides the
life cycle methods for a lter.
• public void init(FilterCon g con g) — It is invoked by the web container to indicate that a
servlet lter is being placed into the service
• public void destroy() — It is invoked by the web container to indicate that a lter is being
taken out of the service
• We have to implement the Filter interface and override the all of those three methods to
create our own lter.
fi
fi
fi
fi
fi
fi
fi
fi
FILTERCHAIN INTERFACE
• The object of FilterChain is responsible to invoke the next
lter or resource in the chain.