2013-06-27 24 views
1

所以我的代碼在這裏我的用戶名參數值發送到我的index.jsp歡迎頁面:屏蔽用戶登錄後的用戶名在JDBC

response.sendRedirect("index.jsp?username=" + username); 

名稱與此顯示在我的index.jsp頁面:

<%= "Welcome " + request.getParameter("username")%> 

但是,該URL顯示這個信息是我不想要的東西:

http://localhost:8088/Trading_Platform_Web/index.jsp?username=ClayBanks1989 

的你的想法如何掩蓋這一點?

此外,更希望僅顯示我的數據庫的名字。但我們可以專注於手頭的任務。

回答

3

使用請求分派而不是重定向。

RequestDispatcher view = Request.getRequestDispatcher("index.jsp"); 
view.forward(request, response); 

這將轉發相同的請求對象的index.jsp。如果用戶名尚未請求參數把它作爲一個屬性

request.setAttribute("username", username); // before doing the forward 

,並在您的index.jsp檢索它作爲

<%= "Welcome " + request.getAttribute("username")%> 

另外,由於用戶登錄(通過JDBC進行後端身份驗證),您可以在HttpSession中保存用戶名(以及與他有關的其他信息)。現在

HttpSession session = request.getSession(); 
session.setAttribute("username", username); 

,您可以轉發(推薦)或選擇像以前那樣重定向但的index.jsp將變爲

<%= "Welcome " + session.getAttribute("username")%> 
6

使用轉發。這將使請求屬性可以傳遞給視圖,並且可以以ServletRequest#getAttribute的形式或使用表達式語言和JSTL的形式使用它們。簡短的例子

控制器(你的servlet)

request.setAttribute(username", username); 
RequestDispatcher dispatcher = servletContext().getRequestDispatcher("index.jsp"); 

dispatcher.forward(request, response); 

查看(你的JSP)。

<% 
out.println(request.getAttribute("username")); 
%> 

另一種選擇是使用會話變量:

//if request is not from HttpServletRequest, you should do a typecast before 
HttpSession session = request.getSession(false); 
//save message in session 
session.setAttribute(username", username); 
response.sendRedirect("index.jsp"); 

再賺回來

<% 
out.println(session.getAttribute("message")); 
session.removeAttribute("message"); 
%> 

同樣可以存儲在從數據庫會話變量的名字,你可以顯示它在您的網站上的任何地方,直到會議維持

相關問題