关闭点击无效

时间:2016-02-20 12:00:07

标签: java html jsp servlets

我是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 个答案:

答案 0 :(得分:1)

尝试cast这些request.getParameter()String,如下所示:

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

并在equals条件中编写if方法,如下所示:

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

    if("off".equals(of))