Java Servlet URL 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5202249/
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-30 09:55:44  来源:igfitidea点击:

Java Servlet URL Mapping

javaservlets

提问by nicholas

i fairly new to java servlet.

我对 java servlet 相当陌生。

Please forgive me if this question seems stupid.

如果这个问题看起来很愚蠢,请原谅我。

I have add servlet mapping such as servlet class and url pattern using netbeans in web.xml servlet tab but when the application was running it does not direct go into the target url which is http://localhost:8080/HelloDuke2/greetingbut http://localhost:8080/HelloDuke2/

我在 web.xml servlet 选项卡中使用 netbeans 添加了 servlet 映射,例如 servlet 类和 url 模式,但是当应用程序运行时,它不会直接进入目标 url,即http://localhost:8080/HelloDuke2/greetinghttp ://localhost:8080/HelloDuke2/greeting ://localhost:8080/HelloDuke2/

What am i missing in the configuration ?

我在配置中缺少什么?

How to set the start up url to http://localhost:8080/HelloDuke2/greeting?

如何将启动 url 设置为http://localhost:8080/HelloDuke2/greeting

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>GreetingDukeServlet</servlet-name>
<servlet-class>HelloDuke.GreetingDukeServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ResponseDukeServlet</servlet-name>
<servlet-class>HelloDuke.ResponseDukeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ResponseDukeServlet</servlet-name>
<url-pattern>/ResponseDukeServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>GreetingDukeServlet</servlet-name>
<url-pattern>/GreetingDukeServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>

Please help.

请帮忙。

Thanks.

谢谢。

EDIT:

编辑:

I have tried the solution provided by Alexey Sviridov but it doesn't works where the browser report http status 404 resource is not available.

我已经尝试了 Alexey Sviridov 提供的解决方案,但它在浏览器报告 http 状态 404 资源不可用的情况下不起作用。

web.xml

网页.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <servlet>
        <servlet-name>GreetingDukeServlet</servlet-name>
        <servlet-class>HelloDuke.GreetingDukeServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>ResponseDukeServlet</servlet-name>
        <servlet-class>HelloDuke.ResponseDukeServlet</servlet-class>
        <init-param>
            <param-name>Message</param-name>
            <param-value>Hello, PeterWkc</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>GreetingDukeServlet</servlet-name>
        <url-pattern>/GreetingDukeServlet</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ResponseDukeServlet</servlet-name>
        <url-pattern>/ResponseDukeServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>
</web-app>

index.xhtml

索引.xhtml

> <?xml version='1.0' encoding='UTF-8'
> ?> <!DOCTYPE html PUBLIC "-//W3C//DTD
> XHTML 1.0 Transitional//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
> <html
> xmlns="http://www.w3.org/1999/xhtml"
>       xmlns:h="http://java.sun.com/jsf/html">
>     <h:head>
>         <title>Hello Duke</title>
>         <meta http-equiv="REFRESH" content="0; url=GreetingDukeServlet/">
>             
>         </meta>
>         
> 
>     </h:head>
>     <h:body>
>         Hello from Facelets
> 
> 
> 
>     </h:body> </html>

Please help.

请帮忙。

Thanks.

谢谢。

回答by BalusC

I see a Facelets page and I see a JSF specific <context-param>, but I don't see the JSF FacesServletbeing definied in web.xml, yet you're fiddling with other servlets.

我看到了一个 Facelets 页面,我看到了一个特定于 JSF 的<context-param>,但我没有看到 JSFFacesServlet被定义在 中web.xml,但您正在摆弄其他 servlet。

Aren't you mixing the basic concepts/technologies? Admittedly, the JSP tutorial is missingin Java EE 6 tutorial, but to work with plain vanilla servlets, you'd usually use plain HTML or JSP instead of Facelets.

你不是混合了基本概念/技术吗?诚然,Java EE 6 教程中缺少JSP 教程,但是要使用普通的 vanilla servlet,您通常会使用普通的 HTML 或 JSP 而不是 Facelets。

Anyway, to invoke a servlet by URL, you need to ensure that the URL matches the <url-pattern>of the servlet as is been definied in the web.xml. You've definied your GreetingDukeServletto listen on URLs matching /GreetingDukeServlet. So the URL has to be http://localhost:8080/HelloDuke2/GreetingDukeServletinstead.

无论如何,要通过 URL 调用 servlet,您需要确保 URL 与<url-pattern>.xml 文件中定义的 servlet匹配web.xml。您已定义GreetingDukeServlet要侦听匹配/GreetingDukeServlet. 所以 URL 必须是http://localhost:8080/HelloDuke2/GreetingDukeServlet

If you actually want the URL to be http://localhost:8080/HelloDuke2/greetinginstead, then you should change the <url-pattern>to /greetinginstead.

如果您确实希望 URL 为http://localhost:8080/HelloDuke2/greeting,那么您应该<url-pattern>/greeting改为 。

See also:

也可以看看:

回答by Alexey Sviridov

Yes, you need define index page for your web app and you can make redirect inside f.e. create page index.html with this contect

是的,您需要为您的网络应用程序定义索引页面,并且您可以使用此内容在 fe 创建页面 index.html 中进行重定向

<html><head><meta http-equiv="REFRESH" content="0; url=greeting/"></head></html>

and then in web.xml

然后在 web.xml

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>