0% found this document useful (0 votes)
8 views12 pages

Servlet

The document provides a comprehensive guide on setting up and using Servlets and JSP in web applications, detailing the installation of Apache Tomcat, Maven project configuration, and various components of web and application servers. It explains the servlet life cycle, session tracking mechanisms, and the differences between static and dynamic responses, as well as the structure and purpose of JSP files. Additionally, it covers JSP tags, directives, and the implicit objects available in JSP for easier coding.

Uploaded by

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

Servlet

The document provides a comprehensive guide on setting up and using Servlets and JSP in web applications, detailing the installation of Apache Tomcat, Maven project configuration, and various components of web and application servers. It explains the servlet life cycle, session tracking mechanisms, and the differences between static and dynamic responses, as well as the structure and purpose of JSP files. Additionally, it covers JSP tags, directives, and the implicit objects available in JSP for easier coding.

Uploaded by

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

SERVLET & JSP

Path setup:-

Window -> preferences ->Server->Runtime Environments ->Add

Servers  no servers are available . click on that apache Tomcat v9.0


next  download and install click on that feature license I accept the
terms of  finish select location where u want to save wait for sometime
 next finish

Pom.xml error resolving step:-

Right click on pom.xml  run as maven install go to console check for
build success.

Right click on project maven update project check box click  force
update .

Assignment:-

Internet connection must

1. Servlet-001 create maven project


2. Pom.xml we have to add dependencies servlet and JSP
3. Update maven project
4. Print details in console
5. Display data browser
6. action=”perc”
Name
age
gender
email
submit

person.jsp

Person details
name : aaa
age : 18
gender : male
email: [email protected]
SERVLET & JSP

Web Server:-

 Web server is a software which can receive the request from client and it
can serve some web content to the client as response.
 Generally web server only can handle static resources like html , css images
etc.
 Web server is limited to client server communication.
 Example of web server Apache http server , micro soft iis etc.

Application server:-

 Application server is a software which helps to deal with dynamic request


send by client and it is responsible to produce dynamic response by
executing server site programs like servlet , JSP , CGI etc.
 Application server contains both web server and application server
functionalities.
 Application server can interact with data base also in order to perform
authentication and to display data.
 Example of Application server Apache Tomcat , wild fly , j-boss , glass fish
etc.

url contains:- protocol://loacal host:portno/project name/resource

Print writer:-

 Print write is a class present in java.io. package.


 Servlet response contains a non static method called get writer which is
responsible to give instance of print writer.
 Print writer class contains write( ) which accepts String data we have to
write html code or a simple text as argument.
 We can declare/ call write( ) multiple times it will concatinate all the
write( ) content together.
 Example :- PrintWriter pw=res.getWritter( );
SERVLET & JSP

Request Dispatcher:-

 Request dispatcher is a interface present in javax.servlet.package.


 Request dispatcher will help to transfer the code from one dynamic
resources to another resources
 We can get request dispatcher type object by invoking get dispatcher or
servlet request.
 Get request dispatcher ( ) is parameterized method we have to pass resource
as argument in the form of String.
 Request dispatcher contain 2-important methods
1. forward( )
2. include ( )

forward ( ):-

 forward ( ) has request dispatches will accept request and response object
as argument.
 Forward( ) will transfer entire controll from one resource to another
resource so response generated by current page only display on web
browser.

include ( ):-

 include ( ) of request dispatcher will help to transfer controll from one


resource to another resource.
 When we use include ( ) it will display current page response along with
previous page response.
SERVLET & JSP

Web-Application:-

 The applications which can be access through internet is known as web-


applications.
 In general web-applications are deployed into servers we can access them
using web-browser.
 To build web applications we have to follow client server architecture.

Client server Architecture:-


 Client server architecture will help to build web applications
 There are three important component of client server architecture are
1. Client
2. Server
3. Protocol

Client:-
 In client server architecture client is nothing but a web browser.
 In a computer we can have more than one client.
 Client is responsible to send request and to receive response.
 Ex:-chrome , internet explorer , safari , fire fox , opera min ……..etc.

Server:-

 Server is a software , if a machine install with server software will become


