Describe what a servlet is
A servlet is a Java class that implements a particular interface or parent class.
A servlet is a small program that runs on the server side. A servlet is a Java class and can be accessed through the request-response programming model, a servlet program that resides in server memory.
Tomcat Container LevelTomcat's container hierarchy is divided into four tiers, the servlet container manages the context container, and a context corresponds to a Web project. Engine is a container of engines. Host is a master container
Implement the servlet three scenarios:
01.Servlet Interface: 5 methods
Init () {
Initialization
}
Service () {
Processing requests
}
The mudslide destroyed the whole village.
Destory () {
Destroyed
}
Getservletconfig () {
Get servlet configuration information
}
Getservletinfo () {
Get information about Servlets, such as version authors.
}
5 methods: Init (): initialized, only executed once
Destory (): Tomcat executes when it is closed, frees resources, executes once
Service (): Handles client requests and responds to client requests
Getservletconfig (): Get Configuration
Getservletinfo (): Version and other information
02. Implement Genericservlet Abstract class
Modify the Servlet class to restart the server, and modify the JSP page can not restart
03. Implement HttpServlet Abstract class
Service (): Scheduling role
If our own Servlet class inherits the HttpServlet abstract class, then there is no need to rewrite the service () of the parent class, and the service () method is just a dispatch function.
Doxxx:dopost (httpservletrequest request,httpservletresponse response) doget ()
steps to manually write a servlet program1. Inherit HttpServlet 2. Rewrite doget () or Dopost () Method 3. Registering a servlet in Web. xml
- To register a servlet using a common method
<servlet>
<!--servlet的名字-->
<servlet-name>MyServlet</servlet-name>
<!--servlet类的名字(包括完整包名)-->
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<!--要与上面的那个名字一一对应-->
<servlet-name>MyServlet</servlet-name>
<!--表示该servlet的访问路径 /表示项目的根目录-->
<url-pattern>/servlet/MyServlet</url-pattern>
</servlet-mapping>
- JavaEE6 and above can use annotations to register a servlet
Name is required, urlpatterns or value for the specified URL to access the address, or to define multiple URL address access
@WebServlet(name = "MyFirstServlet",urlPatterns = "/servlet/MyFirstServlet")
@WebServlet has a number of properties:
asyncsupported |
Declares whether the servlet supports asynchronous operation mode |
Description |
Description of the servlet |
DisplayName |
Display name of the servlet |
InitParams |
The init parameter of the servlet |
Name |
Name of the servlet |
Urlpatterns |
Access URL for the servlet |
Value |
Access URL for the servlet |
How the servlet gets the session object and gives the inside set data.
Parsing: Request.getsession (). SetAttribute (name, value)
Init () and destory () and service () execution times issues
Important: Init () and Destory () in the servlet are executed only once, and each time the client accesses the corresponding Servlet class, the service () is invoked once
the life cycle of the servlet1. Initialize phase, call Init () Method 2. In response to the client request phase, call the servlet () method. The servlet () method chooses to execute the doget () or Dopost () method according to the submission method. 3. Termination phase, call Destroy () method the servlet life cycle phase includes initialization, loading, instantiation, service, and destruction. At the following time, the Servlet container is installed in some servlets automatically when the Servlet:1.servlet container is started, so it only needs to add the following code between <Servlet></Servlet> in the Web. xml file: The smaller the <load-on-startup>1</load-on-startup> number, the higher the priority. 2. After the servlet container is started, the client sends a request to the servlet for the first time. After the 3.servlet class file is updated, remount the servlet. After the servlet is loaded, the servlet container creates a servlet instance and invokes the servlet's init () method for initialization. The init () method is called only once throughout the servlet's life cycle.
Ervlet with nine large built-in objectsHow to get the nine built-in objects for JSPs in a servlet
JSP Object |
How to get |
Out |
Response.getwriter methods (types are inconsistent, but functionally consistent) |
Request |
Parameters in the service method |
Response |
Parameters in the service method |
Session |
Request.getsession () method |
Application |
Getservletcontext () method |
exception |
Throwable |
Page |
This |
PageContext |
PageContext |
Config |
Getservletconfig method |
how to get values in a JSP form from a servlet and how to send values to a JSPGet the values from the JSP form from the servlet:
//获取单个值的时候
String context=request.getParameter("username");
//获取复选框等多个选址的时候
String[] parameterValues = request.getParameterValues("username");
The servlet sends the value to the JSP to store the content in the session and sends the content to the JSP sample through request forwarding:
// 将内容存储到session中
request.getSession().setAttribute("a","object");
// 通过请求转发将其发送给jsp页面
request.getRequestDispatcher("index.jsp").forward(request,response);
servlet path JumpAbsolute path: Calculates "/" from the root to indicate the root relative path of the server: the mapping address of the servlet in the XML is calculated from relative to the current resource must begin with "/" when requesting redirection is used in the servlet. You can use the Request.getcontextpath method to get the context object after the target JSP name in the servlet using the server does not jump, you can use "/" to indicate the project's root directory
Get initialization ParametersWhen you configure a servlet in Web. XML, you can configure some initialization parameters. In the servlet, these parameters can be obtained through the methods provided by the ServletConfig interface. Set initialization parameters in the servlet tag of XML
<servlet>
<servlet-name>GetInitParameterServlet</servlet-name>
<servlet-class>servlet.GetInitParameterServlet</servlet-class>
<!--设置初始化参数-->
<init-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>GetInitParameterServlet</servlet-name>
<url-pattern>/servlet/GetInitParameterServlet</url-pattern>
</servlet-mapping>
Overriding the Init () method in a servlet gets the Getinitparameter method by using the
@Override
public void init() throws ServletException {
String username = this.getInitParameter("username");
this.setUsername(username);
}
JSP servlet Basics