Java 如何从JSP中删除硬编码值并在一行代码中实现重定向?
我试图从JSP文件中删除硬代码名。我需要在配置文件中给出名称,并在jsp中调用,这样当用户查看页面源代码时,他就不会看到名称。 我如何在JSP中实现它 我的代码:Java 如何从JSP中删除硬编码值并在一行代码中实现重定向?,java,jsp,security,config,Java,Jsp,Security,Config,我试图从JSP文件中删除硬代码名。我需要在配置文件中给出名称,并在jsp中调用,这样当用户查看页面源代码时,他就不会看到名称。 我如何在JSP中实现它 我的代码: if (((tech == 'google.com') && ((id == 'email') || id == 'domain'))) || ((tech == 'google') && id == 'test'))) window.location = (url.substring
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));
Config.properties
envs=google.com,yahho.com
name=谷歌、雅虎
tokenurl=google/,yahoo/
sample.jsp
如果((技术==env[0])&((id='email')| | id==''domain'))| |((技术)
==“名称[0]”和&id==“测试”))
window.location=(url.substring(0,res+5)+(“turls[0]”)+
子字符串(res+5,url.length));
如果((tech==env[1])&((id==email')|| id==domain'))|
((tech=='names[1]')和&id=='test'))
window.location=(url.substring(0,res+5)+(“turls[1]”)+
子字符串(res+5,url.length));
但我不确定这是一种正确的代码编写方式。有谁能建议一种适当的标准方法,我可以在一行if条件下实现吗?在扩展名为“.properties”的包中创建一个属性文件,并通过在jsp中导入资源包包来使用jsp中文件中定义的那些属性 配置属性
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 %>">
姓名:
电邮:
电话:
一个经典的JSP。这里我使用%>如果我的答案对您有帮助,请将其标记为已接受。也就是说,javascript、tech
和id
必须以java端的形式存在,并且设置window.location可能意味着向前/重定向到另一个页面。很难理解你想要实现什么。请解释一下。@JoopEggen,你说得对,我正在尝试实现的条件是,如果名称是google,我需要将它们重定向到google/page,如果名称是yahoo,我需要重定向到yahoo/page。如何在JSP中实现这一点?但是在我的JSP代码中,我不应该硬编码名称的值。@JoopEggen,请您强调一些关于如何在不硬编码的情况下重写代码并实现重定向的建议。谢谢!AbhiN,但我的期望是通过从config.properties获取名称来实现Url导航。我想在我给定的条件下使用。谢谢!但我不想向用户显示google,如果我想检查其他名称并分别导航,是否应该为正在编程的另一个名称添加另一个if条件;您拥有的数据以及您希望对这些数据执行的操作。一、 我不知道;您可能在config.properties
中有一种查找,其中的键与可能的请求参数匹配。在所有这些情况下,不应该需要国际单项体育联合会。
<%@ 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;
}
%>