简体   繁体   English

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

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

I have written, for practice, a simple webpage that will display to the user a drop down menu. 作为练习,我编写了一个简单的网页,它将向用户显示一个下拉菜单。 The user will select a number and hit the Submit button. 用户将选择一个号码,然后单击“ Submit按钮。

After that, the servlet will be executed and will send back a list of even numbers. 之后,将执行servlet,并将发送回偶数列表。

在此处输入图片说明

Looking at the address in the address bar of the web browser, it is: 查看Web浏览器地址栏中的地址,它是:

http://localhost:8080/FindEvenOdd/FindEvenOdd

I want it to be http://localhost:8080/FindEvenOdd/Result 我希望它是http://localhost:8080/FindEvenOdd/Result

How do I do that ? 我怎么做 ?
My DD looks like this: 我的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>  

my HTML: 我的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>  

What I tried 我尝试了什么

  • I changed the action="FindEvenOdd" to action="Result" 我将action="FindEvenOdd"更改为action="Result"
  • i changed the <url-pattern> to Result 我将<url-pattern>更改为Result

  • I did the above two things simultaneously but got a 404. 我同时完成了上述两件事,但得到了404。
    So, 所以,
    What do I do to get http://localhost:8080/FindEvenOdd/Result 我该怎么做才能获得http://localhost:8080/FindEvenOdd/Result

    What you tried is almost correct. 您尝试过的几乎是正确的。 The url-pattern must be set to /Result . url-pattern必须设置为/Result

    Some notes though: 一些注意事项:

    • always put your classes in a package. 始终将您的课程放在一个包中。 Not in the default package 不在默认软件包中
    • generate valid HTML5. 生成有效的HTML5。 center is a tag that shouldn't be used for a long time, and your select box is invalid HTML. center是一个不应长时间使用的标记,并且您的选择框是无效的HTML。
    • Learn servlet 3.0 rather than servlet 2.4. 了解Servlet 3.0,而不是Servlet 2.4。

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

    In order to change URL, you need a redirect in the servlet. 为了更改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