Learning Source: xml learning materials dark horse Java57 part of JavaWeb course.
Tomcat: Open Source software running dynamic resources.
Servlet: is an interface that defines the rules for Java to be accessed by a browser (Tomcat can be identified) of.
* 快速入门:
1. 创建JavaEE项目
2. 定义一个类,实现Servlet接口
* public class ServletDemo1 implements Servlet
3. 实现接口中的抽象方法
4. 配置Servlet
在web.xml中配置:
<!--配置Servlet -->
<servlet>
<servlet-name>demo1</servlet-name>
<servlet-class>cn.itcast.web.servlet.ServletDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo1</servlet-name>
<url-pattern>/demo1</url-pattern>
</servlet-mapping>
- The implementation of the principle:
- Resource Path When the server receives the request of the client browser will parse the request URL path to get access to the Servlet
- Find web.xml file, whether or not there is a corresponding tag body content.
- If so, find the full class name corresponding
- tomcat will bytecode file is loaded into memory, and creates its objects
- Calling its methods
With more implementation class HttpServlet is
0. request mode determination
- Obtain data
HTTP:
-
The concept: Hyper Text Transfer Protocol Hypertext Transfer Protocol
-
Transfer Protocol: define, when the client and server communications, data transmission format
-
Features:
- TCP / IP based high-level agreement
- The default port number: 80
- Based on a request / response model: Response time corresponding to the first request
- Stateless: requests each independent of each other, the data can not interact
-
historic version:
- 1.0: in response to each request to establish a new connection will
- 1.1: Multiple Access
-
-
A data request message format
-
The request line
request method request url request protocol / version
GET /login.html HTTP / 1.1- Request mode:
- 7 HTTP protocol has requested embodiment, there are two commonly used
- GET:
- Request parameters in the request line, after the url.
- url requests limited length
- Less secure
- POST:
- Request parameters in the request body
- url is not limited in the length of the request
- Relatively safe
- GET:
- 7 HTTP protocol has requested embodiment, there are two commonly used
- Request mode:
-
Request header: the client browser to tell some information about the server
request header name: request header value- Common request headers:
-
User-Agent: browser tells the server, I access the browser version you are using information
- You can get information on the server side of the head, resolve browser compatibility issues
-
Referer:http://localhost/login.html
- Tells the server, I (the current request) come from?
- effect:
- Security chain:
- quantity survey job:
- effect:
- Tells the server, I (the current request) come from?
-
- Common request headers:
-
Request blank lines
blank lines, that is, for dividing the request header POST request, and the request body. -
Request body (body):
- POST request message encapsulating a parameter request
- 字符串格式:
请求行:POST /login.html HTTP/1.1
请求头:Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: http://localhost/login.html
Connection: keep-alive
Upgrade-Insecure-Requests: 1
-
Request body: username = zhangsan (POST only)
the Request
-
Principle of the request and response objects
- request and response objects are created by the server. We use them
- request object is to get request message, response message in response to the object is set to
-
request object inherits architecture:
ServletRequest - Interface
| inherit
HttpServletRequest - Interface
| achieve
org.apache.catalina.connector.RequestFacade class (tomcat) -
request features:
- A data acquisition request message
-
Line data acquisition request
- GET /day14/demo1?name=zhangsan HTTP/1.1
- method:
-
Acquisition request method: GET
- String getMethod()
-
(*) Gets the virtual directory: / day14
- String getContextPath()
-
Get Servlet Path: / demo1
- String getServletPath()
-
Get request parameter acquisition mode: name = zhangsan
- String getQueryString()
-
(*) Acquisition request URI: / day14 / demo1
-
String getRequestURI(): /day14/demo1
-
StringBuffer getRequestURL() :http://localhost/day14/demo1
-
URL: Uniform Resource Locator: http: // localhost / day14 / demo1 People's Republic of China
-
URI: Uniform Resource Identifier: / day14 / demo1 Republic
-
-
Acquisition protocol and version: HTTP / 1.1
- String getProtocol()
-
Obtain the IP address of the client:
- String getRemoteAddr()
-
-
Data acquisition request header
- method:
- (*) String getHeader (String name): Gets the value of the request header request header by name
- Enumeration getHeaderNames (): Get all request header name
- method:
-
Volume data acquisition request:
- Request POST request parameter only mode, only the request body, encapsulating the POST request in the request body: the request body
- step:
-
Get a stream object
- BufferedReader getReader (): Get a character input stream, only operation of the character data
- ServletInputStream getInputStream (): Get the input byte stream, you can operate all types of data
- After the file is uploaded to explain the knowledge points
-
Then take data from the stream object
-
-
- A data acquisition request message