简体   繁体   English

条件是否出现故障? JSP脚本

[英]if condition malfunctioning? JSP scriptlet

I am facing a problem that I have no clue what it going on with it.. I made a small scriptlet in JSP page to check a simple condition, if there is a session then it does something, if not it should just redirect the page. 我面临的一个问题是我不知道它到底在发生什么。.我在JSP页面中制作了一个小scriptlet,以检查一个简单的条件,如果有会话,那么它会执行某些操作,如果没有,则应该重定向页面。 but what happens is something i can't really explain.. the if condition is doing something really strange! 但是发生的事情是我无法真正解释的.. if条件正在做的事情真的很奇怪!

here is a sample code: 这是一个示例代码:

<%
boolean err = false;
if(session.getAttribute("id") != null)
    {
        cus_id = session.getAttribute("id").toString();
        err=false;
    }else  err=true;

    out.println(err);
    if (err) out.println("<br>There is no session<br>");
    //if(!err)   ############ where it goes completely wrong.##############
    if(1==2)
    {
      statement = "SELECT * FROM flight WHERE fl_code = '"+fco+"'";
      flight   = getData(statement);

      statement = "SELECT * FROM tickets WHERE tik_cus_id ='"+cus_id+"' AND tik_fl_id ='"+flight[0][0]+"'";
      ticket = getData(statement);

      if(ticket.length == 0)
      {
        statement = "INSERT INTO tickets VALUES(null,'"+   session.getAttribute("id")+
                                            "','"+flight[0][0]+
                                            "','"+cls+"','"+seat+
                                            "','"+adul+"','"+chil+
                                            "','"+infa+"','"+pric+"','N')";
        lastId = insertData(statement);
      }
      if(ticket.length==4) //if error occurs
      {
        out.println("<br><br>"+ticket[0]+"<hr>");
        out.println(ticket[1]+"<br>");
        out.println(ticket[2]+"<br>");
        out.println(ticket[3]+"<br>");
      }
      response.sendRedirect("../../confirm.jsp?fco="+flight[0][0]);
    }   else out.println("no session");//else response.sendRedirect("../../login.jsp");
%>

When the condition near where i pointed is if(!err) i get this error 当我指向的位置附近的条件是if(!err)此错误

`rg.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 50 in the jsp file: /scripts/jsp/confirm.jsp
The local variable cus_id may not have been initialized
47:     statement = "SELECT * FROM flight WHERE fl_code = '"+fco+"'";
48:     flight   = getData(statement);
49:     
50:     statement = "SELECT * FROM tickets WHERE tik_cus_id ='"+cus_id+"' AND tik_fl_id ='"+flight[0][0]+"'";
51:     ticket = getData(statement);
52:     
53:     if(ticket.length == 0)`

which is obvious since there is no session, and cus_id comes from the session. 这很明显,因为没有会话,而cus_id来自该会话。 but when I make the condition if(1==2) this is the output: true There is no session no session 但是当我设定条件if(1==2)这是输出: true There is no session no session

As you can see there is not session.. I am 100% sure i have no session, yet when the condition is if(!err) (so when err is false, error is triggered when there is no session) it will get inside, however it clearly outputs that there is an error yet it goes inside the condition to try to execute the code.. but when I put the if(1==2) condition which is clearly false it does not.. I tried using integer, string, char and even double and float but whenever a variable gets into the condition it just passes it as true when it is not.. but it recognizes the if(1==2) or other similar conditions as false. 如您所见,没有会话。.我100%确保没有会话,但是当条件为if(!err) (因此,当err为false时,如果没有会话则触发错误),它将进入内部,但是它显然输出一个错误,但它仍然在尝试执行代码的条件内..但是当我将if(1==2)条件(显然是错误的if(1==2)放入时,它并没有..我尝试使用整数,字符串,字符甚至双精度和浮点型,但只要变量进入条件,它就会在不满足条件时将其传递为true。但是它将if(1==2)或其他类似条件识别为false。

what is going on here? 这里发生了什么? it is making absolutely no sense I am using Tomcat7 server, i played around with the code for hours now and it just doesn't make sense from any way I think about it.. all my tries were futile 我正在使用Tomcat7服务器,这完全没有道理,我玩了几个小时的代码,从我的任何角度来看都没有道理。.我所有的尝试都是徒劳的

Please help 请帮忙

If I understand correctly you're not running into anything your if condition actually does. 如果我理解正确,那么您的if条件实际上并没有遇到任何问题。 Instead you're running into static code analysis that the compiler does: It determines that there is a condition under which your local variable might not be initialized and flags that. 取而代之的是,您将进入编译器进行的静态代码分析:它确定在某种情况下您的局部变量可能不会初始化并进行标记。

When you deactivate a large block with if(1==2) the compiler is smart enough to determine that this block will never be executed and probably never generates any code for it (and ignores any flags inside it). 当使用if(1==2)停用大块时,编译器足够聪明,可以确定该块将永远不会执行,并且可能永远不会为其生成任何代码(并忽略其中的任何标志)。

There are several ways to improve your code (incomplete, but a starting point): 有几种改进代码的方法(不完整,但只是一个起点):

  • Omit memorizing err and just implement all the code within the original if condition 省略记忆err ,仅在原始if条件下实现所有代码
  • Stop writing Java scriptlets in JSPs and use proper MVC - then you'd determine that you don't have a session before delegating to the JSP (and delegate to a different page, from the controller) and the page could just assume that there's always a session available 停止在JSP中编写Java scriptlet并使用适当的MVC-然后在委派给JSP(并从控制器委派到另一个页面)之前,先确定自己没有会话,并且该页面可能只是假设始终存在一个可用的会话

Answering your comment's question about what "omit memorizing err" means: Just don't have that variable. 回答您的评论有关“省略记忆错误”的含义的问题:就是没有该变量。 Pseudocode: 伪代码:

if(session.getAttribute("id") != null) {
    String cus_id = session.getAttribute("id").toString();
    // now cus_id is initialized, and local to this block 
    // (it's not used elsewhere anyway)

    // add your database code here
} else {
    // add your error handling code here
}

And not to forget: The way you're constructing your SQL instructions is prone to be attacked. 不要忘记:您构造SQL指令的方式很容易受到攻击。 Please read about prepared statements (eg parameterized queries) and about Little Bobby Tables . 请阅读有关准备好的语句(例如,参数化查询)和有关Little Bobby Tables的信息

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

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