Directory
Servlet concept
Create a Servlet program
1. Create a project
2. Import dependencies
3. Create a directory
4. Write code
5. Packager
6. Deployment program
7. Verification procedure
Servlet concept
Servlet is a technology to realize dynamic pages
.
is a group
Tomcat
for programmers
APIs,
Help programmers develop a web app simply and efficiently.
Servlet
Main tasks:
- Programmers are allowed to register a class, and when Tomcat receives a specific HTTP request, it executes some codes in this class.
- Help programmers parse HTTP requests, parse HTTP requests from a string into an HttpRequest object
-
help programmers construct
HTTP
response
.
The programmer only needs to give the specified
HttpResponse
The object fills in some property fields
, the Servlet will automatically install HTTP
protocol to construct a
HTTP
response string
,
and pass
Socket
write back to the client
.
Create a Servlet program
1, create project
use
IDEA
Create
Maven
project
.
1. Menu
->
document
->
New Project
-> Maven
2, import dependencies
After the Maven project is created
,
will automatically generate a
pom.xml
document,
We need in pom.xml
introduced in
Servlet API
dependent
jar
Bag
.
Search for Servlet in Maven Central Warehouse https://mvnrepository.com/ to download
Select the version of the downloaded servlet api package
Servlets
version of
Tomcat
to match
.allowable
http://tomcat.apache.org/whichversion.html
Query version correspondence
Copy the xml provided in the central repository to the pom.xml of the project
3. Create a directory
1) at
main
Under contents
,
and
java
directory side by side
,
Create
webapp
Table of contents
2) Create the folder WEB-INF under the newly created webapp folder, and then create web.xml under this folder
3) Write the following code in the web.xml file
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Archetype Created Web Application</display-name> </web-app>
4, write code
Create a HelloServlet class in the main directory:
@WebServlet("/hello") public class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("hello Servlet"); resp.getWriter().write("hello Servlet"); } }
HttpServletRequest
express
HTTP
ask.
Tomcat
according to
HTTP
The format of the request converts the request in string format into an HttpServletRequest
objectHttpServletResponse
express
HTTP
response.
Construct the response object in the code
(
Construct the status code of the response
, header, body etc.
)resp. getWriter()
Will get the character stream object
,
Some data can be written through the character stream object
,
The written data will be constructed as an HTTP
Response
the body
part
, Tomcat
will convert the entire response to a string
,
pass
socket
Write back to the browser.
be careful:
1. Our code is not entered through the main method. The main method has been included in Tomcat, and the code we wrote will be called by Tomcat at the right time. At this time, the code we wrote is not a complete program, but It is a small part of the logic of the Tomcat program.
2. Can any class we write be called by Tomcat? What conditions must be met to be called?
There are three main conditions:
a) The created class needs to inherit from HttpServlet
b) This class needs to use the @WebServlet annotation to associate the last HTTP path
c) This class needs to implement the doXXX method.
When these three conditions are met, Tomcat can find this class and call it at the right time.
5, packager
Click maven->package to package the program
If the following information appears, the packaging is successful
Note: If you want to modify the name of the packaged compressed package, you can add the following code in the pom.xml file:
<!-- war is the suffix of the compressed package for tomcat --> <packaging>war</packaging> <!-- hello_servlet is the name of the compressed package that you want to modify --> <build> <finalName>hello_servlet</finalName> </build>
6, Deployment Program
Bundle
war
copy the package to
Tomcat
of
webapps
Under contents,
Start Tomcat, Tomcat
will automatically put
war
package decompression
7, verification program
Run the Tomcat file: startup.bat, enter the running file path in the browser, and observe the results
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge Java skill treeHomepageOverview 118919 people are studying systematically