7

I have below servlet. I would like to call the servlet on jsp page load. How can I do that?

servlet: SomeServlet.java

<servlet>
  <servlet-name>Hello</servlet-name>
  <servlet-class>SomeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Hello</servlet-name>
 <url-pattern>/HelloWorld</url-pattern>
 </servlet-mapping>

How can I write corresponding jsp to invoke the servlet on jsp page load. Also I need to get the result from servlet and display in the same jsp. Can I send result back to jsp?

Thanks!

Farshid Shekari
  • 2,391
  • 4
  • 27
  • 47
user1016403
  • 12,151
  • 35
  • 108
  • 137
  • What exactly do you mean with 'on page load'? Do you want to redirect to the servlet, include the content on your page, or just trigger it? – home May 02 '12 at 12:56

4 Answers4

10

You should do it the other way round. Call the servlet by its URL and let it present the JSP. That's also the normal MVC approach (servlet is the controller and JSP is the view).

First put the JSP file in /WEB-INF folder so that the enduser can never "accidently" open it by directly entering its URL in browser address bar without invoking the servlet. Then change the servlet's doGet() accordingly that it forwards the request to the JSP.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...

    request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
}

Open it by

http://localhost:8080/contextname/HelloServlet

Note that you can of course change the URL pattern in servlet mapping to something like /hello so that you can use a more representative URL:

http://localhost:8080/contextname/hello

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • And if I have a servlet in this package: `com.work.something.controller` , what is the url that I should call to call it directly?? – hcarrasko Feb 20 '14 at 14:11
  • @Kramnik0: The URL is not dependent on the package/FQN of the servlet registration, but on the URL pattern of the servlet mapping. – BalusC Feb 20 '14 at 15:17
  • I know now that I need do the mapping first in the web.xml file. thank you for you answer. – hcarrasko Feb 20 '14 at 15:36
2
<jsp:include page="/HelloWorld"/>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks Nizet. How can i invoke the jsp from servlet to display the result retrieved in servlet? Thanks. – user1016403 May 02 '12 at 13:00
  • That's a different question. Your question was: "how to invoke servlet from JSP?" Now you're asking "how to invoke JSP from servlet?". What do you exactly want? What's the URL invoked by the client browser, and how should the request be handled? – JB Nizet May 02 '12 at 13:03
0

Call a servlet instead get result in request attribute and forward the request to jsp

or make an ajax call to servlet on load and render the response using javascript

jmj
  • 237,923
  • 42
  • 401
  • 438
0

In JSP paage you can forward the request to the Servlet

response.sendRedirect(request.getContextPath()+"/SomeServlet");
Bhavik Ambani
  • 6,557
  • 14
  • 55
  • 86