当前位置:网站首页>Servlet fast foundation building

Servlet fast foundation building

2022-06-24 09:37:00 Soup key TJ

Catalog

Servlet Thread safety problem

Servlet Different mapping methods

Servlet Multipath mapping

Servlet Create time

Default Servlet


  • Servlet Thread safety problem

  • because Servlet It's using The singleton pattern , That is, there is only one instance object in the whole application
  • So we need to analyze whether the class members in this unique instance object are thread safe
  • A browser represents a thread , Multiple browsers represent multiple threads
  • It is reasonable to say that what we expect is that each browser should view its own user name , But the result is a mess of data in the browser
  • therefore , It can be said that Servlet It's not thread safe
  • solve :
  • Be careful when defining class members
  • If it's shared , And will only be assigned at initialization time , If other times are obtained , Then there's no problem
  • If it's not shared , Or it can be assigned a value every time it is used , Then we have to consider thread safety ; It can be defined as doGet or doPost Method or use the synchronization function
  • Servlet Different mapping methods

  • The first one is
  • The specific name of the way
  • The resource path to be accessed must be exactly the same as the mapping configuration
  • http://localhost:8080/demo2/cpc
  • The second kind
  • / start + The way of wildcards
  • As long as it conforms to the directory structure , Don't worry about the ending
  • http://localhost:8080/demo2/cpc/suibianxie
  • The third kind of
  • wildcard + Fixed format ending way
  • As long as it conforms to the fixed ending format , Regardless of the path ahead
  • http://localhost:8080/demo2/tjtjtj.cpc
  • Be careful : Priority questions
  • The more specific the priority, the higher , The more fuzzy it is, the lower the priority it has
  • The first one is > The second kind > The third kind of
  • Servlet Multipath mapping

  • You can give Servlet Configure multiple access maps , So different functions can be realized according to different request paths
  • Scene analysis :
  • If the access resource path is /vip
  • The price of the goods hit 9 fold
  • If the access resource path is /svip
  • The price of the goods hit 5 fold
  • If the accessed resource path is another
  • There is no discount on the price of goods
  • Code implementation
  • getRequestURI() Method
  • getRequestURI() Method returns a string , The content is the whole access url Of path Content , No query;
  • example : Input url The address is http://localhost:8080/testproject/test
  • getRequestURI() return /testproject/test, For one String
  • To configure
  • Use the second
  • / start + The way of wildcards
  • Code
  • test
  • http://localhost:8080/demo2/cpc/vip
  • http://localhost:8080/demo2/cpc/svip
  • http://localhost:8080/demo2/cpc
  • Servlet Create time

  • Create... On first visit
  • advantage : Reduce the waste of server memory ; Improve the efficiency of server startup
  • disadvantages : If there are some initialization operations to be done when the application is loaded , Cannot complete
  • Created when the server loads
  • advantage : Create objects ahead of time , Improve the efficiency of first time execution ; You can complete some initialization operations when loading applications
  • disadvantages : It takes up a lot of server memory , It affects the efficiency of server startup
  • To be modified Servlet Create a time :
  • Will be in <servlet> In the label , add to <load-on-startup> label
  • Write an integer
  • A positive integer represents the number created when the server loads , The smaller the value. , The higher the priority
  • A negative integer or no write represents the first access to create
  • Default Servlet

  • Default Servlet It's one provided by the server Servlet
  • It's configured in Tomcat Of conf In the directory web.xml in
  • Its mapping path is <url-pattern>/<url-pattern>
  • When we send a request , First of all, it will be in our project web.xml Find mapping configuration in , If found, execute
  • But when we can't find the corresponding Servlet When the path
  • Just go to the default Servlet, By default Servlet Handle
  • therefore , Everything is Servlet
原网站

版权声明
本文为[Soup key TJ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240821048941.html