[英]how can I use doGet response inside a method?
我需要在硒內使用response.flushbuffer方法。
我的密碼
static PrintWriter writer;
static int timer = 0;
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
{
runDriver("radio", "click", "complete");
}
public static void runDriver(String col1, String Col2, String col3)
{
WebElement ack1 = driver.findElement(By.id("represent"));
try
{
ack1.click();
String click1 = "<tr><td>" + col1 + "</td><td>" + col2 + "</td><td>" + col3 + "</td></tr>";
writer.println(click1);
response.flushBuffer(); // Won't let me put this here!
Thread.sleep(timer);
}
catch(InterruptedException e)
{
writer.println( e+" ID:21");
}
}
我要嘗試的是將Webdriver的相同操作隔離到一種方法,這樣我就不必重復它。 我也嘗試過這樣做。
static PrintWriter writer;
static int timer = 0;
protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
{
String radio1 = "Radio";
String clicked = "Click";
String complete = " Complete";
top(radio1, clicked, complete);
response.flushBuffer();
bottom();
}
public static void top(String col1, String col2, String col3)
{
writer.println("<tr><td>" + col1 + "</td><td>" + col2 + "</td><td>" + col3 + "</td></tr>");
}
public static void bottom()
{
try
{
Thread.sleep(timer);
}
catch(Exception e)
{
writer.println( "error: " + e);
}
}
但是它給了我一個NullPointerException。 我需要使用response.flushBuffer()的原因是,這樣用戶可以看到發生的過程。 否則它將完成該過程,然后輸出文本。
更新**
我修復了NPE。 事實證明,我仍然在doget方法中聲明了Printer writer。 我仍然可以在doget方法之外獲取response.flushbuffer。
首先,請注意,Servlet內部的全局變量由所有請求共享,並且可能導致線程安全問題。 除了一些用例(例如全局計數器),在servlet中使用它們幾乎總是一個壞主意。
為什么不簡單地將response
對象傳遞給top()
方法? 例如:
public static void top(String col1, String col2, String col3, HttpServletResponse response)
{
PrintWriter writer = response.getWriter();
writer.println("<tr><td>" + col1 + "</td><td>" + col2 + "</td><td>" + col3 + "</td></tr>");
response.flushBuffer();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.