在JSP Index =>中导入servlet;空参数

时间:2014-01-04 18:33:15

标签: java xml eclipse jsp servlets

我目前正在开展一个学校项目。我们必须用Java做一个小型的市场网站。

我有一个小问题。我有一个Index.jsp,我想要包含一个servlet(RandomArticle.jsp)。这个servlet有一个.jsp和一个.java,只是随机播放一个集合并返回名称。

当我显示index.jsp时,显示了RandomArticle.jsp的html(因此include是正确的),但返回的名称是“null”。

这是一些代码: Index.jsp(在eclipse的WebContent中)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Market</title>
    </head>

    <body>
        <header>
            <h1>market 2013</h1> 
            <h2>Bievenue </h2>
        </header>
        <nav>
        <% String include = "/WEB-INF/RandomArticle.jsp"; %>
        <jsp:include page='<%=include%>' />
        </nav>
    </body>
</html>

RandomArticle.jsp(在WEB-INF中):

<h1>Liste des produits EpiMarket</h1>
<%
String productName1 = (String) request.getAttribute("productName1");
String productName2 = (String) request.getAttribute("productName2");
String productName3 = (String) request.getAttribute("productName3");
%>
<p>Acheter <% out.println(productName1); %></p>
<p>Acheter <% out.println(productName2); %></p>
<p>Acheter <% out.println(productName3); %></p>

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Market</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Test</servlet-name>
    <servlet-class>fr.market.servlets.Test</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Test</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
    <servlet>
    <servlet-name>RandomArticle</servlet-name>
    <servlet-class>fr.market.servlets.RandomArticle</servlet-class>
  </servlet>
</web-app>

和RandomARticle.java

public class RandomArticle extends HttpServlet {

    private ArrayList<Object> allProducts;

    public String getProductName(int index){
                return (((AbstractProduct) allProducts.get(index)).getName());
           } 

    public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException{
         ADAO<AbstractProduct>  dao = new DAOProduit();
         allProducts = ((DAOProduit) dao).getAll();


         Collections.shuffle(allProducts);
         request.setAttribute("productName1", getProductName(0));
         request.setAttribute("productName2", getProductName(1));
         request.setAttribute("productName3", getProductName(2));
         this.getServletContext().getRequestDispatcher( "/WEB-INF/RandomArticle.jsp" ).forward( request, response );
     }
}

我认为.java永远不会被调用,但我不明白为什么。 谢谢你的时间。

吉勒

0 个答案:

没有答案