Servlet Basic Notes

Source: Internet
Author: User

1. What is a servlet? What can I do?
A component specification developed by Sun to extend the functionality of Web servers.
Extension: The Web server itself has no computing power, that is, it cannot handle requests for dynamic resources. A servlet can handle requests for dynamic resources.
Component specification:
Components are software modules that conform to specifications, perform certain functions, and can be deployed separately.
Components are generally not run independently and need to be dependent on the container to run.
Container:
is a program that provides a running environment for components and manages the lifecycle of components.
servlet container:
Provides the environment in which the servlet runs and manages the life cycle of the servlet.
Common Web servers such as Tomcat, in addition to being a servlet container,
is also a Web server.
What can I do?
Servlets can only be developed in the Java language, and Servlets do all the things that the Java class can do.
2. How to develop a servlet?
Step1 Write a Java class that implements the Servlet interface or inherits the HttpServlet class.
Step2 compilation (dependent servlet API)
Step3 package (define a specific directory structure, put the corresponding bytecode, configuration files, related jar files into the corresponding directory structure)
AppName (name arbitrary, commonly called application name)
Web-inf (must have, must be named with this name)
Classes (must have, this must be named, put the bytecode. class file)
Lib (optional, this must be named, put. jar file)
Web. XML (Deployment description file)
html/jsp file
Note: the Web. xml file Content Example:
< header >
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>first (package name). HelloServlet (servlet name) </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet<servlet-name>
<url-pattern>/hello (must have/) </url-pattern>
</servlet-mapping>
STEP4 deployment (puts the package structure of the Step3 folder into a server-specific directory, which is webapps for Tomcat)
STEP5 starting the server
Start success Judgment: http://localhost:8080
STEP6 Access servlet
Http://localhost:8080/appname/url-pattern
3. How does a servlet work?
If you enter the following address in the browser address bar: Http://ip:port/appname/url-pattern
1) Browser A sentence Ip,port connection server;
2) The browser calls the request data into an HTTP request packet (including/appname/url-pattern) by HTTP protocol
3) A module in the server that is responsible for communication (commonly called a servlet engine) creates an instance of HttpServletRequest (
HttpServletRequest is actually an interface that is implemented by the server provider to create only instances of the interface and cannot create objects.
The instance contains the data in the request packet. In addition, an instance of HttpServletResponse was created (also a
Interface).
4) The server finds the Web. XML based on Appname/url-pattern and finds the corresponding Servlet class name according to the information in Web. Xml.
See if the servlet has a corresponding instance, if not, create an instance, and if so, do not create it, using a ready-made instance.
Next, the service () method is tuned.
public void Service (HttpServletRequest request,httpservletresponse response) {}
The code inside the service method can obtain the data in the request by means of the method provided by HttpServletRequest, and the result after processing can be
The returned result is set by the method provided by HttpServletResponse.
5) The Servlet engine obtains data from HttpServletResponse, generates an HTTP response packet, and sends it to the browser.
4. Using MyEclipse to develop servlets
1) Install MyEclipse
2) Development Steps
Step1 Building a Web project
Step2 writing Java classes under SRC
STEP3 configuration in Web. xml
SETP4 deployment
A. Let MyEclipse manage Tomcat
B. Deployment
STEP5 Access
Http://ip:port/appname/url-pattern

Servlet Basic Notes

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.