在URL重寫中,我們將令牌或識別符號附加到下一個Servlet或下一個資源的URL。可以使用以下格式傳送引數名稱/值對:
http://localhost/myappurl?key1=value1&key2=value2&??...
使用=
符號分隔名稱和值,使用&
符號將引數名/值對與其他引數分開。當使用者單擊超連結時,引數名稱/值對將被傳遞到伺服器。 從Servlet中可以使用getParameter()
方法獲取指定引數的值。
網址重寫的優點
URL重寫的缺點
在這個例子中,我們使用連結來維護使用者的狀態。為此,在查詢字串中附加使用者的名稱,並從另一頁面的查詢字串中獲取該值。
開啟Eclipse,建立一個動態Web專案:UrlRewriting,其完整的結構如下所示 -
下面是此專案中幾個重要的檔案程式碼。
檔案:index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>URL重寫</title>
</head>
<body>
<div style="text-algin: center; padding-top: 12px;">
<form action="servlet1" method="get">
名字:<input type="text" name="username" value="maxsu"/><input type="submit"
value="提交" />
</form>
</div>
</body>
</html>
檔案:FirstServlet.java
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FirstServlet
*/
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String n = request.getParameter("username");
out.print("您好, " + n + "<br/><hr/>");
// appending the username in the query string
out.print("<a href='servlet2?uname=" + n + "'>使用URL引數存取使用者名</a>");
out.close();
}
}
檔案:SecondServlet.java
package com.yiibai;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FirstServlet
*/
@WebServlet("/FirstServlet")
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//getting value from the query string
String n=request.getParameter("uname");
out.print("Hello "+n);
out.close();
}
}
檔案:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>UrlRewriting</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>com.yiibai.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>com.yiibai.SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
在編寫上面程式碼後,部署此Web應用程式,開啟瀏覽器存取URL: http://localhost:8080/UrlRewriting ,如果沒有錯誤,應該會看到以下結果 -
填寫使用者名後點選提交表單,應該會看到以下結果 -
注意: 在URL中,附加了引數」username=maxsu」 來區別使用者。
再次點選「使用URL引數存取使用者名」連結,看到顯示的使用者名:maxsu
-