Download Servlet and MVC - Agile Computers

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Cross-site scripting wikipedia , lookup

Remote Desktop Services wikipedia , lookup

Lag wikipedia , lookup

Hypertext Transfer Protocol wikipedia , lookup

Transcript
Servlet and MVC
Assgnment 1
1 Explain MVC architecture.
Model View Controller or MVC as it is popularly called, is a software design pattern for developing web
applications. A Model View Controller pattern is made up of the following three parts:
Model - The lowest level of the pattern which is responsible for maintaining data.
View - This is responsible for displaying all or a portion of the data to the user.
Controller - Software Code that controls the interactions between the Model and View.
MVC is popular as it isolates the application logic from the user interface layer and supports separation
of concerns. Here the Controller receives all requests for the application and then works with the Model
to prepare any data needed by the View. The View then uses the data prepared by the Controller to
generate a final presentable response. The MVC abstraction can be graphically represented as follows.
The model
The model is responsible for managing the data of the application. It responds to the request from the
view and it also responds to instructions from the controller to update itself.
The view
A presentation of data in a particular format, triggered by a controller's decision to present the data.
They are script based templating systems like JSP, ASP, PHP and very easy to integrate with AJAX
technology.
The controller
Compiled By Raj Sir
Agile Computers
Servlet and MVC
The controller is responsible for responding to user input and perform interactions on the data model
objects. The controller receives the input, it validates the input and then performs the business
operation that modifies the state of the data model.
2. Explain life cycle of servlet.
A servlet life cycle can be defined as the entire process from its creation till the destruction. The
following are the paths followed by a servlet
The servlet is initialized by calling the init () method.
The servlet calls service() method to process a client's request.
The servlet is terminated by calling the destroy() method.
Finally, servlet is garbage collected by the garbage collector of the JVM.
Now let us discuss the life cycle methods in details.
The init() method :
The init method is designed to be called only once. It is called when the servlet is first created, and not
called again for each user request. So, it is used for one-time initializations, just as with the init method
of applets.
The servlet is normally created when a user first invokes a URL corresponding to the servlet, but you can
also specify that the servlet be loaded when the server is first started.
When a user invokes a servlet, a single instance of each servlet gets created, with each user request
resulting in a new thread that is handed off to doGet or doPost as appropriate. The init() method simply
creates or loads some data that will be used throughout the life of the servlet.
The init method definition looks like this:
public void init() throws ServletException {
// Initialization code...
}
The service() method :
The service() method is the main method to perform the actual task. The servlet container (i.e. web
server) calls the service() method to handle requests coming from the client( browsers) and to write the
formatted response back to the client.
Each time the server receives a request for a servlet, the server spawns a new thread and calls service.
The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet,
doPost, doPut, doDelete, etc. methods as appropriate.
Here is the signature of this method:
Compiled By Raj Sir
Agile Computers
Servlet and MVC
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}
The service () method is called by the container and service method invokes doGe, doPost, doPut,
doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you
override either doGet() or doPost() depending on what type of request you receive from the client.
The doGet() and doPost() are most frequently used methods with in each service request. Here is the
signature of these two methods.
The doGet() Method
A GET request results from a normal request for a URL or from an HTML form that has no METHOD
specified and it should be handled by doGet() method.
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The doPost() Method
A POST request results from an HTML form that specifically lists POST as the METHOD and it should be
handled by doPost() method.
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Servlet code
}
The destroy() method :
The destroy() method is called only once at the end of the life cycle of a servlet. This method gives your
servlet a chance to close database connections, halt background threads, write cookie lists or hit counts
to disk, and perform other such cleanup activities.
After the destroy() method is called, the servlet object is marked for garbage collection. The destroy
method definition looks like this:
Compiled By Raj Sir
Agile Computers
Servlet and MVC
public void destroy() {
// Finalization code...
}
Compiled By Raj Sir
Agile Computers
Servlet and MVC
Difference between Get and Post Request.
HTTP POST requests supply additional data from the client (browser) to the server in the message body.
In contrast, GET requests include all required data in the URL. Forms in HTML can use either method by
specifyingmethod="POST" or method="GET" (default) in the <form> element. The method specified
determines how form data is submitted to the server. When the method is GET, all form data is encoded
into the URL, appended to theaction URL as query string parameters. With POST, form data appears
within the message body of the HTTP request.
Compiled By Raj Sir
Agile Computers
Servlet and MVC
Difference between generic and http servlet
Generic Servlet:
GenericServlet class is direct subclass of Servlet interface.
Generic Servlet is protocol independent.It handles all types of protocol like http, smtp, ftp etc.
Generic Servlet only supports service() method.It handles only simple request
public void service(ServletRequest req,ServletResponse res )
Generic Servlet only supports service() method.
HttpServlet:
HttpServlet class is the direct subclass of Generic Servlet.
HttpServlet is protocol dependent. It handles only http protocol.
HttpServlet supports :
public void service(ServletRequest req,ServletResponse res )
protected void service(HttpServletRequest req,HttpServletResponse res)
HttpServlet supports also
doGet(),doPost(),doPut(),doDelete(),doHead(),doTrace(),doOptions()etc.
Compiled By Raj Sir
Agile Computers
Servlet and MVC
What is the purpose of war file?
.war files: The war file contains the web application that can be deployed on any servlet/jsp container.
The .war file contains jsp, html, javascript and other files necessary for the development of web
applications.
Disadvantages of CGI System over servlet?
Servlets are server side components that provides a powerful mechanism for developing server web
applications for server side. Earlier CGI was developed to provide server side capabilities to the web
applications. Although CGI played a major role in the explosion of the Internet, its performance,
scalability and reusability issues make it less than optimal solutions. Java Servlets changes all that. Built
from ground up using Sun's write once run anywhere technology java servlets provide excellent
framework for server side processing.
Using servlets web developers can create fast and efficient server side applications and can run it on any
servlet enabled web server. Servlet runs entirely inside the Java Virtual Machine. Since the servlet runs
on server side so it does not depend on browser compatibility.
Servlets have a number of advantages over CGI and other API's. They are:
Platform Independence
Servlets are written entirely in java so these are platform independent. Servlets can run on any Servlet
enabled web 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. Platform independency of servlets provide a great advantages over
alternatives of servlets.
Performance
Due to interpreted nature of java, programs written in java are slow. But the java servlets runs very fast.
These are due to the way 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. In comparison to traditional CGI scripts
which creates a new process to serve the request.
Extensibility
Java Servlets are developed in java which is robust, well-designed and object oriented language which
can be extended or polymorphed into new objects. So the java servlets take all these advantages and can
be extended from existing class to provide the ideal solutions.
Safety
Java provides very good safety features like memory management, exception handling etc. Servlets
inherits all these features and emerged as a very powerful web server extension.
Secure
Servlets are server side components, so it inherits the security provided by the web server. Servlets are
also benefited with Java Security Manager.
Compiled By Raj Sir
Agile Computers
Servlet and MVC
Advantages of J2EE Technologies
J2EE makes the Java language to embrace a number of server-side applications, such as Web-based and
e-commerce applications. Apart from all the innovative and path-breaking features that Java provides,
J2EE technologies brings some additional features most suitable for all enterprise systems.
J2EE makes Java a fully-fledged server-side development platform.
J2EE has a solid infrastructure that provides a well-tested implementation of many common applications
that need security and messaging facilities.
J2EE standardizes development making it easier for companies to commit to J2EE and share third party
code.
J2EE provides a solid backend for wireless applications and many wireless companies use J2EE on the
server for this purpose.
Java is the primary language for designing many exciting application servers of today and hence ecommerce applications fully utilize the guaranteed services of Java.
The use of Java as a development language and architecture for Enterprise Application
Integration (EAI) is widespread.
Compiled By Raj Sir
Agile Computers