I have an example using java.util.prefs.Preferences store and get value from registry in windows server, this is my class.
//File Winreg.java
package oneconect;
import java.util.prefs.*;
public class Winreg {
Preferences prefs;
String defaultValue = "NA";
String InstalledDate ="",version="";
//contrucsiton
public Winreg()
{
try
{
prefs= Preferences.userNodeForPackage(oneconect.Winreg.class);
}
catch (Exception E)
{
System.out.println(E);
}
}
//functio set value
public void setValue(String vKey,String vValue)
{
try
{
prefs.put(vKey,vValue);
}
catch (Exception E)
{
E.printStackTrace();
}
}
//function get value
public String getValue(String vKey,String defaultValue)
{
try
{
String vGetValues =prefs.get(vKey, defaultValue);
prefs.flush();
return vGetValues;
}
catch (Exception E)
{
E.printStackTrace();
return defaultValue;
}
}
}
Working property in java file class,
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
try {
Winreg iWin = new Winreg();
iWin.setValue("filepath", "Program file") ;
System.out.println( iWin.getValue("filepath", null) ); /// out put Program file
} catch (Exception exp) {
System.out.print(exp.toString());
}
}
But when i want to this method in jsp file, server Tomcat 6.0
<%@page import="oneconect.Winreg"%>
<%@page import="java.util.prefs.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>..:: Test a</title>
</head>
<body>
Winreg iWin = new Winreg();
out.println( iWin.getValue("filepath", null) );
%>
</body>
</html>
method getValue do not return value stored before, out put always return null (default value).
Please give me what we wrong, and how to fix this case ?
Thanks,
You installed it using apache-tomcat-6.0.xx.exe and set it as a Windows service.
When Tomcat is a Windows service, its default user execution account is not the account you currently log into the system.
S1
S3
It uses Local System account
as the user account to start Tomcat. Therefore, if you use your login account (XXX) to store the java.util.prefs settings, and use the user account Local System account
to read the java.util.prefs settings in Tomcat, it will not be read, because these are the java.util.prefs settings under two different user accounts.
Add index8.jsp to your webapp (assuming the name is hello),
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="oneconect.Winreg"%>
<%@page import="java.util.prefs.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Greeting JSP</title>
</head>
<body>
<%
Winreg iWin = new Winreg();
iWin.setValue("filepath", "Program file") ;
out.println( iWin.getValue("filepath", null) ); /// out put Program file
%>
</body>
</html>
then open the Browser at http://localhost:8080/hello/index8.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="oneconect.Winreg"%>
<%@page import="java.util.prefs.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test a</title>
</head>
<body>
<%
Winreg iWin = new Winreg();
out.println( iWin.getValue("filepath", null) );
%>
</body>
</html>
then open the Browser at http://localhost:8080/hello/index7.jsp
If you do not use Windows services to start tomcat, but use C:\Program Files\Apache Software Foundation\Tomcat 6.0\startup.bat to start Tomcat, then open http://localhost:8080/hello/index7.jsp, the java.util.prefs settings it reads will be the java.util.prefs settings of your current user login account, which is what you execute the main method to set.
You installed it using apache-tomcat-6.0.xx.exe and set it as a Windows service.
When Tomcat is a Windows service, its default user execution account is not the account you currently log into the system.