2
我对带有@POST方法的@Path(“/ path”)的HttpServlet用法与doPost(HttpServletRequest请求,HttpServletResponse响应)方法有点混淆。我已经看到许多使用@Path的良好工作示例,但从未使用doPost的工作示例。当我尝试使用doPost时,它永远不会被调用。HttpServlet使用@Path(“/ path”)@POST与doPost
首先,有什么区别,哪个更好?另外,我在做什么错误的doPost
public class PostExample extends HttpServlet{
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
// HttpServlet doPost never gets called
System.out.println("hit test doPost "+request.getRequestURI());
}
}
谢谢!
首先谢谢你的解释! 我正在使用Jersey,并且在两个实现下都在web.xml文件中注册了servlet - 我只是在类中测试时评论必要的块。 话虽如此,为什么doPost不会被叫? @POST指定的方法被调用就好了? –
澄清,doPost方法是否需要从@POST指定的方法手动调用?我明白'服务'方法是假设自动调用'doPost'。 –
@POST注解指定该方法响应HTTP POST请求,不管你的方法名称在里面什么都可以。它可以是public void doPost()或public String doPost()或public String postMe()。您可以像在普通的HttpServlet的doPost方法中一样玩游戏。这是你问的一个例子[例子](https://github.com/gmazza/jersey-samples-on-cxf/blob/master/simple-servlet/src/main/java/com/sun/jersey/samples /servlet/resources/ResourceBean3.java) – Ravi