Complete notes Servlet Day-1
Servlet class notes by girraj sir:
What is web application/website?
> Any web project having its services online 24X7 over the network world wide know as web application.
> Any web project under the development known as web application.
> Any web project after deployement becomes website
What is web contents/resources?
> Whatever used inside web project is nothing but web resources.
ex: html,css,java script,servlet,jsp,jar files,properties,video,audio,image...etc.
There are two types of web resources
1. static web resource or active web resource
2. dynamic web resource or passive web resource
1. static web resource or active web resource:
If response of any web resource will not depends/change upon a client request known as static web resource.
Ex: html file,PDF file,documents, image,audio,video,
2. Dynamic web resource or passive web resource:
If response of any web resource will be depends upon a client known as dynamic web resource.
Dynamic web resource can generate different-2 type of response in order to different-2 client request.
Ex: Servlet, JSP.
What is Servlet?
> Servlet is a server side dynamic web components which is managed/executed by web container
> Servlet is a sub (open) specification of JEE/J2EE module.
> Serlvet is a web technology by using which we can develop dynamic web application.
> Servlet is an API interface which is a part of servlet specification. Servlet interface available inside javax.servlet package.
What is Servlet class?
> Any java class which implements javax.servlet.Servlet interface either directly or indirectly known as servlet class.
LoginServlet.java
public class LoginServlet implements Servlet{
}
What is web container?
> Web container is a special kind of software which is responsible for managing/executing servlet and jsp components and generating dynamic response.
There are two types of web container
a. In-Bound web container
It is by default available with web server as in-built
b. Out-Bound web container
It is not available by default, we need to seperate installation.
Which type of JEE software are required for managing servlet or jsp applications?
> JEE is an api open specification provided by sun for developing jee based server software
> Sun doesnt provide jee software.
Who is responsible for providing jee based server software?
Ans: Anybody can provide our own jee software by following jee specification provided by sun.
Most popular JEE server:
1. Tomcat web server(Apache Software Foundation)
2. Web Sphere Application server (IBM corporation)
3. Web Logic Application server(oracle Corporation)
4. Glassfish Application Server (sun/oracle)
5. JBoss Application Server (ASF)
JEE based servers are divided into two categories:
1. Web server(Web container)
> Since web servers are mainly responsible for managing servlet and jsp components therefore it is responsible for supplying web container.
ex: Tomca7
2. Application server (Web container + EJB container)
> Application servers will provides web container as well as ejb container.
Servlet+JSP: Web container
EJB : EJB container
Required softwares for servlet and jsp application(website):
1. JDK(any version)
2. Database Software (mysql/oracle)
3. Web server (Tomcat)
4. MyEclipse or Eclipse
How to install tomcat web server?
Step1: Make sure JDK software path available in permanent setting
Step2: extract tomcat software downloaded zip/rar file
Step3: Double click on apache-tomcat-7.0.12.exe file
>yes >Next > I Agree > Select the type of install: Full
>username: admin
>password: admin
> next > next
> Change Destinition folder: C:\Tomcat7
> Install > Make unchecked all checkbox > Finish
Changing startup type of tomcat service:
> Open Run: services.msc > Ok
> Select Apache Tomcat7 service and right click it
> click on properties
> change startup type as manual > ok
Setting permanent path of tomcat7.exe file:
What is JEE Specification?
> JEE is an open specification which is provided sun.
> At the time of JDK installation only JSE module going to be installed in your machine. JDK s/w doesnt provide JEE specification and JEE implementation
Who is responsible for providing JEE specification?
ans: SUN
Who is responsible for providing JEE implementation?
Ans: JEE implementation can be provided by any one
a. sun
b. third party vendor company
ex: Apache, IBM, redhat...
Note: All JEE based server s/w are comes under the category of JEE implementation.
Whenever JEE specification available in our machine?
Ans: JEE specification automatically available at the time of JEE based server installation.
In tomcat server which jar file having servlet specification related interfaces,classes and packages?
Ans:
tomcat: servlet-api.jar =>javax.servlet.Servlet
web sphere: j2ee.jar =>javax.servlet.Servlet
In tomcat server where the servlet-api.jar file is available?
Ans: Tomcat_Home\lib\*.jar
What is website/web appl according servlet specification?
ans: web application is nothing is a folder/directory
Architecture of web application:
> any web application identified by folder name
ie. mysite
> every web application must have WEB-INF folder
mysite\WEB-INF
> WEB-INF must have two folders (classes,lib) and web.xml file
> web project related all *.class files and package folder should be placed inside classes folder.
> web project related third party given jar files should placed inside lib folder.
![]() |
| Architecture of mywebsite |
> Any web application having two types of resources
a. private web resource
b. public web resource
private web resource:
> Any web resource related file or folders which is available inside WEB-INF folder known as private web resource
> We cant access private web resource(servlet,jsp,html) directly from a browser by using their names.
> We must provide configuration for private web resource inside web.xml file, this web.xml file also known as web configuration or configuration file.
What is configuration file?
Ans: web.xml file is known as configuration file.
![]() |
| Hierarchy of tags |
Important tags/elements of web.xml file?
web.xml
---------
<web-app>
<servlet>
<servlet-name> ? </servlet-name>
<servlet-class> ? </servlet-name>
</servlet>
<servlet-mapping>
<servlet-name> ? </servlet-name>
<url-pattern> ? </url-pattern>
</servlet-mapping>
</web-app>
What are the various steps for creating/managing web application?
1. creating web application
2. deploying web application on the server location(apache)
3. requesting web resource based on requirement by using web browser
Developing static web application using html page?
> Open command prompt
> e:
e:\>md servlet
e:\>cd servlet
e:\servlet>md mysite
e:\servlet>cd mysite
DAY-2
What is mean by web deployment?
> Deployment is a process of make available the contents web project to the web server.
> In tomcat server all running web application should be available inside 'webapps' folder provided by the tomcat server
c:\tomcat7\webapps
How many way of deploying a web project?
a. manual deployment
> In this approach we can deploy a web project by copying the contents web application folder and past inside Tomcat_Home\webapps.
Note: It is not recommended
b. war file deployement (recommended)
c. maven deployment
> Start the tomcat server
home>tomcat7
> sending request by using web browser(client appl)
URL: http://IP:Portno/webappl/resource
Sending request to the tomcat home page
> http://localhost:8080
first.html:
>http://localhost:8080/mysite/first.html
second.html:
> http://localhost:8080/mysite/abc/second.html
third.html:
> http://localhost:8080/mysite/WEB-INF/third.html
four.html
---------
http://localhost:8080/mysite/WEB-INF/xyz/four.html
> We cant access private web resource directly by using their name.
> By using web resource name we can access only public web resource.
mysite3:
> modified version of mysite
> adding web.xml file by providing url configuration for private web resource
Example:
mysite3
|
+--WEB-INF
home.html
web.xml
step1: create mysite3 folder
step2: inside mysite3 create WEB-INF folder
step3: inside WEB-INF create home.html page
step4: inside WEB-INF create web.xml by given configuration
step5: copy mysite3 folder inside webapps directory of tomcat7
step5: start tomcat server
step6: send request to the home.html page
url: http://localhost:8080/mysite3/welcome
Note: Even though we can place html and jsp documents inside WEB-INF location but it is not recommended. It is highly recommended to place html and jsp file in public location (outside WEB-INF folder).
Now starting Servlet:
What is Servlet?
> Servlet is an api interface available inside javax.servlet package
What is servlet class?
> Any java class which is inherited from javax.servlet.Servlet interface either directly or indirectly known as servlet class.
Important package of servlet api specification?
a. javax.servlet package
b. javax.servlet.http package
c. javax.servlet.anotations package
Important interfaces of servlet api:
------------------------------------
> Servlet
Important classes related to servlet api:
-----------------------------------------
Methods of javax.servlet.Servlet interface:
> init()
> service()
> destroy()
> getServletConfig()
> getServletInfo()
note All method are abstract method so we can also implement in child class
What are the life cycle methods of servlet?
> init()
> service()
> destory()
What is mean by servlet life cycle methods?
> Any methods which is automatically executed by the web container from servlet object creation to servlet object deallocation known as life cycle method of that object.
What are the rules for creating servlet class?
1. Servlet class must be inherited from javax.servlet.Servlet interface either directly or indirectly.
2. servlet class must be public
3. servlet class must contain public zero argument constructor
Example:
filename: LifeCycleServlet.java
----------------------------------------
import javax.servlet.*;
public class LifeCycleServlet implements Servlet{
public LifeCycleServlet(){
}
init();
service();
destroy();
getServletConfig();
getServletInfo();
}






Comments
Post a Comment