Servlet Learning
1. The relationship between Servlet and JSP
2. servlet's declaration cycle
3. How to use a simple servlet
What is a servlet? What is JSP?
A program that inherits some of the classes of Javax.servlet and has the features of a servlet
But the servlet's visibility is not very good, JSP can compensate for this, JSP is Java Server pagers, is a Java as a script language, in the Web server can be converted to servlet, in the traditional HTML Web page using <% Java Language%> can embed Java code.
A servlet is a server-side applet that can be used to augment a Web server in a variety of ways.
PS: This statement tells us:
1. Servlet is a Java-written program
2. The servlet is run on the server side, not the client
3. The ultimate goal of the servlet is to get the server to do things for us.
The relationship between Servlet and JSP:
The same point: the JSP will eventually turn into a servlet
Different points:
1 servlet is persistent and requires only a Web server to load once
2 Servlets are extensible because Java is object-oriented
3 JSP is a technique developed to solve programming difficulties in Servlets, JSP is a scripting language, servlet is the Java language
4 The servlet must be compiled before it can be
5 at the first run, the JSP needs to be converted to a servlet and then compiled before it can run. And the servlet compiles directly. (The first time that there is no identical file in the Web container)
6 Servel Write business logic is very powerful. JSP write view layer works well
Servlet life cycle:
A Servlet is a subclass of Javax.Servlet.httpServlet.
1 Load servlet
2 Call the constructor to instantiate a Serlet object whose name has <servelt-name/> specified
3 Calling the Init () method
4 Services Service ()
5 Uninstalling Destoory ()
Demo:
Directory structure:
Xml
----------------------------Test1.java----------------------------------------------------------
Package Com.controller;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/**
* Servlet Implementation Class Test1
* Test Declaration Period
*/
@WebServlet ("/test1")
public class Test1 extends HttpServlet {
Private static final long serialversionuid = 1L;
/**
* @see Httpservlet#httpservlet ()
*/
Public Test1 () {
Super ();
SYSTEM.OUT.PRINTLN ("----------------construction----------------");
TODO auto-generated Constructor stub
}
/**
* @see Servlet#init (servletconfig)
*/
public void init (ServletConfig config) throws servletexception {
TODO auto-generated Method Stub
System.out.println ("---------init------------");
System.out.println (This.getclass ());
}
/**
* @see Servlet#destroy ()
*/
public void Destroy () {
Super.destroy ();
System.out.println ("----------------destroy-----------");
TODO auto-generated Method Stub
}
/**
* @see Httpservlet#service (httpservletrequest request, httpservletresponse response)
*/
protected void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("------Service---------------");
}
/**
* @see Httpservlet#doget (httpservletrequest request, httpservletresponse response)
*/
protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
TODO auto-generated Method Stub
DoPost (request, response);
System.out.println ("---------------Default call Doget ()------------");
}
/**
* @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response)
*/
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
TODO auto-generated Method Stub
PrintWriter out = Response.getwriter ();
Out.println ("--------------------------------------------of written content");
Out.flush ();
Out.close ();
}
}
index.jsp
:
PS: Please note the change of the address bar
This time found no call to Doget () method
The reason: So wrote:
Discover or not >>>>>>>>>>>>>>> then you find that the service method you wrote on this class is written like this:
Right! Is the way to overwrite the parent class, so that the servlet service () will only run SYSO.
This should be changed to:
This is what this class is made of:
Package Com.controller;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import Javax.servlet.ServletConfig;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/**
* Servlet Implementation class TEST1
* Test Declaration period
*/
@WebServlet ("/test1")
public class Test1 Extends HttpServlet {
private static final long serialversionuid = 1L;
/**
* @see httpservlet# HttpServlet ()
*/
public Test1 () {
super ();
System.out.println ("-------- --------Construction----------------");
//TODO auto-generated constructor stub
}
/**
* @see Servlet#init (servletconfig)
*/
public void init (ServletConfig config) throws servletexception {
TODO auto-generated Method Stub
System.out.println ("---------init------------");
System.out.println (This.getclass ());
}
/**
* @see Servlet#destroy ()
*/
public void Destroy () {
Super.destroy ();
System.out.println ("----------------destroy-----------");
TODO auto-generated Method Stub
}
/**
* @see httpservlet#service (httpservletrequest request, HttpServletResponse response)
*/
protected void Service ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {
/TODO auto-generated method stub
Super.service (request, response);
System.out.println ("------Service---------------");
}
/**
* @see httpservlet#doget (httpservletrequest request, HttpServletResponse response)
*/
protected void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {
/TODO auto-generated method stub
DoPost (request, response);
System.out.println ("---------------doget ()------------");
}
/**
* @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response)
*/
protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
TODO auto-generated Method Stub
PrintWriter out = Response.getwriter ();
Out.println ("-------------------content-------------------------");
Out.flush ();
Out.close ();
}
}
---------------------------------------------------
The effect is:
After the boot
Show
The background output information is:
After clicking Connect:
Console output information is
Servlet Review 1: The use of a simple servlet