日期:2014-05-17  浏览次数:20733 次

急求:请教关于JSP首页 取数据并显示的问题
设计相关的3个文件包括index.jsp,IndexServlet.java,web.xml
想做到进首先就会显示数据的页面
各位大侠可有招实现


web。xml配置如下
Java code

  <servlet>
  <servlet-name>IndexServlet</servlet-name>
  <servlet-class>com.lovo.cq.shopping10_1.servlet.IndexServlet</servlet-class>
 </servlet>
  <servlet-mapping>
  <servlet-name>IndexServlet</servlet-name>
  <url-pattern>/index</url-pattern>
  </servlet-mapping>
  
 <welcome-file-list>
  <welcome-file>/index.jsp</welcome-file>
 </welcome-file-list>



index.servlet为取数据的,代码如下
Java code

package com.lovo.cq.shopping10_1.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lovo.cq.shopping10_1.model.Model;
import com.lovo.cq.shopping10_1.po.Book;

public class IndexServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Model model = new Model();
        List hostList0 = model.showBooks(1, 1);
        List newList0 = model.showBooks(2, 1);
        List saleList0 = model.showBooks(3, 1);
        List specialList0=model.showBooks(4, 1);
        Book book11 = (Book) hostList0.get(0);
        Book book12 = (Book) hostList0.get(1);    
        Book book2 = (Book) newList0.get(0);
        Book book3 = (Book) specialList0.get(0);
        
        List<Book> hostList = new ArrayList<Book>();
        hostList.add(book11);
        hostList.add(book12);
        List<Book> newList = new ArrayList<Book>();
        newList.add(book2);
        List<Book> specialList = new ArrayList<Book>();
        specialList.add(book3);
        List<Book> saleList = new ArrayList<Book>();
        for(int i=0;i<3;i++){
            Book book = (Book) saleList0.get(i);
            saleList.add(book);
        }
        
        List informList = model.getAllInform();
        System.out.println(informList.size());
        request.getSession().setAttribute("informList", informList);
        request.getSession().setAttribute("hostList", hostList);
        request.getSession().setAttribute("newList", newList);
        request.getSession().setAttribute("saleList", saleList);
        request.getSession().setAttribute("specialList", specialList);
        
        response.sendRedirect("index.jsp");
//        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doGet(request, response);
    }

}





------解决方案--------------------
神了个奇了,文档明明是这么写的,你是Tomcat吧?

要不两个一起用上:
<welcome-file-list>
<welcome-file>IndexServlet</welcome-file>
<welcome-file>index</welcome-file>
</welcome-file-list>


我在这份文档看到说,又要用url-pattern,但是不能带 /