Servlet review 1: use of a simple Servlet, servlet review 1

Source: Internet
Author: User

Servlet review 1: use of a simple Servlet, servlet review 1

Servlet Learning

1. Relationship between Servlet and JSP

2. Servlet declaration cycle

3. How to use a simple Servlet

What is Servlet? What is JSP?

It inherits some class programs of javax. servlet and has the characteristics of Servlet.

However, Servlet visibility is not very good. JSP can make up for this. jsp is a Java Server Pagers, a language that uses java as a script and can be converted to Servlet on the web Server, using <% java language %> in traditional HTML web pages, you can embed java code.

Servlet is a server-side applet that can be used to expand a Web server in multiple ways.

PS: This sentence tells us:

1. Servlet is a java-written Program

2. Servlet is running on the server, not the Client

3. The ultimate goal of Servlet writing is to let the server help us do things

Relationship between Servlet and JSP:
Similarities: JSP will eventually be converted into a Servlet

Differences:

1. The Servlet is persistent and only needs to be loaded once by the Web server.

2 Servlet is extensible Because java is object-oriented

3 JSP is the development technology to solve the programming difficulties in Servlet. jsp is the scripting language and Servlet is the java language.

4. The Servlet can only be compiled.

5. During the first run, JSP needs to be converted to Servlet and then compiled before it can be run. The Servlet directly compiles it. (the first time is 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 lifecycle:

Servlet is a subclass of javax. Servlet. httpServlet.

1. Load Servlet

2. Call the constructor to instantiate a Serlet object. The object name is specified by <servelt-name/>.

3 call the init () method

4 Service ()

5 uninstall destoory ()

Demo:

Directory structure:

Web. 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 cycle

*/

@ 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 ("--------------- by default, 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 ("------------------- writing content -------------------------");

Out. flush ();

Out. close ();

}

}

Index. jsp

:

PS: Pay attention to the changes in the address bar

The doget () method is not called at this time.

The reason is: write it like this:

You can see that the >>>>>>>>>>>>>>> in this case, you find that the Service method on this class you write is written as follows:

Yes! The method of the parent class is overwritten. As a result, the Servlet Service () will only run Syso.

To do this, change:

This class is like this:

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 cycle
*/
@ 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 ();

}

}

 

 

---------------------------------------------------

Effect:

After startup

Display

Background output information:

 

After clicking CONNECT:

The console output information is

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.