Overview of Servlets
What is a servlet? ;
The servlet is a small Java program on the server side, receiving and corresponding requests sent from the client.
The role of the servlet:
Processes requests from the client and responds appropriately to the request.
Using Servlets: (a simple case of a servlet: Getting started)
* Write a class to implement the Servlet interface.
public class servletTest01 implements servlet{
@Override
public void Service (ServletRequest request, servletresponse response) throws Servletexception, ioexception{
System.out.println ("");
Response.getwriter (). println ("Hello Servlet ...");
}
}
"Configure Servlet" to the Web server. (Configured in Web. XML under the Web-inf directory)
if created, the servlet file Web project is created automatically configured,
<!--configuration servlet-->
<servlet>
<!--name of the servlet--- (can be customized)
<servlet-name>servlettest01</servlet-name>
<servlet-class> directory of files (you can right-copy the path name in the class name) </servlet-class>
</servlet>
<!--Configure the servlet mapping-->
< Servlet-mapping>
<!--servlet name--> (must be consistent with previous)
<servlet-name>servlettest01 </servlet-name>
<url-pattern>/servletTest01</url-pattern> (can be customized but skip to page must fill in this name)
</ Serlet-mapping>
Application of "Servlet" (Login to other similarities)
Analysis: 1, design a login page.
2. Click the login button on the login page to submit to the servlet
3. Write the servlet to receive parameters in the servlet.
4, call the business layer to the database for query operations. ( servlet-->servlce Layer--->DAO layer (query database returns data using Utils package)) (domain layer).
5, according to the processing results to make corresponding.
Code implementation Ideas:
Preparation: 1. Introduce a jar package (mysql,c3p0,dbutils, etc.) and create a database.
2. Creating Packages and Classes
3. Create a tool class Jdbcutils
4, write Servlet,service,dao.
"The life cycle of the servlet" (more important)
Life cycle:
Refers to the process from creation to destruction of an object.
The life cycle of a servlet: the process of creating a Servlet object from creation to destruction.
When a client accesses the servlet for the first time to create a Servlet object, the Init method in Serlet executes.
Any time a request is sent from the client, the server creates a new thread to execute the service method in the servlet for this request.
The internal method of the service method calls different Doxxxxx methods depending on the request, and the Servlet object is destroyed when the servlet removes it from the server or shuts down the server.
The Destroy method will be executed.
Path issues in Web development (important)
Relative path: Not the/start path.
*localhost:8080/Project name/file name (Servlet,. html)
Absolute path:
Usually the/start path.
Path with project name (the path of the client):
Path without project name (server-side path):
"Page five seconds Jump eg: code implementation" To achieve the purpose: the site after the successful landing, 5 seconds after the jump to a successful page.
Analysis: First in the code after the successful login to set the response header and then in writing a successful post-jump page.
Troubleshoot code-garbled problem responses
Response.setcontenttype ("Text/html;charset=utf-8");
Response. Writer (). println ("Response.setheader ("Refresh", "url= page path to jump");
}
}
Step 2:
Write Jump Time countdown in page Hxml file; 5, 4, 3, 2, 1< technology > Label Settings Header info <meta> tags
<! DOCTYPE html>
<meat charset= "Utf-8" >
<meat http-equiv= "Refresh" content= "5; Url= login successful. html >
<script type= "Text/javascript" src= "/web/js/jquery-1.11.3.min.js" ></script>
<title>insert title here<title>
<script type= "Text/javascript" >
$ (function () {
SetInterval ("Changetime ()", 1000);
})
var i = 5;
function Changetime () {
Get the element with ID s1;
i--;
$ ("#s1"). HTML (1);
}
</script>
</body>
"ServletConfig" (understand) (get initialization parameters)
"ServletContext" (all of the content ServletContext in the servlet is known.) (shared by multiple users) "domain Object"
The role and use of ServletContext;Save global information and data:
Number of visits to the website:
Chat Room:
Overview (Domain object):
ServletContext: A web App has only one ServletContext object. When the server starts, the server creates a ServletContext object for each Web project that belongs to its own project.
the server shuts down or the item is removed from the server ServletContext will not be destroyed. If you save the value in ServletContext. The value has a range of functions. So this object is called a "domain object".
We can use this object to access the data, the data accessed by this object can be obtained throughout the Web application.
You can access the data using the following methods (domain object-related methods)
The purpose of the ServletContext object is to share data between dynamic resources throughout the Web application!
For example, in Aservlet, you save a value to the ServletContext object, and then you can get the value in Bservlet, which is the shared data.
Method Name: Description:
SetAttribute (String name, Object object) stores data to ServletContext
GetAttribute (String name) fetching data from ServletContext
RemoveAttribute (name) to remove data from ServletContext
How to get Sevletcontext objects
ServletConfig # getservletcontext ();
Genericservlet # getservletcontext ();
HttpServlet # getservletcontext ();
HttpSession # Getservletcontext (); "Here will be introduced in the session"
Servletcontextevent # Getservletcontext () "Not much to introduce here"
Get the ServletContext object in the servlet:
in void init (servletconfig config): ServletContext context= config.getservletcontext ();
The Getservletcontext () method of the ServletConfig class can be used to obtain the ServletContext object;
Get ServletContext object in Genericeservlet or HttpServlet: "Not used"
ServletContext; Usage API
1. Get the MIME type of the file. --File upload and download.
String type GetMimeType (string file)
2. Get Global Initialization parameters:
3. Accessing data as a Domain object: (* common *)
void type SetAttribute (String name, Object object) stores data to ServletContext
void type getattribute (String name) fetching data from ServletContext
Object type RemoveAttribute (name)
4. Read the file for the Web project: (* *)
InputStream type getResourceAsStream (String path)
String type Getrealpath (string path)
Servlet Common Technology web