我有以下servlet:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MailServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String m = request.getParameter("mail");
String sub = request.getParameter("subject");
String messg = request.getParameter("message");
SendMail.send(m, sub, messg);
out.print("Your Mail has been sent successfully");
out.close();
}
}
我试图通过以下形式调用它:
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body bgcolor="pink">
<form action="MailServlet">
<table>
<tr>
<td>To:</td>
<td><input type="text" name="mail"/><br/></td>
</tr>
<tr>
<td>Subject:</td>
<td><input type="text" name="subject"><br/></td>
</tr>
<tr>
<td>Message Text:</td>
<td><textarea rows="12" cols="80" name="message"></textarea><br/></td>
</tr>
<tr>
<td><input type="submit" value="Send"></td>
<td><input type="reset" value="Reset"></td>
</tr>
</table>
</form>
</body>
但是,它返回HTTP状态404:
答案 0 :(得分:1)
需要映射Servlet才能访问它们。
如果您使用的是Servlet 3.0,则可以使用:
@WebServlet("/MailServlet") // /MailServlet is the partial URL of the servlet.
public class MailServlet extends HttpServlet {
[...]
}
如果您没有使用Servlet 3.0,则必须在web.xml
中映射servlet,方法是将这些行放在webapp
元素中:
<servlet>
<servlet-name>MailServlet</servlet-name>
<servlet-class>com.yourpackage.MailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MailServlet</servlet-name>
<url-pattern>/MailServlet</url-pattern>
</servlet-mapping>