Java learning notes-Servlet configuration and thread security (12), learning notes servlet
Question 1:
Can users use get and post to send requests?
Developers do not know what request method the user wants to use. Therefore, they need to rewrite doGet or doPost. However, the response data is the same. Therefore, you can directly call one method on the basis of implementing one method.
Public class Template extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// process user requests} public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
Question 2: How can I modify the template for creating resources such as servlet and jsp in IDE?
Find the following jar package com. Genuitec. eclipse. wizards_7.0.0.zmyeclipse70020081206.jarServlet.java in the genuitec \ Common \ plugins directory of the IDE installation directory and modify the file.
Servlet Configuration
If you need to use a browser to access the compiled Servlet, You need to configure it. Web. xml
One-to-one configuration (basic configuration)
<Servlet> Configure a servlet <servlet-name> helloservlet </servlet-name> to specify the servlet name (any) to specify the fully qualified name (package name) of the specific servlet class. class Name) <servlet-class> cn. itcast. servlets. helloServlet </servlet-class> </servlet> <servlet-mapping> maps a servlet <servlet-name> helloservlet </servlet-name> servlet name to be mapped <url-pattern >/helloservlet </url-pattern> path to be accessed by the browser </servlet-mapping>
2 one-to-multiple configurations (basic configuration)
<servlet-mapping> <servlet-name>Mapping1</servlet-name> <url-pattern>/map1</url-pattern> <url-pattern>/map2</url-pattern> </servlet-mapping>
3 use * For ing
DTD * 0 to multiple Xpath // * indicates all elements
* Any matching characters in the servlet
<servlet-mapping> <servlet-name>Mapping2</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping>
Principle "if multiple servlets match the user's request resources, the tomcat server will respond to the most similar one !"
4 *. do ing
<servlet-mapping> <servlet-name>Mapping2</servlet-name> <url-pattern>*.do</url-pattern></servlet-mapping>
If you want to map to *. do, do not start with/, and *. do has the lowest priority.
Think: if the user's request resource ends with ".html" at the end of the Internet access time, is it a static page?
Because the servlet can be mapped to any path, the access path you see may be false.
5 default ing
Users are likely to access resources that do not exist in some websites. If you directly use the default servlet on the server for processing, the errors are unfriendly to users, all developed websites should have default servlets for handling user error requests.
<servlet-mapping> <servlet-name>MyDefault</servlet-name> <url-pattern>/</url-pattern></servlet-mapping>
6. self-starting servlet
Before a website is accessed by others, you should establish the website data in advance.
<servlet> <servlet-name>AutoStart</servlet-name> <servlet-class>cn.itcast.servlets.AutoStart</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
The load-on-startup value must be a positive integer. The smaller the value, the more advanced the startup. In actual development, we usually write database creation, table creation, and data addition in a self-starting servlet to improve the user experience.
Servlet thread security
Q: How can I use the following code to demonstrate servlet thread security?
Public class MultiThreadQuestion extends HttpServlet {public int count = 0; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1. verify that the server processes user requests in a thread. try {this. count ++; Thread. sleep (5*1000);} catch (InterruptedException e) {e. printStackTrace ();} response. getOutputStream (). write ("Thread name:" + Thread. currentThread (). getName () + "count =" + this. count ). getBytes ());}}
At the same time, access to the above Code finds three times at the same time, which is the problem of concurrent access to the thread.
Solution: Synchronization
Lock the code used to access shared resources.
Public class MultiThreadQuestion extends HttpServlet {public int count = 0; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 1. the verification server processes the synchronized (this) {try {this. count ++; Thread. sleep (5*1000);} catch (InterruptedException e) {e. printStackTrace ();} response. getOutputStream (). write ("Thread name:" + Thread. currentThread (). getName () + "count =" + this. count ). getBytes ());}}}
Summary:
ArrayList Vector HashSet
If you need a collection that is not thread-safe, you can use Collections for thread-safe conversion.