[英]Forward(request,response) doesn't forward the URL
I'm doing a project using Java servlets. 我正在使用Java servlet进行项目。 I have to include code in an already functioning site. 我必须在已经正常运行的站点中包含代码。 I'm using Netbeans and the server is Tomcat. 我正在使用Netbeans,服务器是Tomcat。 The code that I added is very similar to some parts of the code of the site. 我添加的代码与站点代码的某些部分非常相似。 I had to create a new controller that reads from a database and display, add, update and delete information. 我必须创建一个新的控制器,该控制器从数据库中读取并显示,添加,更新和删除信息。 The site was functioning with different servlets that we created but a requisite for the project is to create the controller servlet. 该站点使用我们创建的不同servlet起作用,但是该项目的必要条件是创建控制器servlet。 This is part of the code of the controller: 这是控制器代码的一部分:
public class MaintController extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
String requestURI = request.getRequestURI();
String url = "/maint";
if (requestURI.endsWith("/displayProducts")) {
url = displayProducts(request, response);
} else if (requestURI.endsWith("/addProduct")) {
url = addProduct(request, response);
} else if (requestURI.endsWith("/editProduct")) {
url = editProduct(request, response);
} else if (requestURI.endsWith("/deleteProduct")){
deleteProduct(request, response);
}
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
private String displayProducts(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
HttpSession session = request.getSession();
List<Product> products = ProductDB.selectProducts();
session.setAttribute("products", products);
out.println(products);
String url= "/maint/products.jps";
return url;
}
The point is that debugging the site I can see that when entering an URL that finishes with /displayProducts
the displayProducts
function is accessed, the products are read and the URL is returned, but when the control goes to getServletContext().getRequestDispatcher(url).forward(request, response);
关键是调试该站点,我可以看到,输入以/displayProducts
的URL时,可以访问displayProducts
函数,读取产品并返回URL,但是当控件转到getServletContext().getRequestDispatcher(url).forward(request, response);
the url is not forwarded and I get a 404 error when the url exists. 该网址未转发,并且该网址存在时出现404错误。
I can see in the displayProducts() method, you have defined the url as follows: 我可以在displayProducts()方法中看到,您已经定义了如下网址:
String url= "/maint/products.jps";
shouldn't that be a typo?? 那不是错字吗?
String url= "/maint/products.jsp";
file extension is wrong right? 文件扩展名是错误的吧?
404 error indicates the requested page not found. 404错误表示找不到请求的页面。 your return url is 您的返回网址是
String url= "/maint/products.jps";
extension of the requested page is not correct. 请求页面的扩展名不正确。 It should be products.jsp 应该是products.jsp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.