2016-02-20 68 views
2

我是新的程序員在Java servlet我有我的網頁有問題。 問題陳述: 當我點擊按鈕,然後在服務器端它正常工作,但當我點擊關閉按鈕它不工作我的HTML代碼是好的。我認爲servlet文件有問題。請檢查下面的代碼並給我解決方案。關閉點擊不起作用

import java.io.IOException; 
import java.io.PrintWriter; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* 
* @author Shekh Ali 
*/ 
public class HomeAutomation extends HttpServlet { 

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> 
    * methods. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     try (PrintWriter out = response.getWriter()) { 
      /* TODO output your page here. You may use following sample code. */ 
      out.println("<!DOCTYPE html>"); 
      out.println("<html>"); 
      out.println("<head>"); 
      out.println("<title>Project HomeAutomation</title>");    
      out.println("</head>"); 
      out.println("<body>"); 
      out.println("<h1>Servlet HomeAutomation Server"); 
      String b1=request.getParameter("on"); 
      String of=request.getParameter("off"); 

       if(b1.equals("on")) 
       { 
        out.println("<h1>on is clicked</h1>"); 

       } 

       if(of.equals("off")) 
       { 
        out.println("<h1>off is clicked</h1>"); 
       } 


      out.println("</body>"); 
      out.println("</html>"); 
     } 
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> 
    /** 
    * Handles the HTTP <code>GET</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Handles the HTTP <code>POST</code> method. 
    * 
    * @param request servlet request 
    * @param response servlet response 
    * @throws ServletException if a servlet-specific error occurs 
    * @throws IOException if an I/O error occurs 
    */ 
    @Override 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     processRequest(request, response); 
    } 

    /** 
    * Returns a short description of the servlet. 
    * 
    * @return a String containing servlet description 
    */ 
    @Override 
    public String getServletInfo() { 
     return "Short description"; 
    }// </editor-fold> 

} 

回答

1

嘗試cast這些request.getParameter()String這樣的:

String b1=(String)request.getParameter("on"); 
    String of=(String)request.getParameter("off"); 

譜寫if條件equals方法是這樣的:

if("on".equals(b1)) 
    .... 

    if("off".equals(of)) 
+0

由於它的工作原理。 –