java Servlet 映射不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16337690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:41:17  来源:igfitidea点击:

Servlet mapping is not working

javajspservletsweb.xml

提问by Edwin Alex

I have created a simple program by using JSP and Servlets. After all, i have set and mapped my servlet in web.xml like below. But i am getting blank page always.

我使用 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>

My JSP file looks like this.

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

Any exampleServlet is,

任何 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);
    }
}

My file Structure is,

我的文件结构是,

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

Servlet   --> tomcat/webapps/application/WEB-INF/classes/exampleServlet.class

Where i went wrong? What is the mistake i have made? Can you please suggest me?

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

EDIT :I am posting my form elements to that servlet. By that time it passes the URL like this http://localhost:8080/application/exampleServlet

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

回答by Paulius Matulionis

Everything is fine in your application. You are getting blank page because your doPostmethod doesn't do anything. It prints the value only to the console output.

在您的应用程序中一切正常。你得到空白页,因为你的doPost方法没有做任何事情。它仅将值打印到控制台输出。

Change it to for e.g.:

将其更改为例如:

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

And then take a look if somethingappears in the browser.

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

回答by NilsH

If the "error" happens when you submit the form, it's because your servlet doesn't render any output, nor return any respons at all. In your doPostyou're supposed to write a response back to the browser, or hand over the request to something that does.

如果在您提交表单时发生“错误”,那是因为您的 servlet 不呈现任何输出,也不返回任何响应。在您的应用程序中,doPost您应该将响应写回浏览器,或者将请求移交给执行的操作。

public void doPost(HttpServletRequest req, HttpServletResponse res) {
    PrintWriter out = res.getWriter();
    out.println("<html><body>Hello world</body></html>");
}

would at least print something, though nothing very useful.

至少会打印一些东西,虽然没什么用。

The "proper" thing to do would be to redirect or include a view that does the rendering.

“正确”的做法是重定向或包含执行渲染的视图。

回答by ajduke

Try to use the different servlet-clasname than url-pattern.

尝试使用不同的servlet-clas名称url-pattern.

something like following or vice-versa

像跟随或反之亦然

  <servlet>
            <servlet-name>example</servlet-name>
            <servlet-class>exampleServlet</servlet-class>
    </servlet>
    <servlet-mapping>
            <servlet-name>example</servlet-name>
            <url-pattern>/egServlet</url-pattern>
    </servlet-mapping>

after the change restart the server

更改后重启服务器

回答by Vineet Singla

Probably your request doesnt matches the URL pattern. Try like this :

可能您的请求与 URL 模式不匹配。像这样尝试:

<url-pattern>/exampleServlet/*</url-pattern>