Operation Method:
1. Create a project;
File --> new --> Java Project --> TestServlet (Project name) --> Finish.
2. Load the servlet-api.jar class package;
TestServlet (right-click) --> Build Path --> Configure Build Path --> Library
--> Add External JAR Selection --> (browse to the lib directory of tomcat find the servlet-api.jar selected and click open ). --> OK
3. Create a HelloWorldServlet class;
The Code is as follows:
import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class HelloWorldServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("doGet"); response.getWriter().write("<a href=http://www.baidu.com>go</a>"); }}
4. Switch to the class generated by copying the navigator view.
5. Create a folder named test under the webapps root directory of tomcat.
6. Go to the test folder and create a folder named WEB-INF
7. Create a new file named web. xml with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>HW</servlet-name> <servlet-class>HelloWorldServlet</servlet-class></servlet> <servlet-mapping> <servlet-name>HW</servlet-name> <url-pattern>/ab</url-pattern> </servlet-mapping></web-app>
Note:
The HW name is the same as the following. HelloWorldServlet is the name of the class file to be tested.
/AB is used when the browser accesses:
Http: // 127.0.0.1: 8080/test/AB
Call this servelet
8. Create a folder named classes and put the copied class file in it.
9. Start the tomcat service. In apache-tomcat-7.0.54, set the environment variable, so you can start -- run -- input: startup to start the service.
If it is tomcat6.0, double-click the service to open the service. Of course, you can also find tomcat service management in the Start menu to open the service.
10. Open your browser and enter the URL and press Enter:
Http: // 127.0.0.1: 8080/test/AB
Note:
This servelt code function is
1. doget is printed in the tomcat background,
2. When go is clicked, it will automatically jump to the Baidu homepage!