Jsp/servlet Getting Started tutorial--servlet Getting started with

Source: Internet
Author: User

Now JSP books are some of the direct use of the JSP, and then explain the use of serverlet, there are books on the use of Serverlet, and then explain the use of JSP. I think the second kind of relatively good, as for the reasons we can learn to experience! So today we continue to learn the use of Serverlet!

Okay, cut the crap, start now!

What is a servlet?
①servlet is the Java class
②servlet is a class that inherits the HttpServlet class
③ This server-side operation to process client requests

Introduction to Servlet related packages
--javax.servlet.*: Store generic servlet classes unrelated to the HTTP protocol;
--javax.servlet.http.*: In addition to inheriting javax.servlet.*, it also adds functionality related to the HTTP protocol.
(Note: It is necessary to learn the HTTP protocol, as Web development will involve)
All servlets must implement the Javax.servlet.Servlet interface (Interface).
If the servlet program is not related to the HTTP protocol, then the Javax.servlet.GenericServlet class must be inherited;
If the servlet program is related to the HTTP protocol, then the Javax.servlet.http.HttpServlet class must be inherited.
--httpservlet: An abstract class was provided to create an HTTP Servlet.
Public void Doget () method:Used to handle a GET request made by a client
Public void DoPost () method:Used to process post requests
There are several ways to check the API Help files yourself.
--Javax.servlet the interface of the package:
ServletConfig Interface:
Used by the servlet container during initialization
ServletContext Interface:Defines a method that the servlet uses to obtain information from its container
ServletRequest Interface:Request information from the server
Servletresponse Interface:Responding to client requests
Filter Interface:
Class of--javax.servlet Package:
ServletInputStream class
: Used to read binary data from the client
Servletoutputstream class: Used to send binary data to the client
--javax.servlet.http the interface of the package:
HttpServletRequest Interface:
Provide HTTP request information
HttpServletResponse Interface:Provide HTTP response

Servlet life cycle
The--servlet life cycle refers to the time that exists and when the servlet instance was created, and when the entire process was destroyed.
The--servlet life cycle has three methods
init () method :
Service () method : Dispatches client requests to the protected method service
Destroy () method : Called by the servlet container to indicate to a servlet, the servlet is being taken out of servi Ce.
--servlet stages of the life cycle
---- instantiation : servlet container creation servlet instance
---- initialization : Calling the init () method
---- Service : If there is a request, call the service () method
---- Destroy : Call the Destroy () method before destroying the instance
---- garbage collection : destroying instances

The basic structure of the servlet

Package cn.dragon.servlet;

The following is the import of the appropriate package

Import java.io.IOException;

Import Java.io.PrintWriter;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

/**

* This is the first example of a servlet

* @author Cn.dragon

*/

public class Servletdemofirst extends HttpServlet {

Used to process a GET request sent by a client

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Response.setcontenttype ("text/html;charset=gb2312"); This statement indicates the format of the content sent to the client and the character encoding used.

PrintWriter out = Response.getwriter ();

OUT.PRINTLN ("Hello! "); To send data to the client using the method of the PrintWriter object

Out.close ();

}

For handling post requests sent by clients

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response); The purpose of this statement is to call the Doget () method for processing when the client sends a POST request

}

}

Deployment of Servlets

The following interception section

<servlet>
<description> any </description>
<display-name> any </display-name>
<servlet-name>Servletdemofirst</servlet-name>
<servlet-class>Cn.dragon.servlet.ServletDemoFirst</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Servletdemofirst</servlet-name>
<url-pattern>/servlet/servletdemofirst</url-pattern>
</servlet-mapping>

Note

① above two <servlet-name> must be the same
②<servlet-class> refers to the corresponding class above. Tip: You can copy it directly from your servlet class to avoid mistakes!
③<url-pattern> must be a /servlet and a servlet name. Everyone is going to remember that now.

Servlet Instance Demo

Package cn.dragon.servlet;

Import java.io.IOException;
Import Java.io.PrintWriter;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Servletdemosecond extends HttpServlet {
Initialization
public void Init () throws Servletexception {
System.out.println ("I Am the init () Method!") Used for initialization work ");
}
Handling GET Requests
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
System.out.println ("I Am the Doget () Method! Used to process a GET request ");
Response.setcontenttype ("text/html;charset=gb2312");
PrintWriter out = Response.getwriter ();
Out.println ("<HTML>");
Out.println ("<BODY>");
Out.println ("This is an example of a servlet");
Out.println ("</BODY>");
Out.println ("</HTML>");
}
Processing Post Requests
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
Destroying instances
public void Destroy () {
Super.destroy ();
System.out.println ("I Am the Destroy () Method! Used for the destruction of instances ");
}
}

Web. xml file

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.4"
Xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee
Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd ">

<servlet>
<servlet-name>ServletDemoSecond</servlet-name>
<servlet-class>cn.dragon.servlet.ServletDemoSecond</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletDemoSecond</servlet-name>
<url-pattern>/servlet/ServletDemoSecond</url-pattern>
</servlet-mapping>

</web-app>

Jsp/servlet Getting Started tutorial--servlet Getting started with

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.