gpt4 book ai didi

java - 关闭点击不起作用

转载 作者:行者123 更新时间:2023-11-30 07:35:02 25 4
gpt4 key购买 nike

我是 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>

}

最佳答案

尝试将这些 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))

关于java - 关闭点击不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35522930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com