简单 Java If 语句无法与 Sevlet 一起正常工作或 Java class

Simple Java If Statement not working properly with Sevlet or Java class

我正在使用 eclipse Keplerr IDE 进行动态 Web 项目

出现的问题是,即使 java 条件 if 语句正确,简单且数据正确,它也不会返回 IF 语句的第一个(真)部分,即使它是真的。

我的代码:

GetSetClass(模型)

package tempTryOct15;


public class GetSetClass {

    String Conditionalz(String txt1){

        String t1 = txt1;
        System.out.println("The String Received is:" + t1);

        String ret="NULL-Value";
        if(t1=="ssc")
            ret="SSC";
        else
            ret="Non-SSC";

        System.out.println("returned: " + ret);
        return ret;
    }

}

tryServlet.java(控制器)

package tempTryOct15;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class tryServlet
 */
public class tryServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public tryServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String txt1=request.getParameter("txt1");
        String txt2=request.getParameter("txt2");

        System.out.println("The 1st String Entered is:" + txt1);
        System.out.println("The 2nd String Entered is:" + txt2);

        request.setAttribute("text1",txt1);

        /*
        GetSetClass GSC = new GetSetClass();
        String text2 = GSC.Conditionalz(txt1);
        System.out.println(text2);

        */

        if(txt1=="ssc")
            txt2="SSC";
        else
            txt2="Non-SSC";

        request.setAttribute("text2",txt2);

        System.out.println("The 2nd String Got is:" + txt2);

        RequestDispatcher view = request.getRequestDispatcher("Display.jsp");
            view.forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

相同的代码在 core plain java 和 Notepad++ 编辑器上运行良好。 如:

Try.java

class Try{

    String Conditionalz(String t1){

        System.out.println("The String Received is:" + t1);

        String ret="NULL-Value";
        if(t1=="ssc")
            ret="SSC";
        else
            ret="Non-SSC";

        return ret;
    }
}

TryDemo.java

class TryDemo{
    public static void main(String[] args){

        Try t = new Try();
        String ret = t.Conditionalz("ssc");
        System.out.println("returned: " + ret);

    }
}

由于 String 不是原始类型,因此您必须使用 str1.equals(str2) 而不是 str1==str2 ...