0

I have a simple html form on my website which sends the data to a servlet and get a response if success. If I test/run the code on Wildfly server on localhost or on a private test server it works fine.

I have uploaded my site to a public webhost (1and1). If I try to send the form, I get the error that my servlet was not found. Whats the problem?

index.html

<form id="contactForm" method="post" action="MailServlet" name="contactForm">
  <div class="form-group">
    <label for="InputName">Ihr Name</label> <input type="text" class="form-control" id="name" name="name">
  </div>
  <div class="form-group">
    <label for="InputEmail1">Ihr EMail Adresse</label> <input type="email" class="form-control" id="mail" name="mail" required="required">
  </div>
  <div class="form-group">
    <label for="InputMessage">Ihre Nachricht an uns</label>
    <textarea class="form-control" id="nachricht" rows="8" name="nachricht" required="required"></textarea>
  </div>

  <button type="submit" class="btn btn-ar btn-primary" id="sendenBtn" style="margin-top: 10px;">senden</button>
  <div class="clearfix"></div>
</form>
<div id="antwort" style="color: green; font-weight: bold;"></div>

My servlet MailServlet.java

@WebServlet("/MailServlet")
public class MailServlet extends HttpServlet {
  private static final long serialVersionUID = 1 L;

  private static final String SMTP_HOST_NAME = "smtp.1und1.de";
  private static final String SMTP_AUTH_USER = "..@....de";
  private static final String SMTP_AUTH_PWD = "...";

  public MailServlet() {
    super();
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    doPost(request, response);
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
    PrintWriter out = response.getWriter();

    response.setContentType("text/html;charset=UTF-8");

    String name = request.getParameter("name");
    String email = request.getParameter("mail");
    String nachricht = request.getParameter("nachricht");

    if (email.equals("") || nachricht.equals("")) {

      out.write("Bitte geben Sie Ihre EMail Adresse und eine Nachricht ein.");
      return;
    } else {

      //Simple Method to send an email
      contact(name, email, nachricht);

      out.write("Vielen Dank, wir haben Ihre Nachricht erhalten.");

    }
    out.close();

  }

my script.js

$(document).ready(function() {

  //Kontaktform
  $("#contactForm").submit(function(e) {

    e.preventDefault();

    $.ajax({
      context: this,
      url: $(this).attr('action'),
      type: $(this).attr('method'),
      data: $(this).serialize(),
      dataType: 'html',
      success: function(data) {
        $("#antwort").text(data);
      },
      error: function(data) {
        alert("Es trat ein Fehler auf. Bitte versuchen Sie es erneut");
      }
    });

  });
});

My web.xml

<welcome-file-list>
  <welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
  <servlet-name>Faces Servlet</servlet-name>
  <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>Faces Servlet</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>MailServlet</servlet-name>
  <url-pattern>/MailServlet</url-pattern>
</servlet-mapping>
</web-app>

My file-structur

Homepage | -- src |-- de.homepage.servlets |-- MailServlet.java

Captai-N
  • 1,124
  • 3
  • 15
  • 26
  • Possibly a duplicate of http://stackoverflow.com/questions/11731377/servlet-returns-http-status-404-the-requested-resource-servlet-is-not-availa – René Scheibe Feb 18 '17 at 17:59

2 Answers2

0

Check your url, maybe on your dev machine you used a context path which is not the same on the public server.

See the http request and response from the browser dev tool and verify if there is some issues with the action url.

minus
  • 2,646
  • 15
  • 18
0

It's all about relative vs. absolute paths when referencing the servlet from HTML here:

<form id="contactForm" method="post" action="MailServlet" name="contactForm">

Have a look at this post. Under "Referencing the servlet URL from HTML" everything you should know is explained.

Community
  • 1
  • 1
René Scheibe
  • 1,970
  • 2
  • 14
  • 20