Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call servlet through a JSP page

Tags:

jsp

servlets

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

like image 725
Rupesh Avatar asked Apr 13 '11 13:04

Rupesh


People also ask

Can we use servlet in JSP?

In this mode, JSP pages are used for the presentation layer, and servlets for processing tasks. The servlet acts as a controller responsible for processing requests and creating any beans needed by the JSP page. The controller is also responsible for deciding to which JSP page to forward the request.

How servlet and JSP interact with each other?

JSP(s) are compiled into Servlet(s); every JSP is-a Servlet. Does it work when you move intro. jsp into the WebContent folder and change to request.

How do you call a servlet?

Calling a Servlet Programmatically To include another servlet's output, use the include() method from the RequestDispatcher interface. This method calls a servlet by its URI and waits for it to return before continuing to process the interaction. The include() method can be called multiple times within a given servlet.


3 Answers

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:

  • Servlets info page - Contains a hello world
  • How to call servlet class from HTML form
  • How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
  • Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
  • Design Patterns web based applications
like image 198
BalusC Avatar answered Sep 21 '22 09:09

BalusC


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.

like image 40
Sam YC Avatar answered Sep 21 '22 09:09

Sam YC


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"/> 
like image 27
lukastymo Avatar answered Sep 22 '22 09:09

lukastymo