Servlet Basics (i)

Source: Internet
Author: User
Servlet One, what is a servlet?

A servlet is a Java class that is hosted and executed on a Web server that accepts client requests (via a Web server), performs tasks, and returns results, in a way that is compliant with the Servlet API specification, as a client-side applet. Because it is a Java class itself, it has all the benefits of Java, as well as its own advantages: interacting with other resources, security, and protocol-independent.

Second, triangular relationship

Specification makers, container providers, component developers.

The official name of the Servlet API is the JSR (Java request specification), which is part of the JCP (Java Community Process). Before the JSR is published, he provides sample code that not only examines the design, but also provides examples for container providers (Servlet engines) and developers. This is called the Reference Implementation (RI). Apache Tomcat is the JSR ri, he is part of the Apache organization, the Apache Charter is "to provide a Java platform for the service provider solution, the Java platform is developed in an open and cooperative way." ”

"It feels very much like the patterns in EJB, Sun, the specification, ibm,bea the containers based on these specifications, and we, the application agents, write components based on these specifications, because the container and the component implement the same interface, or follow the same specification, You can make a component that is written on one container work well on another container that implements the specification. And here? Sun offers (or some of the sun's staff) the servlet APIs, which are interfaces, base classes, and specific requirements specifications, and then each container vendor writes a container based on the counter implementation interface , and our applications write components based on these specifications and the base classes provided by the container manufacturer, which is the servlet or class. ”

Three, the Tomcat directory problem

Bin to start and stop Tomcat scripts.

Common classes and jar files that are common to all Tomcat servers.

CONFG General configuration information, such as server definitions and user information.

WebApps A Web application that is automatically loaded when Tomcat starts.

Logs the Run-time log file generated by Tomcat.

Works is a temporary working directory used by the Web application.

Note: Logs and works were created the first time that Tomcat was started.

Four, Servlet API

The interface name: Javax.servlet.Servlet;

Realized in: Javax.servlet.GenericServlet;

1,init (), and the servlet container calls.

2,public javax.servlet.ServletConfig getservletconfig ();

Get the ServletConfig object.

3,service (), called by the servlet container, allows the servlet to respond to requests.

4,public String getservletinfo ();

Returns information about the servlet, such as author, version, copyright, and so on.

5,destroy () gives the servlet the opportunity to clear all the resources (memory) it occupies.

Two >,servletconfig

Interface Name: Javax.servlet.ServletConfig;

A servlet configuration object established by the servlet container to pass information to the servlet during initialization.

1, public String getservletname ();

2, Public ServletContext getservletcontext ();

Returns a reference to the ServletContext that executes the servlet.

3, Pulic enumeration getinitparameternames ();

Returns a list of servlet initialization parameter names.

4, public string Getinitparameter (string mystring);

Returns the value of a specific parameter name.

For example, in Web.xml we define this end:

<servlet>

<servlet-name>myservlet</servlet-name>

<servlet-class>lsmx. Myservlet </servlet-class>

<init-param>

<param-name>myparamname</param-name>

<param-value>myparamvalue</param-value>

</init-param>

</servlet>



public class Myservlet extends HttpServlet {

Slightly

public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {



ServletConfig config = This.getservletconfig ();

System.out.println (Config.getservletname ());

enumeration enum = Config.getinitparameternames ();

while (Enum.hasmoreelements ()) {

String myparametername = (string) enum.nextelement ();

String Myparametervalue = Config.getinitparameter (myparametername);

System.out.println (Myparametername);

System.out.println (Myparametervalue);

}

Slightly

}

is printed on the console:

Myservlet (<servlet-name>myservlet</servlet-name>), Myparamname,myparamvalue

Three >,genericservlet

Class name: Javax.servlet.GenericServlet;

Super class: Java.lang.Object;

Direct subclass: Java.servlet.http.HttpSrvlet;

Realized in: Javax.servlet.Servlet;

Javax.servlet.ServletConfig;

java.io.Serializable;

Defines a normal, protocol-independent servlet. It has all the methods of servlet and ServletConfig. The use and return value of the method is similar to the above, which is no longer explained here.

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.