亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從 JSP 中刪除硬編碼值并在一行代碼中實現重定向?

如何從 JSP 中刪除硬編碼值并在一行代碼中實現重定向?

開心每一天1111 2022-07-20 19:25:38
我正在嘗試從 JSP 文件中刪除硬編碼名稱。我需要在 Config 文件中給出名稱并在 jsp 中調用,這樣當用戶確實查看頁面源時,他不應該看到這些名稱。我怎樣才能在 JSP 中做到這一點。我的代碼: if (((tech == 'google.com') && ((id == 'email') || id == 'domain'))) || ((tech == 'google') && id == 'test')))        window.location = (url.substring(0, res + 5) + ("google/") + url.substring(res + 5, url.length));現在如何刪除硬編碼的值并在配置文件中給出它并在此處調用它,以便當我查看頁面源時我不應該看到 google.com 名稱我的新代碼嘗試: Config.properties envs=google.com,yahho.com name= google,yahoo tokenurl=google/,yahoo/ sample.jsp <%@ page import = "java.util.ResourceBundle" %> <% ResourceBundle resource = ResourceBundle.getBundle("config"); String names=resource.getString("name"); String env=resource.getString("envs"); String turls=resource.getString("tokenurl"); %> if (((tech == env[0]) && ((id == 'email') || id == 'domain'))) || ((tech  == 'names[0]') && id == 'test'))) window.location = (url.substring(0, res + 5) + ("turls[0]") +  url.substring(res + 5, url.length)); else if (((tech == env[1]) && ((id == 'email') || id == 'domain'))) ||  ((tech == 'names[1]') && id == 'test')))    window.location = (url.substring(0, res + 5) + ("turls[1]") +   url.substring(res + 5, url.length));但我不確定這是編寫代碼的正確方法。誰能建議我可以遵循的適當標準方法來僅在一條 if 條件下實現?
查看完整描述

2 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊

在包中創建一個擴展名為“.properties”的屬性文件,并通過在 jsp 中導入資源包包來使用在 jsp 文件中定義的那些屬性。


config.properties


name=priya

[email protected]

phone=22222

示例.jsp


<%@ page import = "java.util.ResourceBundle" %>


 <% ResourceBundle resource = ResourceBundle.getBundle("config");

    String name=resource.getString("name");

    String email=resource.getString("email");

    String phone=resource.getString("phone");

     %>


    Name:  <input type="text" id="name" value="<%=name %>">

    Email: <input type="text" id="email" value="<%=email %>">

    Phone: <input type="text" id="phone" value="<%=phone %>">


查看完整回答
反對 回復 2022-07-20
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

一個經典的 JSP。在這里我使用%><%所以仍然沒有輸出被寫入,并且可以重定向到另一個頁面。


HTTP 首先發送一些標題行,一個空行,然后是可選的 HTML 內容。進行重定向將是標題行。所以仍然沒有內容必須由 JSP 編寫。標題行可以說明使用的字符集/編碼、cookie 等。

所以:


<%@ page import = "java.util.ResourceBundle"

%><%

   ResourceBundle resource = ResourceBundle.getBundle("config");

   String names = resource.getString("name");

   String[] env = resource.getString("envs").split(",\\s*");

   String turls = resource.getString("tokenurl");


   String tech = request.getParameter("tech");

   if (tech != null && tech.equals("google")) {

       String url = response.encodeRedirectURL(env[13]);

       response.sendRedirect(url); // Just tell the browser to redirect, load the url.

       return;

   }

%>

不幸的是,實際的邏輯是你的事。與 JavaScript 相比,s == 'abc'有s.equals("abc").


學習和使用 JSP 標記和 EL(表達式語言)將使代碼更小。


使用 servlet 作為控制器,準備數據,然后作為視圖轉發到任何 JSP ,使用數據參數化(或重定向到某些外部 URL),模型會更好。


查看完整回答
反對 回復 2022-07-20
  • 2 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號