Day 1: servlet related knowledge (1)

Source: Internet
Author: User

Servlet knowledge 1
1. What is Servlet? What can we do?
Sun's component specification for expanding web server functionality (early CGI: Common Gateway Interface)
Extension:
The Web server itself has no computing capability, that is, it cannot process dynamic resource requests. Servlet can process dynamic resource requests.
Component specification:
A component is a software module that complies with specifications, completes certain functions, and can be separately deployed. Generally, it cannot run independently. It must be dependent on the container to run.
JSR: Java specification request
JCP: Java communication process
Container:
Is a program that provides the runtime environment of components and manages the runtime of components.
Servlet container:
Provides a servlet running environment and manages the servlet running cycle. Common web servers, such as Tomcat,
A servlet container is also a web server.
What can we do?
Servlet can only be developed in Java. servlet can be used as all the events that Java classes can do.
2. How to develop a Servlet?
Step 1: Write a Java class to implement the servlet interface or inherit the httpservlet class.
Step 2: Compile (dependent on servlet API)
Step 3: Package (define a specific directory structure, and put the corresponding bytecode, configuration file, and related jar files into the corresponding directory structure)
Appname (any name, usually called Application name)
WEB-INF (must be named by this name)
Classes (put the. Class file)
Lib (optional, put the. jar file)
Web. XML (deployment description file)
Step 4: deploy (put the result in step 3 to a specific directory, which is webapps for Tomcat)
Step 5: Start the server
Windows-> startup. Bat-> shutdown. bat
Linux-> startup. Sh-> shutdown. Sh
Started successfully: http: // localhost: 8080/
Step 6: Access Servlet
Http: // localhsot: 8080/appname/url-Pattern
3. How does the servlet run?
Assume that the following address is entered in the browser: http: // ip: Port/appname/servletname
Step 1: The browser connects to the server based on the IP address and port.
Step 2: the browser splits the request data into an HTTP request packet (including appname/servletname) based on the HTTP protocol)
Step 3: an instance of httpservletrequest will be created by a communication module (generally called the servlet engine) in the server.
(Httpservletrequest is actually an interface implemented by the server provider). This instance contains
Data. In addition, an instance of httpservletresponse is created.
Step 4: The server finds web. XML based on appname/url-pattern and finds the corresponding servlet class name based on the information. View the Servlet
There is no corresponding instance. If not, an instance is created. If yes, no existing instance is used. Next, call service ()
The code in the service method can obtain the data in the request through the method provided by httpservletrequest.
You can set the returned results through the method provided by httpservletrequest.
Step 5: The Servlet Engine obtains data from httpservletresponse, generates an HTTP response packet, and sends it to the browser.
4. Use myeclipse to develop Servlet
(1) install myeclipse
(2) development steps
Step 1: create a web project
Step 2: Write Java classes under SRC
Step 3: Configure servlet in Web. xml
Step 4: deploy
A. Let myeclipse manage Tomcat
Window-> preferences-> myeclipse-> servers-> tomcat-> Tomcat 6.x
B. Deployment
Step 5: Access
Http: // ip: Port/appname/url-Pattern
5. Web. xml file content
<? XML version = "1.0" encoding = "UTF-8"?>
<! -- The first line of the file must be the above information, indicating the XML file version and character encoding -->
<! -- The following is used to configure a web project -->
<Web-app version = "2.4"
Xmlns = "http://java.sun.com/xml/ns/j2ee"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
<Servlet>
<Description> helloservlet </description>
<Display-Name> helloservlet </display-Name>
<! -- Generally the name used to define the servlet -->
<Servlet-Name> helloservlet </servlet-Name>
<! -- Full name of the servlet -->
<Servlet-class> ttwork. servlet. helloservlet </servlet-class>
</Servlet>
<Servlet>
...
</Servlet>

<Servlet-mapping>
<! -- Same as the name used by the servlet above -->
<Servlet-Name> helloservlet </servlet-Name>
<! -- Relative path when accessing servlet -->
<URL-pattern>/servlet/helloservlet </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
...
</Servlet-mapping>

<Error-page>
<Error-code> 500 </error-code>
<Location>/error.html </location>
</Error-page>

<Welcome-file-List>
<Welcome-File> index. jsp </welcome-File>
</Welcome-file-List>
</Web-app>

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.