我如何更改任意 java class 中的会话变量的值
How can i change the value of a session variable in an arbitrary java class
我有一个 java servlet,它设置一个会话变量并调用一个启动线程 class.I 下面是一个实现
@WebServlet("/ExportLogs")
public class ExportLogs extends HttpServlet
{
public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException,IOException
{
Integer completePercent = new Integer(10);
request.getSession().setAttribute("CompletionStatus" , completePercent);
LogExportingProcess export = new LogExportingProcess();
export.start();
}
}
我有一个线程 class 执行如下长过程;
class LogExportingProcess extends Thread
{
public void run()
{
//i want to change the value of the percent complete variable in here.
}
}
现在我想更改 LogExportingProcess 中的 completePercent 值 class.How 我可以实现吗?
创建 LogExportingProcess
时必须传递 Session
对象
class LogExportingProcess extends Thread
{
private HttpSession session;
public LogExportingProcess(HttpSession session) {
this.session = session;
}
public void run()
{
session.setAttribute("CompletionStatus" , completePercent);
}
}
和 ExportLogs
class
中的一项更改
LogExportingProcess export = new LogExportingProcess(request.getSession());
整数不可变。 AtomicInteger 将是一个不错的替代品。
@WebServlet("/ExportLogs")
public class ExportLogs extends HttpServlet
{
public void doGet( final HttpServletRequest request , final HttpServletResponse response ) throws ServletException,IOException
{
final AtomicInteger completePercent = new AtomicInteger(10);
request.getSession().setAttribute("CompletionStatus" , completePercent);
final LogExportingProcess export = new LogExportingProcess( completePercent );
export.start();
}
}
class LogExportingProcess extends Thread
{
final AtomicInteger completePercent;
public LogExportingProcess( final AtomicInteger completePercent )
{
this.completePercent = completePercent;
}
public void run()
{
completePercent.set( 80 ); //80% complete, substitute with real code
}
}
恕我直言,这比 Yogesh Badke 建议的持有对 HttpSession 对象的引用更可取,因为 HttpSession 可以在超时时正常被垃圾收集,并且 AtomicInteger 通过仅共享百分比而不是整个会话信息来增加封装。
我有一个 java servlet,它设置一个会话变量并调用一个启动线程 class.I 下面是一个实现
@WebServlet("/ExportLogs")
public class ExportLogs extends HttpServlet
{
public void doGet(HttpServletRequest request , HttpServletResponse response) throws ServletException,IOException
{
Integer completePercent = new Integer(10);
request.getSession().setAttribute("CompletionStatus" , completePercent);
LogExportingProcess export = new LogExportingProcess();
export.start();
}
}
我有一个线程 class 执行如下长过程;
class LogExportingProcess extends Thread
{
public void run()
{
//i want to change the value of the percent complete variable in here.
}
}
现在我想更改 LogExportingProcess 中的 completePercent 值 class.How 我可以实现吗?
创建 LogExportingProcess
Session
对象
class LogExportingProcess extends Thread
{
private HttpSession session;
public LogExportingProcess(HttpSession session) {
this.session = session;
}
public void run()
{
session.setAttribute("CompletionStatus" , completePercent);
}
}
和 ExportLogs
class
LogExportingProcess export = new LogExportingProcess(request.getSession());
整数不可变。 AtomicInteger 将是一个不错的替代品。
@WebServlet("/ExportLogs")
public class ExportLogs extends HttpServlet
{
public void doGet( final HttpServletRequest request , final HttpServletResponse response ) throws ServletException,IOException
{
final AtomicInteger completePercent = new AtomicInteger(10);
request.getSession().setAttribute("CompletionStatus" , completePercent);
final LogExportingProcess export = new LogExportingProcess( completePercent );
export.start();
}
}
class LogExportingProcess extends Thread
{
final AtomicInteger completePercent;
public LogExportingProcess( final AtomicInteger completePercent )
{
this.completePercent = completePercent;
}
public void run()
{
completePercent.set( 80 ); //80% complete, substitute with real code
}
}
恕我直言,这比 Yogesh Badke 建议的持有对 HttpSession 对象的引用更可取,因为 HttpSession 可以在超时时正常被垃圾收集,并且 AtomicInteger 通过仅共享百分比而不是整个会话信息来增加封装。