I'm doing a project using Java servlets. I have to include code in an already functioning site. I'm using Netbeans and the server is 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. 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);
the url is not forwarded and I get a 404 error when the url exists.
I can see in the displayProducts() method, you have defined the url as follows:
String url= "/maint/products.jps";
shouldn't that be a typo??
String url= "/maint/products.jsp";
file extension is wrong right?