0

I created a Slide Show with jQuery. It's connected to database with servlet. So, I have to call servlet to run the Slide Show. I tried to call servlet from index.jsp with include but it hasn't found SlideShow servlets. I tried to use Ajax to call servlet but it can't display Slide Show. Please how to call?

Mxyk
  • 10,678
  • 16
  • 57
  • 76
user572575
  • 1,009
  • 3
  • 25
  • 45

1 Answers1

2

Just call the servlet directly by URL instead of the JSP file, do the job in doGet() method and finally let the servlet forward the request to the JSP to let it generate and send the HTML stuff.

@WebServlet(urlPatterns={"/index"})
public class IndexServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Do your job here.
        // ...
        request.getRequestDispatcher("/WEB-INF/index.jsp").forward(request, response);
    }

}

Invoke this by http://example.com/contextname/index instead of http://example.com/contextname/index.jsp and put the JSP in /WEB-INF folder to prevent it from being opened directly without intervention of the servlet.

You can do the preprocessing job in doGet() method and store the data as request attribute. E.g.

List<Data> data = dataService.list();
request.setAttribute("data", data); // It'll be available by ${data} in JSP.

As to the Ajax story, perhaps you're simply implementing it wrongly. You can find some examples here: How to use Servlets and Ajax?

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555