REQUESTDISPATCHER INTERFACES
. The Request Dispatcher dispatches the client request to another web resources,
such as HTMLpage,another servlet, JSP etc.
. it has2 methods
. 1)forward()method is used to transfer the client server to another resources (HTMLpage,servlet,jsp
etc)
. when this method is called,the control,the control is transferred to the next resources called
2)include()this method is used to include the content of the called resources
(HTML file,servlet.jsp etc)into the calling one
.when this method is called ,the control still remains with the calling resources
Servletconfig:-
The servlet config is an object containing server initial parameret createdby servlet config
for a particular servlet and the initial parameters are servlet specific
The parameters are congigured in web.xml file.
Servlet init parameter in web.xml :-
<servlet>
<servlet-name>user servlet</servlet-name>
<servlet-class>user servlet</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>subrat</param-value>
</init-param>
</init-param>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</init-param>
<servlet>
->this init paramet value cant be shared by oyher subject
Servlet context
. context parameter refers to the initialization parameter for all servlet of an application.
is used to define a context parameter. .
. .<context-param>attribute is used to define a context parameter.
. .<context-param>attribute has two main sub attribute
<param-name>,<param-attribute>.
. <param-name> contains the name of the parameter& <param-value>contains the value of the
parameter.
<context-param>
<param-name>username</param-name>
<param-value>tul.ka</param-value>
.</context-param>
.<context-param>
<param-name>email</param-name>
<param-value>subrat@hbdjghgafhab gmail.com</param-value>
.</context-param>
Private string user name;
Private string email;
Public void init(Servlet config config)throws ServletException{
Super.init(config);
Servlet Context Context = getservletcontext()
Username = context get init parameter(“username”);
Out.println(‘<html><)
Email= context.getInitParameter(“email”);
PrintWrite out=response.getWrite();
out.println(“<html><head><title>Servlet Context Page</title></head>”);
out.println(“<body> in contextparameterServlet1 – Username is <b>”+username);
out.println(“</b><br>Email is <b>”+email +”</b>”);
out.println(“</body></html>”);
SESSION TRACKING
Session- A particular interval of time.
.EX-Time from user login until logout.
.HTTProtocol is stateless. It cannot maintain eash users state(User’s Data)
Each request is a new request.
.session tracking is a way to maintain the state of
COOKIES
IT IS A SMALL PIECE OF INFORMATION THAT IS PERSISTED B/W THE MULTIPLE CLIENT REQUEST.
Cookies are stored in the cache memory browser
.cookies has the following properties:-
.cookies name
.cookies value
.Maximum age,i.e., life time of a cookies in second.
EX.
.cookies cookie = new cookies (“username”,”niharika”);
.cookies set max age(1800);
.Add cookis using addcookies() method;
.response.addcookies(cookies);
.to maintain the user identity HTTPsession are used.
. A session object stores user-specific data during multiple request/response.
. Servlets’s HttpSession interface provides a way to identify a user in multiple page request.
. we can store , retrive & remove “attribute” from HttpSession object.
Attributename attribute value
.Http Session attribute are set in 1st servlet
.Ex. ServletOne.java
.HttpSession session = request getSession();
.Session .set Attribute(“username”,”ankita”);
.HttpSession Attribute is uses in other servlet for single user
Difference Between Session and Cookies :
Cookie Session
Cookies are client-side files on a
Sessions are server-side files that contain user
local computer that hold user
data.
information.
Cookies end on the lifetime set by When the user quits the browser or logs out of
the user. the programmed, the session is over.
It can only store a certain amount
It can hold an indefinite quantity of data.
of info.
We can keep as much data as we like within a
The browser’s cookies have a session, however there is a maximum memory
maximum capacity of 4 KB. restriction of 128 MB that a script may consume at
one time.
Because cookies are kept on the
To begin the session, we must use the session
local computer, we don’t need to
start() method.
run a function to start them.
Cookies are not secured. Session are more secured compare than cookies.
Cookies stored data in text file. Session save data in encrypted form.
Cookies stored on a limited data. Session stored a unlimited data.
In PHP, to get the data from
In PHP , to get the data from Session, $_SESSION
Cookies , $_COOKIES the global
the global variable is used
variable is used
We can set an expiration date to In PHP, to destroy or remove the data stored
delete the cookie’s data. It will within a session, we can use the session_destroy()
automatically delete the data at function, and to unset a specific variable, we can
that specific time. use the unset() function.
MVC ARCHITECTURE
.A Design pattern
.Model—represent state of application e.g.,form data,database
.view---looka at model and generate an appropriate user interface to present the data.
.controller—receive user interface input,update data model.
.ex.
Model—java beans,
WHAT IS JSP ?
. Mostly html page,with html.jsp
. include jsp tag to enable dynamic content creation
. Dynamic content: The information generated and displayed at run time is called “dynamic content” jsp uses java to display dynamic content
. Translation:JSP Servlet class
. Compiled at Request time
. Execution Req
Provides information about the page imports the required api
<%@page contentType=”text/html”pageEncoding=”UTF-8”%>
1.@PAGE
Provide information about the page
<%@page content Type= “Text/html”PageEnco0ding =”” UTF-8%>
2.@INCLUDE :
. include another resources (jsp / html / servlet) into the current page
<%@include file = “sample.html”%>
1. DECLARATION:<%!...%>
. For Page wide variable and method declarations.
EX.--<% Int num%>
2.
.3 Expressions
.Formats expression valude I nto string in the output in thw web page
Ex.
4.Comments:
. to add user documented comments will not parsed by the jsp compiler.
Ex. <%--Calculate the sum of 1+2+3 dynamic >
Action tags
.it affects the one time behaviour of the jsp page.
.The response of the action will be sent to the ckient
Why to use ORM?
. Object and relational model matching is the issue.
. Database mindependent.
.Avoiding sql queries.
.Avoiding JDBC COMPLETEly
.Automatic table creation.
.Automatic key generation. application
Hibernate XML mapping
Property hibernate file
hhhh
JDBC AP
.HIBERNATE COMPONENTS
.Hibernate.cfg.xml file:
.hibernate.cfg.xml file contains database related configuratyuon
And session related configuration.
.database configuration includes database connection URL, database user credentials, driver
class and hibernate dilect.
. <classname>.hbm.xml file;
<classname>.hbm.xml :the files spe ifies the mapping b/w POJO classes variables and table
column.
. it represented in the “hibernate” . cfg.xml.
Spring Framework:
A Software framework is re usable design for a software system.
Application Framework:
. A Framework that is fit for any type of application.
. EX :Stand Alone Web,Enterprise etc,
. Spring is a layed archicture based Framework.
.Allows selection of components based on the need in a real time application.
.EX : Modeled components-POJO,JDBC
-MVC components- spring MVC
-ORM components-hibernate integration
.NON-Invaise --- Need to use the built in class and overriding the methods
.LIGHT WEIGHT FRAMEWORK—
.Light weight – Light weight frame work doesnot depend on class to hook components into them!
.Uses user defined class and method
.An alternative replacementfor the Enterprises java Bean(EJB) model
.flexible-User will define method in a single controller instead of having lifecycle mrthod (for servlet)
.Not exclusive to java
.Do not usen everything of spring – Based on the need the module are used in application.
Dependency Injection & Inversion of Control
Dependency : A POJO / been /Entity
Non IOC- Geeting a POJO by pulling-hard Coding
Employee emp1 = new Employee();
Employee emp2 = new Employee();
Inversion of control
IOC—Getting a pojo Object by pushing on demand by spring core container
Employee emp1=(Employee)xxxxx.getBean(“emp 1”);
Employee emp2=(Employee)xxxxx.getBean(“emp 2”);
Set(B)
Object B
OBJECT A
OBJECT C
The act of Dependency
Spring Core Container
Spring’s container uses IoC manages components of the application.
. Application
Context (org.springframework.context.ApplicationContext provide application framework
service.
CONSTRUCTOR INJECTION
We can inject the dependency (Adress) by the constructor
Setter injection
Scenerio : Employee HAS-A-Adress
The address class object will be termed as thr dependent object.
Thye Employee class object will be termed as dependency.
ANNOTATIONS
Annotion are the instruction for the computer.
Also known asmetadata/datya about code
Ex:
@override
Checks that the method is available in the super class
EX:
@Override
Public String to String(){
return “Welcome!”;
}
Annotation are used to avoid configuration of certain tags in Xml files in frameworks.
They Wont affect the normal application flows.
@Auto wired
Auto wired annotation is used to inject a bean automatically without using ref attribute
Normal configuration : without@Autowired
It can be used for constructurs ,senders and for properties
Ex:
Spring MVC view RE- solver
Models are rendered in a browser through view Resolver(dynamic to static content)
Webpage will become light weight after conversion of dynamic page into static page.
Before view Resolving-hellopage.jsp
After view Resolving-hellopage.html
<servlet-name>spring</servlet-name>
<url-pattern>*html</url-pattern>
Based on the Servlet Mappings which we provide in our web.xml, the request will
be routed by the Servlet Container to our DispatcherServlet.
Once the request is received, the DispatcherServlet will take the help
of HandlerMapping which has been added in the Spring Configuration file and
get to know the Controller class to be called for the request received.
Now the request will get transferred to the Controller, the Controller then executes
the appropriate methods and returns the corresponding ModelAndView object to
the DispatcherServlet.
The DispatcherServlet will send the Model received to the ViewResolver to get
the view page.
Finally, the DispatcherServlet will pass the Model to the View page and the page
will be rendered to the user.
RESULT SET META DATA
Result set meta data is a interface I java.sql package.
It is used to get the meta data
When a QUERY of the database usi ng SELECT statement is executed , the result will be
stored in a RESULTset object.
Every ResultSet object is associaterd with one ResultsetMetadata object.
The object will have all the Metadata about a result set object like table name, number
&column ,column name,datatype of the column.
Resultset meta data object is obtained by using getMetadata() Method of result set.
Spring boot
Spring boot is a spring module which provide RAD fetures to spring frame work.
It provide an easier an faster way to set up, and run b oth simple aand web_based
application.
It creates stand along spring application. That means no need to deploy spring boot
application to a web server or any special environment.
Spring framework - Xmlfile + embedded server = spring boot.
Benefits of spring boot
Spring boot helps
Steps to create SPRING BOOT APPLICATION.
Add the folling dependiencies…
1. Add spring boot starter parent.
2. Add spring boot starter web.
3.Configure java version B.
4.
JPA annotation are used in mapping java object to the data base , tables columns ,etc.