server machine.
 There are two types of servers 1) web server and 2)Application server.
 Server is responsible to receive the request generated by client and it will
analyze the request and sends the response.

Static Response:-

 the response which does not changes from user to user is known as static
response.

Dynamic Response:-
SERVLET & JSP

 the response which changes from user to user dynamically based on


sessions or location is known as dynamic response.
 An application server contains two important components
1)main server and 2)container .
 Here main server is responsible for handling static responses like HTML ,
CSS or images etc.
 Container is responsible to handle dynamic resources like servlets , jsp , CGI
(common gate-way interface) etc.

Generic Servlet:-

 It is a Abstract class present in javax.servlet package.


 Servlet interface is the parent of generic Servlet.
 Generic servlet is a direct child of servlet interface and in generic servlet four
abstract methods of servlet interface implementation was provided.
 Generic servlet contains one abstract method that is service.
 If we create a servlet class by extending generic servlet it is mandatory to
provide implementation to service method.
 Container is responsible to invoke service method by passing servlet request
and servlet response objects as argument.

Http Servlet:-
 It is a abstract class without any abstract methods.
 It is present in javax.servlet.http package.
 It is a direct child of generic servlet class and indirect child of servlet
interface.
 http servlet class contains the methods which is specific to http method like
get , post……..etc.

Servlet:-

 Servlet is an API which acts as intermediate layer between user interface


and business layer.
 Servlet is a server site technology which is developed by using java
language.
SERVLET & JSP

 Servlet technology is used for building web application in order to generate


dynamic response.
 By using servlet technology we can perform data operations , database
operations and many more.
 Servlet can respond to http request generated by client.

Servlet ( I ) 5 methods

Generic Servlet ( AC ) 1 methods

Http Servlet ( AC ) 0 methods

Servlet life cycle:-

 Servlet life cycle contains 5- steps


1. Servlet class loading.
2. Instantiation of servlet class.
3. Initialization ----------> invoking int ().
4. Servlet Request Processing -----------> invoking service().
5. De-instantiation. ----------->destroying method.
 Container is responsible to execute servlet life cycle.
 For the first request of servlet first 4-steps will execute from the second
request only 4th step will execute every time
 Before killing the server 5th step will execute.

Servlet interface:-
SERVLET & JSP

 Servlet interface will present in javax.servlet package.


 Servlet interface contains 5-abstact methods 1)init ( ) 2)getservletconfig ( )
3)service ( ) 4)getServletInfo ( ) 5)destroy ( ).
 From this 5-methods there are three life cycle methods init , service , and
destroy method.
 This methods will invoke by container while performing life cycle.

Servlet-session-tracking -----> dependencies ---------> update project.

Session tracking mechanism:-

 Mainly we use session tracking mechanism in order to transfer data from


one resource to another resource to maintain session of a user.
 In general http protocol is stateless so it cannot maintain the session here we
have to use session tracking mechanism to maintain the session.
 There are four important session tracing mechanism
1. URL re-writing
2. Http session
3. Cookie
4. Hidden form field

URL re-writing:-

 In url re-writing technique we have to transfer the data through url in the
form of query String.
 url re writing is suitable for get request.
 In url re writing data will be attached to url in the form of key and value
pairs.

Drawbacks of URL re-writing:-

 url re-writing is only applicable for get type request.


 In url re writing we can transfer only character data.
 url re writing can accept only small amount of data.

Http session:-

 http session is a interface present in javax.servlet.http package.


SERVLET & JSP

 By using request reference we can invoke get session method which will
return implementation class object of http session.
 The type of http session object is singleton.
 By using set attribute method of http session we can add the data into http
session object in key and value pairs.
 Set attribute method will accept two arguments as argument one we have to
pass key in the form of String and argument two will accept value in the of
object.
 By using get attribute method we can get the values we have to pass key in
the form of String to get attribute method and it will return value in the form
of object.

Cookie:-

 Cookie is a class Present in javax.servlet.http package.


 We can insert the key and value pair to the Cookie object by using
constructor.
 Cookie class having a Constructor which accepts two String arguments we
have to pass key as argument one and value as argument two.
 We can transfer Cookie from one resource to another resource with the help
