How to develop a servlet
The mapping path of the servlet
servlet Default Path
The life cycle of Sevlet
Automatic loading of Servlets
The Init method with parameters and the Init method without parameters
Multithreading concurrency problems for servlets
ServletConfig Object
ServletContext Object
1. How to develop a servlet
Steps:
1) Write Java class, inherit from Javax.servlet.http.HttpServlet class
2) Overwrite Doget and Dopost methods
3) servlet program to the Tomcat server to run!!
3.1) Copy the servlet program's class byte-code file to the Web-inf/classes directory
3.2) Configure in the Web. xml file
Example: Myfirstservlet.java
Package Com.rk.http.a_howtouse;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class Myfirstservlet extends httpservlet{@Overrideprotected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioexception{response.getwriter () . Write ("This is my first Servelt");}}
Example: section of configuration servlet in Web. xml
<!--Configure a servlet--<servlet> <!--servlet configuration--<servlet-name>myfirstservelet</ servlet-name><!--The internal name of the servlet, custom. Try to make sense--<servlet-class>com.rk.http.a_howtouse. myfirstservlet</servlet-class><!--servlet class full Name: Package name + Simple class name--</servlet> <servlet-mapping> <!--servlet mapping configuration-<servlet-name>MyFirstServelet</servlet-name><!--servlet internal name, Be sure to keep up with the internal name above!! -<url-pattern>/first</url-pattern><!--The servlet's mapping path (the name of the access servlet)--</servlet-mapping >
Example: Full Web. xml file
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "/http Java.sun.com/xml/ns/javaee " xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation = "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" > <display-name></display-name> <!-- Configure a servlet --> <servlet> <!-- servlet Configuration --> <servlet-name>myfirstservelet </servlet-name><!--The internal name of the servlet, custom. Try to make sense --> <servlet-class>com.rk.http.a_howtouse. myfirstservlet</servlet-class><!-- servlet class full name: package name + Simple class name --> </servlet > <servlet-mapping>< mapping configuration for!-- servlet --> <servlet-name> myfirstservelet</servlet-name><!-- servlet internal name, must be consistent with the internal name above!! --> <url-pattern>/first</url-pattern><!-- servlet Mapping Path (the name of the access servlet) --> </servlet-mapping ></web-app>
Question: How do I find a resource based on the URL ("Http://localhost:8080/myweb/first")?
the process of finding resources through URLs
| URL Component |
function |
| Premise |
When the Tomcat server starts, it first loads the Web. XML configuration file for each WebApps in the application. |
| /HTTP |
HTTP protocol. The browser follows the HTTP protocol to send (request) the data |
| localhost |
To the local hosts file to find out if there is a corresponding IP address for the domain name, get 127.0.0.1 |
| 8080 |
Locate the Tomcat server |
| /myweb |
Find the directory of MyWeb in the WebApps directory of Tomcat |
| /first |
The resource name. 1) Find out if there is a matching url-pattern content (/first) in the MyWeb Web. xml 2) If a matching url-pattern is found, the servlet configuration with the name of the current servlet-name to query for the same name in the Web. xml file 3) If found, remove the Servlet-class content from the corresponding servlet configuration information: String: Com.rk.http.a_howtouse. Myfirstservlet By reflection: A) construct the Myfirstservlet object b) and then call Myfirstservlet inside the method |
2. servlet Mapping Path
<servlet-mapping> <servlet-name>MyFirstServelet</servlet-name> <url-pattern>/first</ url-pattern><!--This section discusses objects-</servlet-mapping>
exact matching and fuzzy matching of servlet mapping paths
| category |
Url-pattern |
Browser Input |
| exact horse With |
/first |
http://localhost:8080/myweb/first |
| /rk/first |
http://localhost:8080/myweb/rk/first |
| blur match |
/* |
http://localhost:8080/myweb/any path |
| /rk/* |
http://localhost:8080/ Myweb/rk/arbitrary path |
| *. suffix name *.do *.action *.html (pseudo-static) |
http://localhost:8080/myweb/any path. Do |
Precautions:
1) Url-pattern either start with a/or start with a *. For example, the direct write "RK" (No Front "/" line) is an illegal path.
2) Two fuzzy matches cannot be used at the same time, for example/rk/*.do is an illegal path, and it is composed of two fuzzy matches of rk/* and *.do.
3) When there are multiple servlets that have input URLs that are matched at the same time: (Conflict handling)
3.1) Exact match priority. (Long most like, priority is matched)
3.2) After the end of the suffix of the fuzzy url-pattern priority lowest!!!
3. servlet Default Path
In%tomcat%/conf/web.xml, there is the following XML code:
<!-- The mapping for the default servlet --> <servlet-mapping> < Servlet-name>default</servlet-name> <url-pattern >/</url-pattern> </servlet-mapping> <servlet> < Servlet-name>default</servlet-name> <servlet-class >org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> < param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name >listings</param-name> < Param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
The default path for the servlet (/) is a path built into the Tomcat server. The path corresponds to a defaultservlet (the default servlet). The default servlet is used to parse a static resource file for a Web application.
Question: How does the URL input http://localhost:8080/myweb/index.html read the file?
1) Find out if there is a matching url-pattern to the Web. xml file under the current MyWeb application.
2) If there is no matching url-pattern, then give Tomcat the built-in defaultservlet processing
3) Defaultservlet The program to the root directory of the MyWeb application to find out if there is a static file named Index.html.
4) If the file is found, the contents of the file are read and returned to the browser.
5) If the file cannot be found, the 404 error page is returned.
Conclusion: We should find the dynamic resources and then find the static resources.
Servlet Programming: (1) How to use Servlets