Servlet
Introduction
Java Servlets are programs that run on a Web or Application
server and act as a middle layer between a request coming from a Web browser or
other HTTP client and databases or applications on the HTTP server.
Java Servlets often serve the same purpose as programs
implemented using the Common Gateway Interface (CGI). But Servlets offer
several advantages in comparison with the CGI.
Advantages Of Servlet
Over CGI:-
CGI Technology enables webserver to call an external program
and pass Http request information to external program to process the request
.For Each request , it starts with new Process.
While in Servlet,
The web container
create threads for handling the multiple request to the servlet.since threads
have a lots of benefits over process.
Servlet API
Two packages:
1-javax.servlet
2-javax.servlet.http
represents interfaces and classes for
the Servlet API.
Interfaces in javax.servlet packages:-
1-Servlet
2-ServletRequest
3-ServletResponse
4-RequestDispatcher
5-ServletContext
6-Filter
7-FilterConfig
8-FilterChain
Classes in javax.servlet packages:-
1-Generic Servlet
Life Cycle Of Servlet
Web container maintains the life cycle of the Servlet.
1-Servlet classes are loaded
2-Servlet Instances are created
3-init method is invoked.
4-service method is invoked.
5-destroy method is invoked.
Steps to create and
run the servlet using Tomcat Server and command prompt:
Create directory as given below:
Web-app(context-root)
WEB-INF
Classes(class
files)
lib
web.xml
HTML
Images/css etc.
Configure the web.xml and paste the servlet jar file in side the JRE/lib/ext folder
How Servlet Works:-
The server checks if servlet is requested for the first
time:-
If
Yes , web container does the following task:-
Loads the servlet
Instantiates the
servlet
Calls the init method
to pass the request and response
Else
Calls the service
method passing the request and response
The web container
calls the destroy method when it need or server
undeploy the project or server stops.
How web container
handles the request:-
1-Map the
request with servlet in web.xml.
2-create request and response object for this request.
3-calls te service method on thread
4-public service method internally call the protected
service method.
5-The protected service method calls the doGet() method
depending upon the type of request.
6-the doGet() methodgenerates the response and it passes to
the client.
7-After sending the response,the web container deletes the
request and response objects
RequestDispatcher Interface
The request dispatcher interface provides the facility of
dispatching the request to another resources it may be html,servlet,jsp.
This interface can also be used to include the content of
another resource also .
Two methods
1-public void forward()
2-public void include()
Difference between GET and POST:-
History:
|
Parameters
remain in browser history
because they are part of the URL |
Parameters
are not saved in browser history.
|
Bookmarked:
|
Can be
bookmarked.
|
Can not
be bookmarked.
|
Hacked:
|
Easier
to hack for script kiddies
|
More
difficult to hack
|
Restrictions on form data type:
|
Yes,
only ASCII characters allowed.
|
No
restrictions. Binary data is also allowed.
|
Security:
|
GET is
less secure compared to POST because data sent is part of the URL. So it's
saved in browser history and server logs in plaintext.
|
POST is
a little safer than GET because the parameters are not stored in browser
history or in web server logs.
|
Restrictions on form data length:
|
Yes,
since form data is in the URL and URL length is restricted. A safe URL length
limit is often 2048 characters but varies by browser and web server.
|
No
restrictions
|
Usability:
|
GET
method should not be used when sending passwords or other sensitive information.
|
POST
method used when sending passwords or other sensitive information.
|
GET
method is visible to everyone (it will be displayed in the browser's address
bar) and has limits on the amount of information to send.
|
POST
method variables are not displayed in the URL.
|
|
Cached:
|
Can be
cached
|
Not
cached
|
Idempotent: |
Get
Request is Idempotent.
|
Post is
not Idempotent.
|
Note:-
Imagine an operation that retrieves a particular database record for display. The URL might be something like ".../showOrder?orderID=123456" and is implemented with a GET. Idempotency means that accessing the URL one time or multiple times yields the same result. Clearly it does in this case - accessing this URL gives you the same result every time you access it. It retrieves the web page that shows the record. You can hit reload in the browser as many times as you want - the result is always going to be exactly the same page.Now, POST is generally used to change something, e.g. inserting a new record into a database. If you have just submitted a form that adds a new order record to the database, and if you then hit Reload (in other words, you access the same URL again), you'd be inserting another record. That means the result would not be the same. That's why browsers generally ask the user before reloading a page that was the result of a POST - because POSTs often change something, and doing the same change twice is usually not what the user wants. (As an aside, that's also the reason of existence for the PostRedirectGet technique. It allows you to submit something with POST, and then show a page that can be reloaded without problems.)
[ November 23, 2007: Message edited by: Ulf Dittmer ]
ServletContext Interface
If any information is shared to many servlet, it is better to provide it from the web.xml file using the <context-param> element.
<web-app>
……..
<context-param>
<param-name>c_name</param-name>
<param-value>Jenesys Technology Pvt
Ltd,Bangalore</param-value>
</context-param>…<web-app>
And in any servlet class whatever would be configured in web.xml we can get the values mentioned.
For Example:-
ServletContext
sercon=getServletContext();
String
name2=sercon.getInitParameter("c_name");
//for getting a number of contextparam
values…
·
ServletContext context=getServletContext();
·
Enumeration<string> e=context.getInitParameterNames();
·
·
String str="";
·
while(e.hasMoreElements()){
·
str=e.nextElement();
·
out.print("<br> "+context.getInitParameter(str));
· }
Difference between ServletConfig and ServletContext
The servletconfig
object refers to the single servlet whereas servletcontext object refers to
the whole web application.
|
Attribute in Servlet
ServletContext context=getServletContext();
context.setAttribute("company","IBM");
ServletContext context=getServletContext();
String n=(String)context.getAttribute("company");
Cookies in Session Tracking
Cookie is a mechanism to maintain the state of an user
Session simply
means a particular interval of time.
|
Session Tracking is a way to maintain state of an
user. Http protocol is a stateless protocol. Each time user requests to the
server, server treats the request as the new request. So we need to maintain
the state of an user to recognize to particular user.
|
Why use Session Tracking?
To recognize the user.
|
Session Tracking Techniques
There are four
techniques used in Session tracking:
|
1) Cookies
A cookie is a small piece of information
that is persisted between the multiple client requests.A cookie has a name, a
single value, and optional attributes such as a comment, path and domain
qualifiers, a maximum age, and a version number.
Cookie cook=new Cookie("url","Jenesys
technology Pvt. Blre");
cook.setMaxAge(60*60);//1 hour
response.addCookie(cook);
out.print("cookie has been
created");
Advantage of Cookies
|
Disadvantage of Cookies
|
2-Hidden Form Field
In case of Hidden Form Field an invisible textfield is used
for maintaing the state of an user. In such case, we store the information in
the hidden field and get it from another servlet. This approach is better if we
have to submit form in all the pages and we don't want to depend on the
browser.
No comments:
Post a Comment