2

I have a jsp page from where I am calling my servlet named 'InsertServlet'. I have done my required work in the service method in my servlet. I have also created a user define method in my servlet named doSomething(). But now I am being failed to call the method doSomething() from my jsp page. Is it possible to do it or I have to create a single servlet for every single action ?!! Can anyone please help me on this please ???!!! Here are my codes below >>>

my jsp page ###

<form action="IbatisInsertServlet" method="POST">
        First Name : <input type="text" name="firstName" value="" /><br/>
        Last Name : <input type="text" name="lastName" value="" /><br/>
        Salary : <input type="text" name="salary" value="" /><br/>
        <input type="submit" value="Enter" /><input type="reset" value="Clear" /><br/>
    </form>

my servlet's service method where I have done my work ###

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
        ...
        ... 
    } catch (Exception ex) {
        System.out.println("Exception is :: " + ex.getMessage());
    } finally {
        out.close();
    }
}

my servlet's doSomething method which I want to call ###

public void doSomething(){
    System.out.println("working");
}
Sumon Bappi
  • 1,937
  • 8
  • 38
  • 82

5 Answers5

2

If the doSomething method is going to be called from a JSP (which is really just a servlet) then I suggest that you put this code in a separate class which can be instantiated from the JSP and/or the the servlet. This would assume that the logic ofdoSomething has got nothing to do with the request

The idea of calling a servlet is that you are interfacing through HTTP, so if in some cases (as part of the GET/POST) you want to call doSomething then consider adding a parameter informing the servlet to do this.

E.g

myWebApp/myServlet?action=doSomething
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Are you sure about this `myWebApp/myServlet?action=doSomething` ? I never tried it but I doubt. – akash Jul 16 '14 at 05:28
  • 1
    @TAsk Sorry I am probably not making myself clear. All I am suggesting is that user use normal HTTP (maybe even ajax) to pass a parameter to the servlet and in the servlet test for this paramater. if the parameter is set then call this method (for whatever use it is) – Scary Wombat Jul 16 '14 at 05:32
  • I posted a complement to your answer as my own answer, because it was too long for a comment. – Serge Ballesta Jul 16 '14 at 06:47
0

First you have to understand how Servlet Container works!

The whole thing stands on a idea, named "Don't call us, we'll call you" also known as "Inversion of Control".

So when we write a servlet, we simply provides the methods which may be called by the container when needed! As a result the method signatures are predefined for a servlet and its not in your hand to add any method if you want.

So the answer is no, you can not invoke your doSomething() of Servlet from jsp, in a straight way!

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

Try this example:

html

<form action="IbatisInsertServlet" method="POST">
  ...
  <input type="hidden" value="save" name="cmd"/>
  <input type="submit" value="Enter"/>
  ...
</form>

<form action="IbatisInsertServlet" method="POST">
  ...
  <input type="hidden" value="edit" name="cmd"/>
  <input type="submit" value="Edit"/>
  ...
</form>

servlet

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
{
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
        String cmd = request.getParameter();//<--
        if("save".equals(cmd)) {
           save();
        }
        else if("edit".equals(cmd)) {
           edit();
        }
    } catch (Exception ex) {
        System.out.println("Exception is :: " + ex.getMessage());
    } finally {
        out.close();
    }
}

private void save() {
  ...
}

private void edit() {
  ...
}
  • I have thought about this but searching for the best. thanks for your approach. – Sumon Bappi Jul 16 '14 at 05:23
  • @SumonBappi, are you using any frameworks, if yes then you can do so, e.g. Spring, Liferay, Struts etc –  Jul 16 '14 at 05:24
0

If you can extend the JSP page from servlet (subclassing), there may be way. You can post the data to the same jsp page or servlet (based on your needs). You can call the method of the parent class, here it would be the servlet you are extending from.

Your JSP code

<%-- top of the page. extend the servlet --%>
<%@ page extends="your.package.IbatisInsertServlet" %>

<form action="IbatisInsertServlet" method="POST">
        First Name : <input type="text" name="firstName" value="" /><br/>
        Last Name : <input type="text" name="lastName" value="" /><br/>
        Salary : <input type="text" name="salary" value="" /><br/>
        <input type="submit" value="Enter" /><input type="reset" value="Clear" /><br/>
</form>

<%-- to call the function --%>
<% doSomething() %>

Note that you can leave the action empty if you want to call the same JSP or redirect it to the server. Also note that the JSP servlet and the IbatisInsertServlet will be two separate instances. Make sure you call the super.service(...) as needed.

This should work for you

Jp Vinjamoori
  • 1,173
  • 5
  • 16
0

This is just a complement to ScaryWombat answer.

If you call your method doSomething, it should not be part of a servlet but of another class. This way, you separate the concerns :

  • servlets (including JSP) manage the interactions with forms, requests, responses, session
  • other classes do the real job

If you full application if less than 100 lines of code, it not much important, but if it grows, you get smaller classes with less dependances which are easier to write and test.

This domain classes can be instantiated by a servlet (or a JSP) if they do not last longer than a request. If not you should instantiate them in a ServletContextListener, put them in the ServletContext and use them from any servlet of JSP you want.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252