-
servlet是一个在服务器上运行的小程序。一个servlet就是一个java类。并且可以通过“请求-相应”编程模型来访问这个驻留在服务器内存里的servlet程序。
-
-
继承HttpServlet
-
重写doGet()或doPost()方法
-
在web.xml中注册servlet
-
-
Servlet :
-
package servlet;
-
import java.io.IOException;
-
import java.io.PrintWriter;
-
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 TestServlet
-
*/
-
@WebServlet("/TestServlet")
-
public class TestServlet extends HttpServlet {
-
private static final long serialVersionUID = 1L;
-
-
@Override
-
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
// TODO Auto-generated method stub
-
PrintWriter out=resp.getWriter();
-
out.println("hello--");
-
}
-
@Override
-
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
-
// TODO Auto-generated method stub
-
PrintWriter out=resp.getWriter();
-
out.println("world");
-
}
-
}