Servlt entry
Both architectures of java
C / S (Client / Server) architecture communications high efficiency and safety, but the system takes more than
B / S (Browser / Server) architecture development cost savings
C / S (Client / Server) architecture communications high efficiency and safety, but the system takes more than
B / S (Browser / Server) architecture development cost savings
http protocol
http request
Request line request header request body
Request line comprising : request type Path Protocol (HTTP1.1)
get no request body, splice content directly in the request header
http response
Status line, the first response, the response body
Status line comprising : protocol response code (200) in response to instructions (OK / notfund)
Referer request header address field value of an input
Location response header field is used to jump page
Refresh field is used to automatically jump, html page meta tags to achieve
<meta http-equiv=” Refresh” content=”3”;url=”地址”>
servlet implementation
1 Create a web project
Creating Dynamic Web project project
Set the project name to select Dynamic web module version 2.5 version
After 2 arranged next context root (external access name) (name of an external access does not require front /)
NOTE: Modify the name of the access method for external projects:
右键Properties->Web Project Settings->Context root
2 achieve servlet achieve
1 Create a class file , class files inherit HttpServlet class , override the service method
@Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ...... handling code } |
2 the Servlet class implementation class unowned method , call server according to
3 Set webContent \ web-INF directory web.xml configuration file (web-app inside the tag )
<servlet>
<Servlet-name> resource name </ servlet-name>
<servlet-class>class类路径</servlet-class>
</servlet>
<servlet-mapping>
<Servlet-name> resource name supra </ servlet-name>
<Url-pattern> / Resources External Access Name </ url-pattern>
</servlet-mapping>
Note:
1 <url-pattern> to add the value of the foregoing "/"
2, if there are a plurality of different resource name <servlet-mapping> the same point <servlet>
3 <servlet> tag <servlet-name> can not be repeated
4 <url-pattern> to access external resource name must be unique
5 Modify the xml need to restart the server
6 url-pattern may be provided a plurality of access paths
"*" Wildcard to match all
Only on the front or in the back, it can not be used alone, not in the middle, the letter can not be spliced, and
The more accurate, the higher the priority
/ Path / *: as long as the path visits a "path", can be accessed
* Suffix: as long as the path is specified ending suffix, can access
/ *: Any path, can access
7 <servlet> tag set start priority servlet
<Load-on-startup> 3 </ load-on-startup> value indicating higher priority, perform
8 <servlet> tag set initialization parameters (may be provided a plurality of <init-param>, structure of key-value)
<init-param>
<param-name>encode</param-name>
<param-value>UTF-8</param-value>
</init-param>
9 with a <servlet-mapping> may be provided a plurality of <url-pattern> path
<! - a plurality of paths ->
<url-pattern>/ser005</url-pattern>
<! - access to the specified directory ->
<url-pattern>/test/*</url-pattern>
<! - end with the specified path suffix ->
<url-pattern>*.do</url-pattern>
<-! Access all paths ->
<url-pattern>/*</url-pattern>
servlet implementation class
1 inherited HttpServlet class , override the service method
2 implement the interface Servlet
Rewrite all interface methods
init () code executed initialization (initialization is executed only once)
destroy () (executed once when destruction) method to execute when the destruction of the servlet
getServletConfig () configuration information
getServletInfo () server version information
service () function code execution
3 GenericServlet class inheritance
Override service () method
4 associated with the plurality of classes implemented
(HttpServlet) extends (GenericServlet) implements (Servlet)
3 external servlet access
External Access : HTTP: // domain name ( hostname, ip, custom name): port / Project Access name / resource access name