使用请求调度程序,我将 Controller 包中的 Servlet 类的请求转发到 bean 包中的类。这是我的代码..
private void credentialProcessing(HttpServletRequest request,
HttpServletResponse response) {
try{
RequestDispatcher rd=request.getRequestDispatcher("algo");//this class is in bean package
rd.forward(request, response);
}
catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
错误: 每当我尝试执行此操作时,Tomcat 都会给我错误消息,提示找不到请求的资源。另一方面,如果我访问控制包类,它就会被访问。
最佳答案
RequestDispatcher
使用路径转发请求,它不允许您指定转发请求的类。
getRequestDispatcher
Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response. The resource can be dynamic or static.
如果您想从 servlet 访问 algo
类,请在 servlet 中创建该类的实例。
private void credentialProcessing(HttpServletRequest request,
HttpServletResponse response) {
try{
algo al = new algo();
al.callSomeMethod();
}
catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
旁注
查看 Java 类的命名约定,它要求类名大写。
关于java - 无法从 Controller 包类访问 bean 包类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740363/