1

我有一个简单Servlet的发送电子邮件和一个html-form

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>A form</title>
 </head>
 <body>
 <form action="feedback" method="post">

  <!-- Simple text field -->
 <label for="name">Name </label>
 <input type="text" name="name"/>
 <br/>

  <!-- Email -->
 <label for="email">Email </label>
 <input type="email" name="email"/>
 <br/>


  <!-- Textarea -->
 <label for="description">Description </label>
 <textarea  name="description" cols="50" rows="5">Type your comment here</textarea>
 <br/>


 <input type="submit" name="submit" value="Send Request"/>
 </form>
 </body>
</html>

在 web.xml 文件夹中,我注册并映射了 servlet。但是当我按下发送按钮时,我得到:

HTTP 错误 404

访问/反馈时出现问题。原因:

NOT_FOUND

我的问题还在哪里?

--UPDATE-- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee ">

<!-- Servlets -->
<servlet>
    <servlet-name>FeedbackServlet</servlet-name>
    <servlet-class>org.wunderapps.mailservice.server.FeedbackServlet</servlet-class>
</servlet>

<servlet>
    <servlet-name>TestAppServlet</servlet-name>
    <servlet-class>org.wunderapps.mailservice.server.TestAppServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>FeedbackServlet</servlet-name>
    <url-pattern>/wunderapps_mail_service/mail</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>TestAppServlet</servlet-name>
    <url-pattern>/wunderapps_mail_service/test</url-pattern>
</servlet-mapping>

<!-- Default page to serve -->
<welcome-file-list>
    <welcome-file>Wunderapps_mail_service.html</welcome-file>
</welcome-file-list>

小服务程序:

package org.wunderapps.mailservice.server;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class FeedbackServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name = req.getParameter("name");
        String description = req.getParameter("description");
        String email = req.getParameter("email");
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = name  + " :Name" + "\n" + description + " :Description" + "\n" + email + " :EMAIL";

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("apps@gmail.com",
                    "it works"));
            msg.addRecipient(Message.RecipientType.TO, new InternetAddress("testtesttest@gmail.com", "Your name"));
            msg.setSubject("Bestellung");
            msg.setText(msgBody);
            Transport.send(msg);

        } catch (Exception e) {
            resp.setContentType("text/plain");
            resp.getWriter().println("Something went wrong. Please try again.");
            throw new RuntimeException(e);
        }

        resp.setContentType("text/plain");
        resp.getWriter().println(
                "Thank you for your feedback. An Email has been send out.");
    }
}
4

1 回答 1

1

从您web.xml看来,url 模式存在一些问题,因此只有它给出 404 错误。更改<form action="feedback" method="post"><form action="/wunderapps_mail_service/mail" method="post">或者,您也可以更改 web.xml。

<servlet-mapping>
    <servlet-name>FeedbackServlet</servlet-name>
    <url-pattern>/feedback</url-pattern>
</servlet-mapping>
于 2012-07-13T15:26:41.310 回答