java - Calling servlet from JSP -


Actually, I want to display products in an Arraylight on a JSP page. I have done that sublet code but there is no output.

Do I have to keep products.jsp in the / web-ins folder? When I do this, I get no request processing error.

MySuplet code (InventorySurlate.Java)

  Secure zero codeg (HTTPSERT request request), HTTPSvette Response response) Servlet Upption, IOException {// TODO auto-generated Gone method stub {list & lt; Products & gt; Products = new arrelisted & lt; Products & gt; (); Products = inventory. PopulateProducts (); // Get All Products Request.setAttribute ("Product", Product); // Request For Store Products Request.getRequestDispatcher ("/ products.jsp") Forwarded (Request, Feedback); // Next to the JSP page to display them in an HTML table. } Hold (exception before) {new service exception ("product reload failed!", Formerly); }}   

My JSP page (products.jsp)

  & lt; H2 & gt; Product List & lt; / H2 & gt; & Lt; Table & gt; & Lt; C: forEach item = "$ {products}" var = "product" & gt; & Lt; TR & gt; & Lt; TD & gt; $ {Product.Description} & lt; / TD & gt; & Lt; TD & gt; $ {Product.UnitPrice} & lt; / TD & gt; & Lt; / TR & gt; & Lt; / C: foreach & gt; & Lt; / Table & gt;   

Web.xml

  & lt; Web-App version = "3.0" xmlns = "http://java.sun.com/xml/ns/ Javaee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema -instance" xsi: Schema location = "http://java.sun.com/xml/ns/javaee http: // Java sun.com/xml/ns/javaee/web-app_3_0.xsd"> & Lt; Servlet & gt; & Lt; Servlet-name & gt; Inventory & lt; / Servlet-name & gt; & Lt; Servlet category & gt; Com.ShoppingCart.InventoryServlet & lt; / Servlet category & gt; & Lt; / Servlet & gt; & Lt; Servlet-mapping & gt; & Lt; Servlet-name & gt; Inventory & lt; / Servlet-name & gt; & Lt; URL pattern & gt; / Products & lt; / URL pattern & gt; & Lt; / Servlet-mapping & gt; & Lt; / Web application & gt;  

You have to open the page by requesting the sublet URL instead of the JSP URL. By keeping JSP in this will call doGet () method

/ WEB-INF effectively prevents endorsors from opening it directly DoGet () The files in the servlet's way / Web-INF are not publicly accessible, so if the pre-installation of the servlet is mandatory, then you need to do this. Place the JSP in the / WEB-INF folder and point to the requestdispatcher.

  request.getRequestDispatcher ("/ WEB-INF / products.jsp") .forward (request, response);   

But you need to replace all existing links which point to servlet URL instead of JSP URL.

Also see:


Comments