DataSource ds = (DataSource)getServletContext().getAttribute("")找不到getServlet
我在测试监听器,结果DataSource ds = (DataSource)getServletContext().getAttribute("dataSource");这里面getServletContext()下面有红线,提示错误,请问该怎么解决?
这个测试类完整代码是:
package listener;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class TestLintener {
public void test (HttpServletRequest request,HttpServletResponse response)
{
response.setContentType("text/html;charSet=UTF-8");
response.setCharacterEncoding("UTF-8");
try {
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
DataSource ds = (DataSource)getServletContext().getAttribute("dataSource");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
------解决方案--------------------
public class TestLintener这行是不是少了点什么?