servlet-1

* The concept : run on the server side applet
* Servlet is an interface that defines the rules for Java classes to be accessed by a browser (tomcat recognition).
* Future we customize a class, Servlet interface replication methods.

* Getting Started:
1. Create a JavaEE project
2. Define a class that implements the Servlet interface
* public class ServletDemo1 the implements Servlet
3. implement abstract methods interface
4. Configure Servlet
configuration in web.xml:
<! - Configure Servlet - ->
<the servlet>
<the servlet-name> the demo1 </ the servlet-name>
<the servlet-class> cn.itcast.web.servlet.ServletDemo1 </ the servlet-class>
</ the servlet>

<-Mapping the servlet>
<the servlet-name > the demo1 </ the servlet-name>
<URL-pattern> / the demo1 </ URL-pattern>
</ Mapping the servlet->

* The implementation of the principle :
1. When the server receives the request from the client browser parses the request URL path, the path Servlet access to resources accessed
2. Find web.xml file, if there are <url-pattern> tag corresponding body content.
3. If there is, then find the corresponding <servlet-class> full class name
4. tomcat will bytecode file loaded into memory, and its object is to create
5. calling its methods

* Servlet life cycle approach is:
1. to be created: the init method, only once
* Servlet when being created?
* By default, the first time it is accessed, Servlet is created
* can be configured to execute the Servlet create opportunity.
* Configuration in <servlet> tag
1. When the first accessed, created
* <load-on-startup> negative value
2. When the server starts creating
* <load-on-startup> value of 0 or a positive integer

* Init method of the Servlet, performed only once, usually used to load resources, indicating a Servlet there is only one object in memory, Servlet is a singleton
when multiple users simultaneously access *, there may be thread-safety issues.
* Solution: Try not to define the member variables in a Servlet. Even if you define a member variable, do not modify the value of

2. Provision of services: service execution method, perform multiple
* each time you visit Servlet, Service method is called once.
3. be destroyed: the implementation destroy method is performed only once
executed when * Servlet is destroyed. When the server is shut down, Servlet is destroyed
* Only server normally closed, will perform the destroy method.
* Destroy method before Servlet is destroyed executed, for general release resources

 

Servlet3.0 *:
* Benefits:
* Support for annotations configuration. You may not need a web.xml.

* Step:
1. Create JavaEE project, select Servlet version 3.0 or more, may not create the web.xml
2. definition of a class that implements Servlet interface
3. The method of replication
4. @WebServlet annotation on the class, configured
* @WebServlet ( "resource path")

 

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
String name() default "";//相当于<Servlet-name>

String[] value() default {};//代表urlPatterns()属性配置

String[] urlPatterns() default {};//相当于<url-pattern>

int loadOnStartup() default -1;//相当于<load-on-startup>

WebInitParam[] initParams() default {};

boolean asyncSupported() default false;

String smallIcon() default "";

String largeIcon() default "";

String description() default "";

String displayName() default "";
}

 

Guess you like

Origin www.cnblogs.com/LihanyangQAQ/p/11532894.html