Servlet 小知识

1.In servlet 3.0 we have new feature annotations to replace XML.也就是说应该尽量使用annotations

  1. Servlet is an API that provides many interfaces and classes to create web applications.

APIs to create Servlets
javax.servlet and javax,servlet.http are the two packages required to create servlets.

3.What is javax?
The javax prefix is used by the Java programming language for a package of standard Java extensions.
These include extensions such as javax.servlet,which deals with running servlets,and javax.jcr,which deals with the java content library.

The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets.
All servlets must implement the Servlet interface,which defines lifecycle methods.
When implementing a generic service ,you can use or extend the GenericServlet class provided with the Java Servlet ApI.
The HttpServlet classes provides methods,such as doGet and doPost, for handling HTTP-specific services.

  1. 3 ways to create a servlet:
    -extending HttpoServlet Class
    -extending GenericServlet class
    -implementing Servlet interface.

The mostly used approach is by extending HttpServlet because it provides http.request specific method such as doGety(),.doPost(),doHead() etc.

Servlet Lifecycle

The lifecycle of a servlet is controlled by the container in which the servlet has been deployed .
When a request is mapped to a servlet, the container performs the following steps.
1.If an instance of the servlet does not exist ,the web container
a.Loads the servlet class .
b.Creates an instance of the servlet class.
The servlet instance is created only once int the servlet life cycle.
c.Initializes the servlet instance by calling the init method.

2.Invokes the service method passing request, and response objects.

Web Pages are 2 types
a,Static page-Page is already made and same for all users.
b.Dynamic page-Page that loads at runtime and page changes as per the request from the user.

Tomcat Server
Tomcat is an HTTP server or web server and an HTTP container.
HTTP stands for Hyper Text Transfer Protocol used to communicate between client and server.
Tomcat converts JSP to servlets.

猜你喜欢

转载自www.cnblogs.com/zienzir/p/9000849.html