Servlet Notes 1

One: Servlet technology (ServerLet)

①Servlet:

1. Servlet is a program (code, function implementation) on the server side, which can interactively process the client's request and respond to the result.

2. Dynamic web technology.

3. The foundation of JavaWeb development, an integral part of the JavaEE specification (a set of interfaces).

//provided by the server manufacturer

②The core role of Servlet:

I. Receive client requests and complete operation tasks.

II. Dynamically generate web pages (page data is variable).

III. Respond to the client with a dynamic web page containing the operation result.

 

 

③: Servlet development steps

1. Build the environment: Create an app (folder) in the server and improve the directory structure.

Configure the Servlet-related Jar package (servlet-api.jar) to the CLASSPATH.

2. Implement the javax.servlet.Servlet interface and cover 5 abstract methods.

 

//GetServletConfig() returns the ServletConfig object to return initialization parameters and ServletContext. The ServletContext interface provides environment information about the servlet.

//GetServletInfo() is an optional method that provides information about the servlet, such as: author, version, copyright.

3. Implement the core method service to provide "service content" (a piece of logic code).

4. Place the compiled .class file in WEB-INF/classes.

5. Add configuration information to the web.xml file

 

④: Utility class: javax.servlet.http.HttpServlet, a class dedicated to serving the HTTP protocol, (indirectly) implementing the Servlet interface

How TomCat handles servlets

 

 

Generation of dynamic web pages

 

 

 

Two: Servlet life cycle

1. The creation time of the Servlet object:

I. On first access, create an object (singleton pattern).

1). The same servlet is requested multiple times, and the same object is accessed.

2). Servlet作为单例对象,实例变量为临界资源,线程不安全。如迫不得已,加锁。

 

II. 启动服务器时,创建对象。

 

2. Servlet的初始化:伴随对象的首次创建(只执行一次),自动调用init()方法,完成必须的初始化行为(逻辑代码)。

 

3. Servlet的销毁:服务器停止服务时,会调用destroy()。


三:Web访问的三种形式

1:Form表单

 

2:地址栏

 

3:超链接

 

 

四:Servlet获取Client的请求参数

 

1. 获得请求中的参数:

I. request代表一次请求对象,包含一次请求中的所有信息。

II. request.getParameter("表单元素的name属性 / URL拼接的key");

 

2. 向服务器发送请求、传递数据的方法(get、post)的乱码:

I. get请求: (地址栏传递文字乱码)

修改TomCats service.xml配置文件

 

 

II. post请求:(设置解码方式)

 


III.响应 :(设置响应的编码方式)

 

 

 

3. Get/Post的区别:

I. get:通过地址栏(String Parameter),明文数据传递,数据量小,不安全。

II. post:通过请求体(Form Data),密文数据传递,数据量大,安全。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325432626&siteId=291194637