我使用 JSP 和 Servlet 创建了一个简单的程序。毕竟,我已经在 web.xml 中设置并映射了我的 servlet,如下所示。但我总是得到空白页。
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>exampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/exampleServlet</url-pattern>
</servlet-mapping>
我的 JSP 文件如下所示。
<html>
<head></head>
<body>
<form action ="exampleServlet" method="POST" enctype="multipart/form-data">
<table width="500" style="margin-top:100px;">
<tr>
<td>Subject</td>
<td><input type="text" name="subj" id="subj"/></td>
</tr>
<tr>
<td>Upload File</td>
<td><input type="file" name="upload_file" id="upload_file"/></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
任何 exampleServlet 都是,
import java.io.File;
import java.util.List;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class exampleServlet extends HttpServlet {
public void init() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sub = request.getParameter("subj");
System.out.println(sub);
}
}
我的文件结构是,
JSP file --> tomcat/webapps/application/index.jsp
Servlet --> tomcat/webapps/application/WEB-INF/classes/exampleServlet.class
我哪里出错了?我犯了什么错误?你能建议我吗?
编辑:我将表单元素发布到该 servlet。到那时它会像这样传递 URLhttp://localhost:8080/application/exampleServlet