of add Cookie method.
 Add Cookie method is a non-static method present in Response it will
accepts one argument of Cookie type.
 We can retrieve the Cookies by using getCookies method of request it will
return array of Cookies.

Drawbacks of Cookie:-
 One Cooke can hold a single value.
 Cookie does not accepts space.
 The value type of Cookie is String.
 The maximum size of Cookie is 4kb.

Hidden form field:-


SERVLET & JSP

 In this technique programmer is responsible to transfer the data from one


resource to another resource.
 By using print writer we can create form with some hidden fields later we
can redirect to another resource by using form action and submit.

JSP:-

 JSP stands for java server pages or Jakarta server pages.


 JSP technology is mainely used for developing view pages in web
applications.
 JSP is a Dynamic Resource so its life cycle will be performed by container in
order to execute.
 JSP pages internally will be converted to servlet in order to execute.
 Mainely JSP technology is introduced to reduce code complexity while
building web applications ( to reduce html code utilization in servlet
classes ).
 In JSP files we can write both HTML and JAVA codes without changing their
representation.

Tags if JSP:-

 By using JSP tags we can write JAVA code inside JSP pages.
 There are three JSP tags
1. Declaration tag
2. Scriptlet
3. Expression

Declaration:-
 Declaration tag is a class Scope tag we can declare variables , methods and
blocks.
 The code written inside declaration tag will be translated to class block of
translated jsp class.
 Syntax:- <%! %> .

Scriptlist:-

 Scriptlet tag is used for writing instruction for service method .


 The instruction written inside Scriptlet will transfer to service method of
translated JSP.
SERVLET & JSP

 The scope of Scriptlet tag is equals to non-static method block.


 Syntax:- <% %> .

Expression:-
 Expression tag is used for display content on web browser.
 Expression tag contain only one argument and it can accept method calls if
method contains return type.
 Syntax:- <%= %> .

Jsp Directives:-

 There are three JSP directives


1. page
2. include
3. taglib

Page:-

 in JSP file page directives are use-full to import classes , interface and
packages.
 Page directives also contains meta data about JSP pages like language ,
content type etc.
 Syntax:- <%@page key=value %>

Include:-

 In JSP Include directives is used for including one page response into
another page.
 Syntax:- <%@include file=filename %>.

Taglib:-

 In JSP taglib directive is used for importing custom tags to utilize the custom
tags in current JSP file.
 Syntax:- <%@taglib %>.

JSP file translation:-

 Jsp file internally converts into servlet class so JSP files are considered as
dynamic resource.
 While translation class will extend http servlet and implements http jsp base.
 http jsp base interface contains three abstract methods
SERVLET & JSP

1. _jspInit ( ).
2. _jspDestroy( ).
3. _jspService(request req , response)
 Container is responsible to provide implementation for _jspInit and
_jspDestroy ( ).
 Programmer is responsible to provide implementation for _jspService( ).
 Translates jsp class name will be jspfilename_Jsp.

Servlet ( I )
Jsp file
(interface)

httpjspfile
Generic Servlet ( AC )

http servlet ( AC ) httpjspbase

(Interface)

Home.jsp

JSP life cycle:-

 Container is responsible to perform life cycle of JSP file in-order to execute.


 In jsp life cycle there will be 7-steps and 2-phases.
 7-steps are:-
A. Translation phase
1. Jsp- translation.
2. Compilation of servlet class.
B. Execution phase
1. Servlet class loading.
2. Instantiation.
SERVLET & JSP

3. Initialization. -------->jspInit( )
4. Request processing. -------->jspService( )
5. De-instantiation. --------->jspDestroy( )

Implicit objects in Jsp

 In jsp file to build java codes we frequently use some objects like http servlet
request , http servletresponse……. etc.
 In jsp files all the frequently used object references will be available by
default these objects are known as implicit Objects.

Ref Type
application Servlet context
config Servlet config
out Jspwritter
page Object
pagecontext Pagecontext
request Httpservletrequest
response Httpservletresponse
session Httpsession

Jsp actions:- by using jsp actions we can avoid java codes from jsp file.

JDBC –steps of JDBC.

You might also like