繁体   English   中英

执行servlet后,如何控制浏览器地址栏中生成的地址?

[英]How do I control the address generated in address bar of browser after executing a servlet?

作为练习,我编写了一个简单的网页,它将向用户显示一个下拉菜单。 用户将选择一个号码,然后单击“ Submit按钮。

之后,将执行servlet,并将发送回偶数列表。

在此处输入图片说明

查看Web浏览器地址栏中的地址,它是:

http://localhost:8080/FindEvenOdd/FindEvenOdd

我希望它是http://localhost:8080/FindEvenOdd/Result

我怎么做 ?
我的DD看起来像这样:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    web-app_2_4.xsd"
    version="2.4">

    <servlet>
        <servlet-name>Evens</servlet-name>
        <servlet-class>FindIt</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Evens</servlet-name>
        <url-pattern>/FindEvenOdd</url-pattern>
    </servlet-mapping>
</web-app>  

我的HTML:

<html>
    <head>
        <title> List of Even/Odd Numbers </title>
    </head>

    <body>
        <form method="POST" action="FindEvenOdd">
        <center>
            <select name="number" size="1">
                <option> <50
                <option> <100
                <option> <150
            </select>
        </center>
            <center>
                <input type="SUBMIT">
            </center>
        </form>
    </body>
</html>  

我尝试了什么

  • 我将action="FindEvenOdd"更改为action="Result"
  • 我将<url-pattern>更改为Result

  • 我同时完成了上述两件事,但得到了404。
    所以,
    我该怎么做才能获得http://localhost:8080/FindEvenOdd/Result

    您尝试过的几乎是正确的。 url-pattern必须设置为/Result

    一些注意事项:

    • 始终将您的课程放在一个包中。 不在默认软件包中
    • 生成有效的HTML5。 center是一个不应长时间使用的标记,并且您的选择框是无效的HTML。
    • 了解Servlet 3.0,而不是Servlet 2.4。

    将网址格式更改为/FindEvenOdd/Result并尝试

    为了更改URL,您需要在servlet中进行重定向。

    String contextPath = request.getContextPath();
    response.sendRedirect(response.encodeRedirectURL(contextPath + "/Result") );
    

    暂无
    暂无

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM