[Lehr] Servlet Basics Study Notes

What is Servlet

Servlet is a small program (Server applet) running (such as tomcat) on the application server, is mainly used to respond Http request. Broadly speaking, it is a Java class that implements the Servlet interface can handle Http request.

How to register Servlet

Registration Servlet Servlet path mapping is actually configured so that the server can directly find the Servlet in time of the visit.

Sign up configuration files

Open web.xml
wrote these:

 <servlet>
	 <servlet-name>ShowServlet</servlet-name>		//起个名字,下面好对应
	 <servlet-class>Servlet.ShowServlet</servlet-class>	//它所在的具体的类,包名类名写完
</servlet>

Configured to map the path:

<servlet-mapping>
    <servlet-name>ShowServlet</servlet-name>		//名字要和上面一样
    <url-pattern>/Servlet/ShowServlet</url-pattern>		//配置的这个Servlet访问的路径
</servlet-mapping>					//现在直接输入ShowServlet这个名字就会按照路径去找了

In a row can be <servlet></servlet>written in a plurality, the plurality may be used <servlet></servlet>
to write each one inside, which can be more easily disposed of other properties (such as priority load)

Notes Registration

Start, write something on top in the corresponding Servlet file needs to be registered after JavaEE6 (Tomcat7, Servlet3.0).

/**
 * Servlet implementation class ShowAllServlet
 */  (这一团可以不用写,重点是下面那句)
@WebServlet("/ShowAllServlet")

Servlet life cycle and operational processes

Borrowing a map on imooc:
Here Insert Picture Description
Servlet life cycle is divided into the following steps:
Load -> instantiation -> Initialization -> Services -> destruction

Load -> instantiation -> Initialization

At the beginning, servlet instance to determine whether there is, if not, it loads the servlet class and create an instance (constructor call), here calls the init () method. init () method will call this time! ! !

service

The client sends a request to the server, the server to find the corresponding Servlet method. If the Servlet already exists, the server calls the Servlet, direct call service method (that does not exist, then went to load). The method of automatically transmits the server will service a request and a response object, this is provided by the server tomcat packaged), and then generates a response content to the server and then back to the client.

service approach is both POST and GET methods, specific request to see what kind of method.
Get way request Servlet ------> hyperlinks.
For example
<a href="HelloServlet">
to follow this hyperlink to <servlet-mapping>where to find the corresponding to url, then to above by a name corresponding <servlet>inside to find the same name, can be to locate the target by category (servlet-class)
is then performed according to the request mode get way corresponding get methods (but generally get the default method and call the post method)
When writing Servlet doPost method, you need to throw ServletExcpetion and an IOException, does not throw an exception HttpServletException

destroy

If the server is shut down, it calls the destroy () method. (Looks only at the server shut down when they could destroy method is called)

Auto Reload

Servlet Servlet container automatically loaded at startup:
In the web.xml file
<servlet></servlet>is added between the <loadon-startup>1</loadon-startup>
higher numbers indicate higher priority
in fact should give each write a servlet <> <>
then put a name on the inside, class, and this automatic loading setting.

When a client for the first time to send the request Servlet
will automatically load

When the servlet is modified when the page is reloaded, because after the servlet instance will be stored in the memory, so to make changes to reload

Example: Tomcat servlet loaded three cases of

Constructor is executed --------------------> then the init method
(the constructor is executed is called instantiation)

Destroy the point is that you stop server when the server will call

Open a server will be able to see the System.out.println ();

Running instance of the compiled class is placed under the WEB-INF directory of classes, which is instantiated, so you should file here, if you modify the servlet after recompilation (instantiation)
that you save after modification revising the

The relationship between Servlet and Jsp built-in objects

JSP How to get in the servlet
out response.getWriter (in the strict sense, but they are not the type of match)
request (At the interface where the function is passed in the request) method method of service request is passed in, for example request.getSession
response Similarly
session request.geSession ();
apllication getServletContext(); ServletContext application=this.getServletContext();
exception Throwable
page this
pageContext PageContext
Config getServletConfig function

How to respond to the output page?
Get the front end of the brush

PrintWriter out = response.getWriter();

I began to write this page do not draw a direct jump
out.println("<h1>Hi Lehr!</h1>");

You like this output <h1>will be output, is useless because the text specifies the output type

response.setContentType(“text/html;charset=utf-8”);

The above process can be understood as a response time will return to write the output text directly on the page this white paper, but <h1>this is html language, so if you do not tell it directly as a string output went.

But here, if you are using System.out.println (); this is output in the background, which is the result of your compiler box instead of page

Published 33 original articles · won praise 26 · views 2619

Guess you like

Origin blog.csdn.net/qq_43948583/article/details/89819679
Recommended