隐藏

JavaWeb(继承HttpServlet类)
2022年 03月 21 日

Calvin

HttpServletRequest是servletRequest的子接口,HttpServlet实现了此接口,故HttpServlet类可以使用servletRequest下的所有方法。

示例代码如下:

public class ChenTest extends HttpServlet {

                //继承HttpServlet类后需要重写doPost和doGet方法来处理前端表单的请求
      @Override//(对应前端表单内action定义的"get"&"post")
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //doGet()方法用于处理客户端浏览器直接访问和表单get方式提交的表单。
    //路径会在地址栏内直接显示,不安全。
        this.doPost(request,response);//为了提高复用性,当有get请求传入时,携带传入的参数直接调用post方法。
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //doPost()方法用来处理表单post方式的提交,不能处理客户端浏览器的直接访问。
        //在此方法内实现具体的业务逻辑。
    }
}

在一个HttpServlet内实现多个servlet请求

  1. 在前端表单action路径后附带标识请求: action="/*?method=*"
    如:<form action="/emp?method=add" method="post"></form>
  2. web.xml内配置好Servlet映射路径(不需要带上自定义的标识请求),

    <servlet-mapping>   //映射示例
         <servlet-name>employeeServlet</servlet-name>
         <url-pattern>/emp</url-pattern>
     </servlet-mapping>
     <servlet>
         <servlet-name>employeeServlet</servlet-name>
         <servlet-class>com.controller.EmployeeServlet</servlet-class>
     </servlet>
  3. 在创建好的HttpServlet实现类内编写不同的方法来判断调用不同方法处理请求
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws 
    ServletException, IOException {
    
            req.setCharacterEncoding("utf-8");
                     //设置编码方式
        
            String method = req.getParameter("method");
                    //获取请求的方法参数
        
        switch (method){
              case "add"://判断自定标识路径,若符合就进入语句体内
                     addChen(req,resp);
                     break;
              case "updateChen"://需要多少请求就写多少请求,无需在web.xml内重新配置
                     updateEmp(req,resp);
                     break;
        } }
        private void addChen (HttpServletRequest req, HttpServletResponse resp){
                        ······
                //在方法体内实现具体业务逻辑
        }
        private void updateChen (HttpServletRequest req, HttpServletResponse resp){
                        ······
                 //在方法体内实现具体业务逻辑
        }

JavaWeb(继承HttpServlet类)

HttpServletRequest是servletRequest的子接口,HttpServlet实现了此接口,故HttpServlet类可以使用servletRequest下的所有方法。

示例代码如下:

public class ChenTest extends HttpServlet {

                //继承HttpServlet类后需要重写doPost和doGet方法来处理前端表单的请求
      @Override//(对应前端表单内action定义的"get"&"post")
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //doGet()方法用于处理客户端浏览器直接访问和表单get方式提交的表单。
    //路径会在地址栏内直接显示,不安全。
        this.doPost(request,response);//为了提高复用性,当有get请求传入时,携带传入的参数直接调用post方法。
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //doPost()方法用来处理表单post方式的提交,不能处理客户端浏览器的直接访问。
        //在此方法内实现具体的业务逻辑。
    }
}

在一个HttpServlet内实现多个servlet请求

  1. 在前端表单action路径后附带标识请求: action="/*?method=*"
    如:<form action="/emp?method=add" method="post"></form>
  2. web.xml内配置好Servlet映射路径(不需要带上自定义的标识请求),

    <servlet-mapping>   //映射示例
         <servlet-name>employeeServlet</servlet-name>
         <url-pattern>/emp</url-pattern>
     </servlet-mapping>
     <servlet>
         <servlet-name>employeeServlet</servlet-name>
         <servlet-class>com.controller.EmployeeServlet</servlet-class>
     </servlet>
  3. 在创建好的HttpServlet实现类内编写不同的方法来判断调用不同方法处理请求
@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws 
    ServletException, IOException {
    
            req.setCharacterEncoding("utf-8");
                     //设置编码方式
        
            String method = req.getParameter("method");
                    //获取请求的方法参数
        
        switch (method){
              case "add"://判断自定标识路径,若符合就进入语句体内
                     addChen(req,resp);
                     break;
              case "updateChen"://需要多少请求就写多少请求,无需在web.xml内重新配置
                     updateEmp(req,resp);
                     break;
        } }
        private void addChen (HttpServletRequest req, HttpServletResponse resp){
                        ······
                //在方法体内实现具体业务逻辑
        }
        private void updateChen (HttpServletRequest req, HttpServletResponse resp){
                        ······
                 //在方法体内实现具体业务逻辑
        }

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论