-
编写一个类去实现Servlet接口。
-
实现service()方法,处理请求,并响应数据。
-
既然这个东西是跑在服务器上的,那么所有服务器上的东西都是需要一个访问地址与之对应访问。那么这个地址的配置就是到web.xml中去配置Servlet程序的访问地址。
//在创建的项目中对类进行接口实现
package com.servlet.one;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
@Override
public void init(ServletConfig config) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("Hello Servlet 被访问了");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
然后就是在web.xml中配置
HelloServlet com.servlet.one.HelloServlet HelloServlet /hello2
http://ip:port/工程路径,这里的工程路径指的是idea中tomcat的配置那一块。
然后我们去启动项目,默认访问的是index.jsp首页,地址为:http://localhost:8080/06_servlet/
但是我们去访问自己配置的http://localhost:8080/06_servlet/hello2时,我们所写的service方法就被执行。



