POST方法之外的创建方法是servlet

时间:2015-04-20 11:18:37

标签: java ajax servlets

您好我在servlet中创建了一个私有方法。 该方法将从post方法调用。我的问题是, 它是线程安全的 ,因为很多用户会通过ajax调用它吗?

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
    callPrivateMethod();
}

private static void callPrivateMethod(){
}

5 个答案:

答案 0 :(得分:2)

只要callPrivateMethod()是线程安全的,即它不使用类成员变量,那么你就没事了。

答案 1 :(得分:0)

不,您的私有方法不会是线程安全的,因为doPost在servlet中不是线程安全的。

答案 2 :(得分:0)

这是静态方法,在您的情况下,不可变对象作为参数(无参数)是线程安全的

答案 3 :(得分:0)

Servlet应该是无状态的。 Hawever,如果你需要使用类成员或任何其他线程不安全元素,你总是可以使用“同步”句子。

答案 4 :(得分:0)

servlet在加载时仅为实例一次。如果要调用Private Method()线程安全,可以将其放在同步块中。

private Object mutex = new Object();

protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
   synchronized (mutex){
    callPrivateMethod();
   }
}

private static void callPrivateMethod(){
}
相关问题