Professional Documents
Culture Documents
Servlet Side Scripting Using Servlets-1
Servlet Side Scripting Using Servlets-1
Servlets
CGI Technologies
The CGI (Common Gateway Interface) is an interface which
handles external programs (CGI scripts) on a web server to
enable the execution of the interactive web pages.
The common gateway interface provides a consistent way for
data to be passed from the user's request to the application
program and back to the user.
One of the examples of CGI flow is the Web browsers send the
forms data to the backend server, and CGI connects to the
application program on the web-server and the program
response to the web browser.
CGI Technologies
CGI was developed by NCSA (National Center for
Supercomputing Applications) in 1993. It resides in the
server side and enables web browsers to interact with
programs on the web server.
For example, if a web page queries a database or a user is
submitting the form information to the server at that time
CGI scripts are invoked.
The server passes that information on to an application in
two ways GET or POST, then the application responds to
the server back to the browser. In this way, browsers get
some results for the user.
Understanding Common Gateway Interface
Advantages
It is simple.
CGI is a technology that interfaces with
HTML.
Speedy since You don’t need to have any
server.
For example: if you develop an web application
in windows machine running Java web server,
you can easily run the same on apache web
server (if Apache Serve is installed) without
modification or compilation of code.
Performance
The java servlets runs very fast.
Servlets run on web server.
For any program initialization takes significant amount
of time. But in case of servlets initialization takes place
first time it receives a request and remains in memory
till times out or server shut downs. After servlet is
loaded, to handle a new request it simply creates a
new thread and runs service method of servlet.
Safety and Secure
6. The web server sends the response back to the client and
the client browser displays it on the screen
Servlet packaging
Servlets are build from two packages:
javax.servlet(Basic)
javax.servlet.http(Advance).
Interface
ServletConfig Serializable
SERVLET class
Built in
Generic Servlet class
Http Servlet
UserDefined Servlets
Various classes and interfaces present in
these packages are:
Component Type Package
HttpServletResponse.
in java.io (for PrintWriter, etc.), javax.servlet (for HttpServlet,
etc.),
and javax.servlet.http (for HttpServletRequest and HttpServletR
esponse).
Finally,that doGet and doPost are called by the service method,
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;
{
@Override // use @Override. It helps catch errors where something has caused the class being
extended to change.
out.println("Hello World!");
}
}
doGet
Notice that there is no main() method in above program. When user enters a
URL on the address line of browser the browser sends the request and the
servlet’s doGet() method is called by the Container (tomcat).
doGet method takes two arguments: HttpServletRequest and
HttpServletResponse
HttpServletRequest lets you get all of the incoming data; like form/query
data, request headers.
HttpServletResponse lets you send document content and reponse headers
back to the client.
doGet method throw two execeptions ServletException and IOException you
need to include them in method signature.
You can get a PrintWriter from the response object. Use the PrintWriter object
to send document content to the browser.
@WebServlet("/hello")
This is the URL relative to the application. HelloWorld.clsss is invoked
with the URL