0

I'm work with JSP,JSTL,Servlet etc. at present.I have a problem.I don't understand this case.

Case 1 (This case excellent work,shows database records in JSP file):

Servlet

@WebServlet("/books")
public class IndexController extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Book> books = new BookDAO().getAllBooks();
        request.setAttribute("books", books);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

JSP

<c:forEach items="${books}" var="book">
        ${book}
    </c:forEach>

Case 2 (this case doesn't work,don't show database records in JSP file):

Servlet

@WebServlet("/")
public class IndexController extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<Book> books = new BookDAO().getAllBooks();
        request.setAttribute("books", books);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

JSP file same above.

Case 1: I try localhost/AppName/books => it works.

Case 2: I try localhost/AppName/ => it doesn't work

I want to localhost/AppName page show me database records.What am I do ?

mert
  • 920
  • 3
  • 15
  • 24
  • 3
    You should **never** have a servlet without a URL to match, note the case 2 has `("/")` as URL matching. This would wrap **every single request** (even a GET request for a resource, like JS/CSS/image file) – Luiggi Mendoza Feb 18 '13 at 22:47
  • 1
    Note: [BalusC answer](http://stackoverflow.com/a/3687003/1065197) for a similar case. It heavily relates to your *issue* – Luiggi Mendoza Feb 18 '13 at 22:55
  • You can set your welcome page to `books` and keep the first case (that would be the best). I don't really recommend case 2 to starters. – Luiggi Mendoza Feb 18 '13 at 22:58
  • If I can't find solution, I keep first case.But I don't want in this way.I will research.Thanks @LuiggiMendoza – mert Feb 18 '13 at 23:04
  • By the way, don't do cross-calls : `doGet()` is `doGet()`, and `doPost()` is `doPost()`. Don't mix them. – Med Feb 19 '13 at 02:02

0 Answers0