23

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Rupesh
  • 2,061
  • 5
  • 22
  • 36
  • 1
    When you say you want to "call servlet through jsp", what exactly do you want to do? Do you want to forward your request to another servlet? Do you want to redirect to a servlet? Do you want to call a method contained in one of your servlets? – El Guapo Apr 13 '11 at 13:07
  • i jst want to redirect to servlet – Rupesh Apr 13 '11 at 13:14
  • there are many good answers below. you'll need to create a mapping for your servlet in your web.xml (there's an example below) then you'll need to call response.redirect (the response object will be present in your jsp) to the mapping for your servlet. – El Guapo Apr 13 '11 at 13:17
  • @ElGuapo dear i have a userIndex.jsp where i wanna to fetch two servlets one is fetching and sending User Information and other is sending the books information. when i am logining it is showing me user information but not books class so how can i call both at same time ?? – Muhammad Siddique Apr 02 '18 at 14:06

6 Answers6

41

You could use <jsp:include> for this.

<jsp:include page="/servletURL" />

It's however usually the other way round. You call the servlet which in turn forwards to the JSP to display the results. 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. http://example.com/contextname/servletURL.

Do note that the JSP file is explicitly placed in /WEB-INF folder. This will prevent the user from opening the JSP file individually. The user can only call the servlet in order to open the JSP file.


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:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Good answer. 7 years after first written, tells me everything I need to know, everything I need to change and everything I am now going to do :) – thonnor Mar 23 '18 at 10:17
  • dear i have a userIndex.jsp where i wanna to fetch two servlets one is fetching and sending User Information and other is sending the books information. when i am login it is showing me user information but not books servlet so how can i call both at same time ?? books are fetched when i am manually giving link of the servlet. – Muhammad Siddique Apr 02 '18 at 14:06
6

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.

Sam YC
  • 10,725
  • 19
  • 102
  • 158
  • I'm not sure what, request.setAttribute("msg","HI Welcome"); is supposed to be doing, but that doesn't send a request "msg" of "HI Welcome". It's actually ignored. – Chizl Mar 13 '17 at 14:13
3

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"/> 
lukastymo
  • 26,145
  • 14
  • 53
  • 66
  • This will be the way you create the mapping.. but, you'll have to call response.sendRedirect(redirectURL); to the full URL with the mapping you've created... you have to make sure you response is flushed before you call sendRedirect or you'll get an error... I think, however, you should take @smas's full approach here... since a forward doesn't require the flushing. – El Guapo Apr 13 '11 at 13:20
  • 3
    Sending a forward from JSP to servlet will only risk that the server logs get cluttered with `IllegalStateException: response already committed` exceptions. JSP is the wrong place to act as a request/response controller. – BalusC Apr 13 '11 at 13:27
2

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.

egervari
  • 22,372
  • 32
  • 121
  • 175
1

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

And to redirect use:

response.sendRedirect("servleturl")
Harry Joy
  • 58,650
  • 30
  • 162
  • 207
  • 1
    Sending a redirect from JSP to servlet will only risk that the server logs get cluttered with `IllegalStateException: response already committed` exceptions. JSP is the wrong place to act as a request/response controller. – BalusC Apr 13 '11 at 13:25
  • @BalusC: Yes jsp is wrong place for this. OP should use servlet for processing and then redirect to JSP. But OP asks to do so thats why I answered to use `response.sendRedirect("")`. – Harry Joy Apr 13 '11 at 13:31
0

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.

Papuass
  • 327
  • 1
  • 10