request.getParameter()对提交按钮名称返回始终为null

时间:2017-11-14 15:42:37

标签: java html forms jsp servlets

我的网站上有联系表格,我正在努力让它发送电子邮件给我。电子邮件发件人类工作正常问题是当我尝试从表单获取参数时,它总是返回null。我用另一个简单的形式进行了测试,代码工作但它不能在我自己的表单上工作。这是来自jsp文件的表单。

<!-- Contact Form -->

<div class="col-md-6">
  <form id="contact" class="contact-form row" role="form" method="post">
    <div class="col-md-12 text-center">
      <h5 class="successContent">
        <i class="fa fa-check left" style="color: #5cb45d;"></i>Your message has been sent successfully.
      </h5>
      <h5 class="errorContent" style="color: #e1534f;">
        <i class="fa fa-exclamation-circle left"></i>There was a problem validating the form please check!
      </h5>
    </div>

    <div class="col-md-12">
      <div class="form-field-wrapper">
        <input class="input-lg form-full" id="form-name" type="text" name="name" placeholder="Your Name" required />
      </div>


    </div>

    <div class="col-md-12">
      <div class="form-field-wrapper">
        <input class="input-lg form-full" id="form-email" type="email" name="email" placeholder="Email" required />
      </div>
    </div>

    <div class="col-md-12">
      <div class="form-field-wrapper">
        <input class="input-lg form-full" id="form-subject" type="text" name="subject" placeholder="Subject" />
      </div>
    </div>

    <div class="col-md-12">
      <div class="form-field-wrapper">
        <textarea class="home-contact-txt-msg input-lg form-full" id="form-message" rows="7" name="message" placeholder="Your Message" required></textarea>
      </div>

    </div>

    <div class="col-md-12">
      <input class="btn-contact-submit btn btn-lg btn-black" type="submit" name="submit" value="Send Message">
    </div>
  </form>
</div>
</div>
<!-- Contact Form End -->

这是我需要获取参数的部分。

<div class="col-md-12">
  <input class="btn-contact-submit btn btn-lg btn-black" type="submit" name="submit" value="Send Message">
</div>

这是发送电子邮件的代码。

<%
    String status = null;
    String parameter = null;

    //If request is submit, sending email.
    if (request.getParameter("submit") != null) {

        EmailSender email = new EmailSender();
        email.setMailServerProperties();

        //Email subject.
        String emailSubject = "Message from asdasd.com";
        //Email body.
        String emailBody = "";

        //If name is not empty, adding name to email body.
        if (request.getParameter("name") != null) {
            emailBody = "Sender Name: " + request.getParameter("name") + "<br>";
        }

        //If email is not empty, adding email to email body.
        if (request.getParameter("email") != null) {
            emailBody = emailBody + "Sender Email: " + request.getParameter("email") + "<br>";
        }

        //If subject is not empty, adding subject to email body.
        if (request.getParameter("subject") != null) {
            emailBody = emailBody + "Subject: " + request.getParameter("subject") + "<br>";
        }

        //If message is not empty, adding message to email body.
        if (request.getParameter("message") != null) {
            emailBody = emailBody + "Message: " + request.getParameter("message") + "<br>";
        }

        //Creating email object with emailSubject and emailBody.
        email.createEmailMessage(emailSubject, emailBody);


        /*
        try {
            //Sending email.
            email.sendEmail();          
        } catch (MessagingException me) {
            out.print(me.getMessage());         
        }
        */
        status = "Email sent.";
        parameter = request.getParameter("submit");
        out.print(status + parameter);
    } else {
        status = "Email not sent.";
        parameter = request.getParameter("submit");             
        out.print(status + parameter);
    }
%>   

如果单击表单提交按钮,如何使此部分(request.getParameter("submit") != null)成立?

编辑:我注意到了一些事情。我无法获取此页面中任何字段的参数,而不仅仅是提交按钮。

Edit2:经过多次尝试后,工作代码开始无效。问题可以与apache tomcat有关吗?有没有办法在没有服务器问题的情况下持续测试它。

1 个答案:

答案 0 :(得分:-1)

您是否在JSP文件中添加了库?与<%@ page import = "java.io.*,java.util.*" %>

一样