servelet 容器
将前台的请求转发给后台 接受 http 表单, 后台处理操作数据库并且放回用户 。(粗劣)
1, 继承httpservlet
2, 重写 doGet() , doPost()
3, 在web.xml 中注册servlet
目录结构:
第一个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 HelloServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("处理get"); PrintWriter out = resp.getWriter(); resp.setContentType("text/html;charset=utf-8"); out.println("<strong> i know </strong>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("处理post method"); PrintWriter out = resp.getWriter(); String str = "username: " + req.getParameter("username") + " " + "password: " + req.getParameter("password"); out.println(str); } }
1, 当编写的servlet接受的是 get 方法, 就会调用 doGet 方法处理
2, 当servlet 接受的是 post 方法, 就会调用 doPost 方法。
获取请求表单里的数据 :
request,getParameter("name");