Servlet映射不起作用

Edw*_*lex 3 java jsp web.xml servlets

我使用JSP和Servlets创建了一个简单的程序.毕竟,我已经在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>
Run Code Online (Sandbox Code Playgroud)

我的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>&nbsp;</td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="submit" value="Upload" /></td>
            </tr>
        </table>
    </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

任何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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的文件结构是,

JSP file  --> tomcat/webapps/application/index.jsp

Servlet   --> tomcat/webapps/application/WEB-INF/classes/exampleServlet.class
Run Code Online (Sandbox Code Playgroud)

哪里出错了?我犯了什么错误?你能建议我吗?

编辑:我将表单元素发布到该servlet.到那时它传递这样的URLhttp://localhost:8080/application/exampleServlet

Pau*_*nis 6

您的应用程序中的一切都很好.您将获得空白页面,因为您的doPost方法无法执行任何操作.它仅将值打印到控制台输出.

将其更改为例如:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter writer = response.getWriter();
    writer.print("something");
}
Run Code Online (Sandbox Code Playgroud)

然后看看是否something出现在浏览器中.