[英]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 http://localhost:8080/FindEvenOdd/Result
您嘗試過的幾乎是正確的。 url-pattern
必須設置為/Result
。
一些注意事項:
center
是一個不應長時間使用的標記,並且您的選擇框是無效的HTML。 將網址格式更改為/FindEvenOdd/Result
並嘗試
為了更改URL,您需要在servlet中進行重定向。
String contextPath = request.getContextPath();
response.sendRedirect(response.encodeRedirectURL(contextPath + "/Result") );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.