Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Servlet中使用服务方法,同时已经存在doGet和其他6种方法_Java_Jsp_Jakarta Ee_Servlets - Fatal编程技术网

Java 在Servlet中使用服务方法,同时已经存在doGet和其他6种方法

Java 在Servlet中使用服务方法,同时已经存在doGet和其他6种方法,java,jsp,jakarta-ee,servlets,Java,Jsp,Jakarta Ee,Servlets,我正在编写一个简单的servlet。它只打印用户传递的值,或在相应的URL参数中。现在在实现doGet方法时,我看到同样的功能也可以通过服务方法来执行。其代码如下: protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter o = resp.getWriter(); Str

我正在编写一个简单的servlet。它只打印用户传递的值,或在相应的URL参数中。现在在实现doGet方法时,我看到同样的功能也可以通过服务方法来执行。其代码如下:

protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {


    PrintWriter o = resp.getWriter();
    String user = req.getParameter("username");

    HttpSession session = req.getSession();
    ServletContext context = req.getServletContext();

    if (user != "" && user != null) {

        session.setAttribute("myhuzu", user);
        context.setAttribute("myhuzu", user);
    }

    o.println("request parameter has value " + user);
    o.println("session parameter  has user name as "
            + session.getAttribute("myhuzu"));

    o.println("context parameter  has user name as "
            + context.getAttribute("myhuzu"));

    o.println("init parameter  has user name as "
            + getServletConfig().getInitParameter("default"));


}
}

所以我的问题是,为什么我们需要doGet或doPOST方法,而服务方法本身正在处理所有这些事情。由于上面的代码运行完美,如果保存在doGet方法中,它也会运行完美,那么为什么它们不只保留其中一个呢?
注意:我非常了解servlet生命周期和其他相关概念,所以请不要解释所有这些概念

HttpServlet类中的服务方法检查head请求中的方法并重定向到指定的方法,如当它是get请求时,将调用doGet方法,当您的servlet将针对不同的方法(如REST服务)以不同的目的进行应答时使用它,如当您有get请求时,您将返回一个信息,但是当您有一个PUT请求时,您将更新一个信息,因此servlet为您提供了这个方法

此外,它还保证servlet不会响应错误的请求,使用像您这样的服务方法,我可以调用您的servlet,即使是像“request method TEST”这样的奇怪请求,servlet也会响应,并保持代码更干净

请参阅原始服务代码:

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long errMsg;
        if(method.equals("GET")) {
            errMsg = this.getLastModified(req);
            if(errMsg == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if(ifModifiedSince < errMsg) {
                    this.maybeSetLastModified(resp, errMsg);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if(method.equals("HEAD")) {
            errMsg = this.getLastModified(req);
            this.maybeSetLastModified(resp, errMsg);
            this.doHead(req, resp);
        } else if(method.equals("POST")) {
            this.doPost(req, resp);
        } else if(method.equals("PUT")) {
            this.doPut(req, resp);
        } else if(method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if(method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if(method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg1 = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg1 = MessageFormat.format(errMsg1, errArgs);
            resp.sendError(501, errMsg1);
        }

    }
protectedvoid服务(HttpServletRequest-req,HttpServletResponse-resp)抛出ServletException,IOException{
String method=req.getMethod();
长味精;
if(method.equals(“GET”)){
errMsg=this.getLastModified(req);
如果(errMsg==-1L){
这是doGet(请求,响应);
}否则{
long ifModifiedSince=req.getDateHeader(“如果自修改”);
if(ifModifiedSince
它根据所使用的请求方法做出不同的反应


如果您执行自定义http请求,您可以设置所需的方法,而不是GET、PUT或DELETE,您可以发送TEST,您的服务将抛出“method not implementation”异常,但如果您重写服务方法,它将简单地回答,您的servlet代码仍将被执行。

HttpServlet类中的服务方法检查head请求中的方法,并重定向到指定的方法,如当它是get请求时,将调用doGet方法,当您的servlet针对不同的方法(如REST服务中)以不同的目的应答时,将使用doGet方法,当您有一个GET请求时,您将返回一个信息,但是当您有一个PUT请求时,您将更新一个信息,所以servlet为您提供了这个方法

此外,它还保证servlet不会响应错误的请求,使用像您这样的服务方法,我可以调用您的servlet,即使是像“request method TEST”这样的奇怪请求,servlet也会响应,并保持代码更干净

请参阅原始服务代码:

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getMethod();
        long errMsg;
        if(method.equals("GET")) {
            errMsg = this.getLastModified(req);
            if(errMsg == -1L) {
                this.doGet(req, resp);
            } else {
                long ifModifiedSince = req.getDateHeader("If-Modified-Since");
                if(ifModifiedSince < errMsg) {
                    this.maybeSetLastModified(resp, errMsg);
                    this.doGet(req, resp);
                } else {
                    resp.setStatus(304);
                }
            }
        } else if(method.equals("HEAD")) {
            errMsg = this.getLastModified(req);
            this.maybeSetLastModified(resp, errMsg);
            this.doHead(req, resp);
        } else if(method.equals("POST")) {
            this.doPost(req, resp);
        } else if(method.equals("PUT")) {
            this.doPut(req, resp);
        } else if(method.equals("DELETE")) {
            this.doDelete(req, resp);
        } else if(method.equals("OPTIONS")) {
            this.doOptions(req, resp);
        } else if(method.equals("TRACE")) {
            this.doTrace(req, resp);
        } else {
            String errMsg1 = lStrings.getString("http.method_not_implemented");
            Object[] errArgs = new Object[]{method};
            errMsg1 = MessageFormat.format(errMsg1, errArgs);
            resp.sendError(501, errMsg1);
        }

    }
protectedvoid服务(HttpServletRequest-req,HttpServletResponse-resp)抛出ServletException,IOException{
String method=req.getMethod();
长味精;
if(method.equals(“GET”)){
errMsg=this.getLastModified(req);
如果(errMsg==-1L){
这是doGet(请求,响应);
}否则{
long ifModifiedSince=req.getDateHeader(“如果自修改”);
if(ifModifiedSince
它根据所使用的请求方法做出不同的反应

如果您执行自定义http请求,您可以设置所需的方法,而不是GET、PUT或DELETE,您可以发送测试,并且您的服务将抛出“method not implementation”异常,但是如果您重写服务methodo