Rupesh
Rupesh

Reputation: 2051

How to call servlet through a JSP page

I would like to call a Servlet through a JSP page. What is the method to call?

Upvotes: 23

Views: 177690

Answers (6)

BalusC
BalusC

Reputation: 1109532

First put the JSP page anywhere in /WEB-INF folder so that it's impossible to accidentally open the JSP page individually without invoking the Servlet first. E.g. /WEB-INF/result.jsp.

Then 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 put this line in /WEB-INF/result.jsp.

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

Now call the Servlet by the URL which matches the URL pattern as defined in its @WebServlet annotation or in its <url-pattern> configuration in web.xml, e.g. /servletURL: http://example.com/contextname/servletURL.


If your actual question is "How to submit a form to a servlet?" then you just have to specify the servlet URL in the HTML form action.

<form action="servletURL" method="post">

Its doPost() method will then be called.

See also:

Upvotes: 42

Sam YC
Sam YC

Reputation: 11637

You can use RequestDispatcher as you usually use it in Servlet:

<%@ page contentType="text/html"%>
<%@ page import = "javax.servlet.RequestDispatcher" %>
<%
     RequestDispatcher rd = request.getRequestDispatcher("/yourServletUrl");
     request.setAttribute("msg","HI Welcome");
     rd.forward(request, response);
%>

Always be aware that don't commit any response before you use forward, as it will lead to IllegalStateException.

Upvotes: 6

egervari
egervari

Reputation: 22522

Why would you want to do this? You shouldn't be executing controller code in the view, and most certainly shouldn't be trying to pull code inside of another servlet into the view either.

Do all of your processing and refactoring of the application first, then just pass off the results to a view. Make the view as dumb as possible and you won't even run into these problems.

If this kind of design is hard for you, try Freemarker or even something like Velocity (although I don't recommend it) to FORCE you to do this. You never have to do this sort of thing ever.

To put it more accurately, the problem you are trying to solve is just a symptom of a greater problem - your architecture/design of your servlets.

Upvotes: 2

Harry Joy
Harry Joy

Reputation: 59694

You can submit your jsp page to servlet. For this use <form> tag.

And to redirect use:

response.sendRedirect("servleturl")

Upvotes: 1

Papuass
Papuass

Reputation: 327

You can either post HTML form to URL, which is mapped to servlet or insert your data in HttpServletRequest object you pass to jsp page.

Upvotes: 0

lukastymo
lukastymo

Reputation: 26809

there isn't method to call Servlet. You should make mapping in web.xml and then trigger this mapping.

Example: web.xml:

  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>test.HelloServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>hello</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>

This mapping means that every call to http://yoursite/yourwebapp/hello trigger this servlet For example this jsp:

<jsp:forward page="/hello"/> 

Upvotes: 3

Related Questions