有关getServletContext

lyj57 2013-04-24 11:52:27
public void parseRequest(HttpServletRequest request){
System.out.println("parseRequest:"+request.getParameter("username"));
System.out.println("realPath:"+request.getServletContext().getRealPath("/"));
path=request.getServletContext().getRealPath("/test_db.accdb");

ConnectAccessFile(request.getParameter("username"));

}
上面是TestAction.java中的几句,运行提示错误
The method getServletContext() is undefined for the type HttpServletRequest
The method getServletContext() is undefined for the type HttpServletRequest

求助啊!这要怎么解决
...全文
272 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
还原本质 2015-05-18
  • 打赏
  • 举报
回复
引用 3 楼 jia20003 的回复:
request.getSession().getServletContext().getRealPath("/"));
学习了,谢谢
菖蒲老先生 2013-04-24
  • 打赏
  • 举报
回复
压根就没这方法,你代码编译不报错么。。。
  • 打赏
  • 举报
回复
getServletContext() 这个方法在Servlet 3.0规范里才有,你是tomcat6?要tomcat7才行的
gloomyfish 2013-04-24
  • 打赏
  • 举报
回复
request.getServletContext().getRealPath("/")
在高版本的servlet中已经不支持这个API了
gloomyfish 2013-04-24
  • 打赏
  • 举报
回复
request.getSession().getServletContext().getRealPath("/"));
protected void doPost HttpServletRequest request HttpServletResponse response throws ServletException IOException { String action request getParameter "action" ; Teacher t new Teacher ; t setUsername request getParameter "username" ; if "register" equals action { t setPassword MD5 generateMD5 request getParameter "password" ; if teacherDao add t >0 { request getSession setAttribute "mess" "恭喜 注册成功 " ; response sendRedirect "login jsp" ; }else{ response sendRedirect "error jsp" ; } }else if "login" equals action { t setPassword MD5 generateMD5 request getParameter "password" ; if teacherDao queryByTeacher t size >0 { Constants ONLINE COUNT++; Object oCount getServletContext getAttribute "count" ; if oCount null { getServletContext setAttribute "count" 1 ; }else{ getServletContext setAttribute "count" Integer getServletContext getAttribute "count" +1 ; } request getSession setAttribute "teacher" t ; request getSession setAttribute "students" studentDao queryStudentsByTeacher t ; if request getParameter "autoLogin" null { Cookie cookie1 new Cookie "username" t getUsername ; Cookie cookie2 new Cookie "password" t getPassword ; cookie1 setMaxAge 60 60 24 14 ; cookie2 setMaxAge 60 60 24 14 ; response addCookie cookie1 ; response addCookie cookie2 ; } response sendRedirect "index jsp" ; }else{ request setAttribute "mess" "用户名或密码错误 " ; request getRequestDispatcher "login jsp" forward request response ; } }else if "autoLogin" equals action { t setPassword request getParameter "password" ; if teacherDao queryByTeacher t size >0 { Constants ONLINE COUNT++; Object oCount getServletContext getAttribute "count" ; if oCount null { getServletContext setAttribute "count" 1 ; }else{ getServletContext setAttribute "count" Integer getServletContext getAttribute "count" +1 ; } request getSession setAttribute "teacher" t ; request getSession setAttribute "students" studentDao queryStudentsByTeacher t ; response sendRedirect "index jsp" ; }else{ request setAttribute "mess" "用户名或密码错误 " ; Cookie[] cookies request getCookies ; if cookies null { for Cookie cookie : cookies { if "username" equals cookie getName || "password" equals cookie getName { cookie setValue null ; 关键 cookie setMaxAge 0 ; response addCookie cookie ; } } } request getRequestDispatcher "login jsp" forward request response ; } }else if "loginOut" equals action { request getSession invalidate ; response sendRedirect "login jsp" ; } }">protected void doPost HttpServletRequest request HttpServletResponse response throws ServletException IOException { String action request getParameter "action" ; Teacher t new Teacher ; t setUsername request getParameter "username" ; if "register" equals ac [更多]

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