2017-02-18 66 views
0

我在我的網站上有一個簡單的html表單,它將數據發送到servlet並在成功時獲得響應。如果我在本地主機上或專用測試服務器上測試/運行Wildfly服務器上的代碼,它可以正常工作。無法在真實網絡服務器上發送html表格

我已將我的網站上傳到公共虛擬主機(1and1)。如果我嘗試發送表單,則會發生錯誤,表明我的servlet未找到。有什麼問題?

的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> 

我的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 = "[email protected]"; 
    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(); 

    } 

我的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"); 
     } 
    }); 

    }); 
}); 

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

我的文件structur

首頁| - src | --default.servlets | - MailServlet.java

+0

可能是重複的http://stackoverflow.com/questions/11731377/servlet-returns-http-status-404-the-requested-resource-servlet-is-not-availa –

回答

0

檢查你的url,也許在你的開發機器上你使用了一個上下文路徑,這在公共服務器上是不一樣的。

查看來自瀏覽器開發工具的http請求和響應,並驗證動作url是否存在問題。

0

這都是相對與絕對路徑從HTML引用Servlet時這裏:

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

看一看this post。在「引用HTML中的servlet URL」下,你應該知道的所有內容都會被解釋。

相關問題