我的逻辑基本就是这样;
public static void main(String[] args) {
check("This is string");
System.out.println("logic continue");
}
private static void check(String text) {
if (text.equals("This is string")) {
System.out.println("true");
} else {
System.out.println("false");
}
}
我想检查另一种方法中的逻辑,如果语句是" false"我不想返回调用方法。
例如;
在"检查"方法,如果字符串不相等,程序/线程或其他东西必须完成,并且"逻辑继续"不能写。
我想在Web服务中使用此逻辑来检查标头。在doGet和doPost超级方法。如果标题不正确,则提供自定义异常,并且子类不会继续执行程序。
Thread.currentThread().stop();
上面的代码(Therad.currentThread()。stop())在sevlet中不起作用。
有人能以安全的方式了解这种方法吗?
编辑:
有些人理解我错了,所以我想编辑我的问题。以下是我想做的事情。我在动态Web应用程序中有servlet。所有这些servlet都扩展了myBaseServlet。
BaseServlet.java
public class BaseServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse resp) throws ServletException, IOException {
if(HeadersCheckHelpers.checkHeaders(httpServletRequest){
//if this part is working, everthing fine, application does what it wants
}else{
// if this else block works, thread should be stopped and non-return the subclasses
}
}
}
示例子servlet类
public class ContextServlet extends BaseServlet{
@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
super.doGet(httpServletRequest, httpServletResponse);
//if headers are not correct, i want this part not working. super class should handle this.
}
}
再次,如果逻辑是真或假,我想停止进程(我不想返回子类方法)。
答案 0 :(得分:0)
声明方法返回boolean
并在调用它时检查它:
public static void main(String[] args) {
if (check("This is string"))
System.out.println("logic continue");
}
private boolean void check(String text) {
if (text.equals("This is string")) {
System.out.println("true");
return true;
} else {
System.out.println("false");
return false;
}
}
澄清:我不知道你对java有多新,所以这一行
if (check("This is string"))
与:
相同boolean result = check("This is string");
if (result == true)
答案 1 :(得分:0)
在Servlet请求中,您必须始终返回响应。在这种情况下,您应该返回4xx Http状态:
if (!check("This is string")) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
} else {
System.out.println("logic continue");
}