【问题标题】:How to remove hard coded values from JSP and achieve redirection in one line of code?如何从 JSP 中删除硬编码值并在一行代码中实现重定向?
【发布时间】:2019-03-06 12:26:26
【问题描述】:

我正在尝试从 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 条件下实现?

【问题讨论】:

  • 如果我的回答对您有帮助,请标记为已接受。
  • 即javascript,techid必须在java端的表单中,设置window.location可能意味着转发/重定向到另一个页面。很难理解你想要达到的目标。稍微解释一下。
  • @JoopEggen,你说得对,我正在尝试实现条件是如果名称是 google 我需要将它们重定向到 Google/ 页面,如果名称是 yahoo 我需要重定向到 yahoo/页。如何在 JSP 中实现这一点?但在我的 JSP 代码中,我不应该硬编码名称的值。
  • @JoopEggen,您能否强调一些关于如何在不进行硬编码的情况下重写代码并实现重定向的建议。

标签: java jsp security config


【解决方案1】:

在包中创建一个扩展名为“.properties”的属性文件,并通过在jsp中导入资源包包来使用jsp文件中定义的那些属性。

config.properties

name=priya
email=priya@gmail.com
phone=22222

sample.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 %>">

【讨论】:

  • 谢谢! AbhiN ,但我的期望是通过从 config.properties 获取名称来实现 Url Navigation。我想在我给定的条件下使用。
【解决方案2】:

一个经典的 JSP。这里我使用%&gt;&lt;%,所以仍然没有输出,可以重定向到另一个页面。

  • 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 作为 controller,准备数据,然后作为 view 转发到任何 JSP,参数化数据(或重定向到某个外部 URL),model,会更好。

【讨论】:

  • 谢谢!但我不想向用户显示谷歌,如果我也想检查其他名称并分别导航,是否应该为另一个名称添加另一个 if 条件
  • 那是编程;你有什么数据,你想对这些数据做什么。 ,我不知道;您可能在 config.properties 中使用与可能的请求参数匹配的键进行查找。而所有这些情况,都不应该需要 if。
猜你喜欢
  • 2022-11-10
  • 1970-01-01
  • 2015-05-28
  • 1970-01-01
  • 2016-02-21
  • 2014-11-28
  • 1970-01-01
  • 2019-08-14
  • 1970-01-01
相关资源
最近更新 更多