• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java Web Learning (4) — servlet concept / servlet running process / servlet implementation class /

King Wang

1 月 3, 2022

JavaWeb Study ( Four )—Servlet Concept /Servlet Operation process /Servlet Implementation class / Use IDEA Development Servlet

01 Servlet Concept

  • Servlet yes sun Developed by the company for the development of dynamic web Technology of resources , It’s one for other Java Program (Servlet engine ) Called Java class , It can’t run independently , It runs entirely by Servlet Engine to control and schedule .
    For clients many times Servlet request , Usually , The server will only create one Servlet Instance object , in other words Servlet Once the instance object is created , It will reside in memory , Serve other subsequent requests , until web Container exit ,servlet Instance object Will destroy .
    stay Servlet Of Whole In the life cycle ,Servlet Of init Method Only called once . And to one Servlet Of Every access request results in Servlet Engine call once servlet Of service Method . about Every visit request ,Servlet The engine will be Create a new HttpServletRequest Request object and A new HttpServletResponse The response object , then Pass these two objects as parameters to the Servlet Of service() Method ,service Method and call respectively according to the request mode doXXX Method .

  • Development trends WEB Resources need to complete two steps :
    • Write a Java class , Realization Servlet Interface .IDEA2018 Version can create a servlet
    • Put the developed Java Class deployment to web Server ( To configure web.xml file )

02 Servlet Operation process

Servlet The procedure is made by WEB Server calls ,web The server received… From the client Servlet After access request :
①Web The server first checks to see if the… Has been loaded and created Servlet Instance object of . If it is , Then directly execute the ④ Step , otherwise , Execution section ② Step .
② Mount and create the Servlet An instance object of .
③ call Servlet Of instance objects init() Method .
④ Create one for encapsulation HTTP Asking for information HttpServletRequest Object and a representative HTTP In response to the message HttpServletResponse object , And then call Servlet Of service() Method and pass in the request and response objects as parameters .
⑤WEB Before the application is stopped or restarted ,Servlet Engine will be uninstalled Servlet, And call before unloading. Servlet Of destroy() Method .

Web browser ————– issue http request ————————–>Web Containers

Web Containers —————— First visit create target Servlet———>Servlet

Web Containers —————— Create request and response objects —————->request,response

Web Containers ——————Servlet Method —————————->Servlet.service(req,resp)

service(req,resp) Get request information , And write the response information back to WEB Containers

Web Containers —————— issue http Respond to ————————–>Web browser


03 Servlet Implementation class

SUN The company defines two default implementation classes , Respectively :GenericServlet、 as well as HttpServlet

  • HttpServlet: Able to handle HTTP request , In the original Servlet The interface has added some information about HTTP How to deal with the agreement , Than Servlet The interface is more powerful . When developing , It usually inherits this class .

HttpServlet In the realization of Servlet Interface , Overwrite the service Method , The code in the method body will automatically determine the user’s request mode , If the time is GET request , It will call doGet Method , If it is POST request , It will call doPost Method . When developing , Usually, it’s just an overlay doGet perhaps doPost One of the ways , Instead of rewriting service Method , And then in doPost perhaps doGet Call the overwritten method .

public class Test01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Override code
}
}

04 Use IDEA Development Servlet

  • stay IDEA Create a new one in Web project , Create a new class or create a new one Servlet(IDEA2018 Above version support )
  • Import Servlet Needed jar package : Create a new class ,IDEA It can guide packets intelligently , Create a new class , Inherit javax.servlet.http.HttpServlet
public class Test01 extends javax.servlet.http.HttpServlet{
//alt+enter Complete the guided package
}
  • Write code
  • To configure web.xml in Servlet The mapping of mapping URL( Usually a URL For a request , By mapping class The file handles the request )
    The focus is on the need for Servlet Mapped to URL Can also be used in * wildcard , But there are only two fixed formats : One format is *. Extension , Another format is forward slash (/) Start with /* ending .
    The same Servlet Can be mapped to multiple URL On , That is many <servlet-mapping> Elemental <servlet-name> The setting value of the child element can be the same Servlet Registered name of .
    If a Servlet The mapping path of is just a forward slash (/), So this Servlet Become the current Web Application default Servlet.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>Test01</servlet-name>
<servlet-class>com.hooi.demo01.Test01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Test01</servlet-name>
<url-pattern>/test01</url-pattern>
</servlet-mapping>
</web-app>
  • start-up Tomcat The server
  • Through the corresponding url Access page

发表回复