1. 程式人生 > >【Servlet】如何隱藏.jsp字尾或是更改字尾名

【Servlet】如何隱藏.jsp字尾或是更改字尾名

【需求描述】

    因為某些原因,我們討厭看到.jsp的結尾作為一個網頁的字尾名。當然,我們同樣不希望看到.html,我們希望簡潔好記,比如說,如果是hello.jsp或者hello.html我們只需要顯示hello就好了。

       這個就類似於struts2框架中所有的action預設是.action字尾,我們通過修改配置檔案 struts.xml可以修改為沒有後綴或者是其他任何字尾,比如說“.php",".asp"都可以。大笑

        當然,springMVC也同樣可以做到。

        言歸正傳,那當我們使用Servlet的時候,應該怎麼做呢?

【程式碼實現】(以eclipse IDE為例)

    1. 新建專案

    2. 新建一個index.jsp(或者index.html),還有一個hello.jsp(或hello.html)。

        此步驟需要注意index.jsp(或者index.html)我們並不做任何操作,直接讓它在載入的時候就跳轉到某個Servlet,這個例子之所以這樣做是為了效果顯示更快。

        程式碼如下:

        index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0;url=index"> 
<title>Welcome to Smileyan</title>
</head>
<body>

</body>
</html>


        hello.html

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Hello Servlet</title>
	<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">  
	<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
	<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
	<div class="jumbotron">
		<h1>歡迎訪問此頁面!</h1>
		<p>這是一個超大螢幕。</p>
		<p><a class="btn btn-primary btn-lg" role="button">
			學習更多</a>
		</p>
	</div>
</div>

</body>
</html>

        3. 新建Servlet

        需要說明的是,eclipse(只要不是太老的版本),自帶了註解方式,所以新建之後會在class名上一行會有註解,修改這個註解就可以修改訪問路徑,一次是值得推崇的。但是myeclipse和Idea並沒有自帶這個功能,因此需要自己在web.xml配置中修改一下同樣不麻煩。

package cn.smileyan.demos.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 HelloServlet
 */
@WebServlet("/index")
public class HelloServlet 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 {
		RequestDispatcher rs = request.getRequestDispatcher("hello.html");
		rs.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

    4.  執行後預設跳轉到index.html,然後會跳轉到訪問路徑index的Servlet,然後顯示的介面卻是hello.html的介面,瀏覽器上面的路徑是servlet的訪問路徑,也就是 index。

   【特別說明】我們可以通過這種方式,模擬成其他語言的網頁,比如我們把index改成index.php,訪問路徑和index.html的跳轉路徑都改成這個,就可以分方便的實現最後訪問路徑成了,index.php。

【總結】

       也就是說,我們可以通過這種方式來隱藏.jsp或是.html的後罪名,我們可以讓所有的頁面顯示都交付給servlet,當然,這樣做也就意味著需要新建很多servlet,如果需要這樣做的也不錯。

        同樣的道理,我們以後任何想要顯示的介面都可以用這種方式,來取出字尾名。