[英]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>
action="FindEvenOdd"
to action="Result"
action="FindEvenOdd"
更改为action="Result"
<url-pattern>
to Result <url-pattern>
更改为Result 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: 一些注意事项:
center
is a tag that shouldn't be used for a long time, and your select box is invalid HTML. center
是一个不应长时间使用的标记,并且您的选择框是无效的HTML。 将网址格式更改为/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.