2017  Kodetalk | Feedback | Privacy Policy | Terms | About

How to call servlet through a JSP page ?

How to call servlet through a JSP page ?

Use the <jsp:include>    to call the Servlet.

jsp:include page="/URLOfServlet" />

Which will  call the Servlet from the JSP that will include the response of the Servlet in the JSP at runtime.Create a Servlet which does something like following in doGet() method.

request.setAttribute("result", "This is the result of the servlet call");
request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);

and in /WEB-INF/result.jsp

<p>The result is ${result}</p>

Now call the Servlet by the URL which matches its <url-pattern> in web.xml, e.g. example URL pattren.

Answer is