简体   繁体   English

JAVA:Servlet会根据所使用的方法doGet / doPost做一些不同的事情

[英]JAVA: Servlet doing something different depending on used method doGet/doPost

I'm quite a newbie when it comes to servlets and I would like somebody to help me a little bit. 关于servlet,我是一个新手,我希望有人能帮上我一点。

I need to write a simple method calling println with an different information depending on used doPost or doGet , for example: 我需要编写一个简单的方法,调用println,并根据所使用的doPostdoGet提供不同的信息,例如:

if (doPost was used) {
    out.println("The doPost method was used);
}

else if (doGet was used) {
    out.println("The doGet method was used);
}
else
{
    out.println("Neither doPost nor doGet was used");
}

Can somebody help me? 有人可以帮我吗? :) :)

Thanks in advance! 提前致谢!

An example of simple servlet that it would do something similar that you want: 一个简单的servlet的示例,它将执行您想要的类似操作:

public class ServletDemo1 extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        // do something with GET petitions  
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        // do something with POST petitions 
    }

}

This code do something different depending what type of petition GET or POST is coming. 这段代码会根据GET或POST请求的类型进行不同的操作。 Or you could use the service method: 或者,您可以使用服务方法:

protected void service(HttpServletRequest req, HttpServletResponse resp) {...}

and filter depending of the request method value ( request.getMethod() ). 并根据请求方法值( request.getMethod() )进行过滤。 You could manage more than GET or POST (like PUT, DELETE...) 您可以管理的不仅仅是GET或POST(例如PUT,DELETE ...)

A servlet typically has two methods which you define named doGet(...) and doPost(...). Servlet通常具有两个定义为doGet(...)和doPost(...)的方法。 On the client side, when you make a request to the servlet on the server side, you generally specify which method you want to send your request information to. 在客户端,当您向服务器端的Servlet发出请求时,通常会指定要将请求信息发送到的方法。 For instance, if you are working on a Java Server Page that involves JQuery you would make an Ajax call like this: 例如,如果您正在处理涉及JQuery的Java Server Page,那么您将像这样进行Ajax调用:

$.ajax({
      type: "GET",
      url: "/ChatEngine/ChatServlet/users/list?roomId=" + roomID, 
      contentType: "application/json; charset=utf-8",
      cache: false,
      dataType: "json",
      success: process,
      error: function(err) {
      alert('Get User List Error:' + err.responseText + '  Status: ' + err.status); 
      }
    }); 

Notice the type attribute is set to "GET". 请注意,type属性设置为“ GET”。 It could just as easily be set to POST. 可以很容易地将其设置为POST。 When the servlet on the server side receives the request, it will know which method to send the request data to, either doGet(...) or doPost(...). 当服务器端的servlet收到请求时,它将知道将请求数据发送到哪个方法,即doGet(...)或doPost(...)。 In the servlet, in each method doGet(...) and doPost(...) you can write your println statement signifying which one was executed. 在servlet中,可以在每种方法doGet(...)和doPost(...)中编写您的println语句,以表明执行了哪一个。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM