Knowing the role of the servlet, let's look at a servlet's program running an instance, why do you want to see the running instance? Because you want to run a servlet program successfully, you need to configure some files.
The writing of the servlet program
A servlet program that develops an HTTP request that can be processed, must inherit the HttpServlet class, and at the very least overwrite the doget () method provided in the HttpServlet class in the custom servlet class as follows:
public void doget (HttpServletRequest req,httpservletresponse resp)
throws Servletexception,ioexception
It is not difficult to see from this method that this method can use the request and response objects.
Don't worry, let's take an example and say it from Hello World.
Create a project test in MyEclipse, and then create a package servlet to create a servlet_01.java underneath the package. As shown in the following illustration:
Do not rush to write code after the project, we also want to import a jar package----Servlet-api.jar, this is the Servlet jar package, this package needs to download online, but this article has attached the download link to the package.
To import a jar package into a Lib file in MyEclipse
Then write the code for Servlet_01.java, as follows:
Servlet_01.java
package Servlet;
Import java.io.*;
Import javax.servlet.ServletException;
Import javax.servlet.http.*;
public class Servlet_01 extends httpservlet{public
void doget (HttpServletRequest req,httpservletresponse)
throws servletexception,ioexception{ //Overwrite Doget () method
PrintWriter out=resp.getwriter ();//Prepare Output
Out.println ("
This code inherits HttpServlet and then overrides the Doger () method, which first obtains an output stream object from the HttpServletResponse object and then prints the HTML element.