0

I am developing a plugin for a software written in .jsp technology on Tomcat Server.

The software house which develops the main software (I am only writing a plugin to manage calendar events for that software), has told me it is better not to modify the web.xml file and other configuration files. In advance, I don't have access (and thus cannot modify) the source code of the web-application. I can only add jsp pages and .jars.

I can put .jsp pages, along with .js or .css files in the WEB-INF folder for the front-end. I can also access the WEB-INF/lib directory to add my .jar files for the back-end library with my backend logic.

I have the following question:

In this scenario, I need to call via jquery.ajax method the functions in my .jar library from my FrontEnd.jsp page. How can I achieve that?


I have supposed to do that:

FrontEnd.jsp (calls jquery.ajax (passing the method to invoke))

--> Backend.jsp (reads the request.getParameter(method) and calls the appropriate method in the BackEndLibrary.jar)

--> --> .jar library (executes the server-side code and gets back the result to the Backend.jsp)

<-- Backend.jsp (gets the response and serializes it as a JSON object)

<--<-- FrontEnd.jsp (gets the JSON object through the jquery.ajax method)


Example:

FrontEnd.jsp

 $("#click").click(function () {
        event= $("#event").val();
        date = $("#date").val();
        methodBackEnd = "getEventByDate";
        // call the ajax backend jsp passing the BackEnd Method to invoke 
        $.ajax({
            type: "POST",
            url: "BackEnd.jsp",
            data: "{'event':'" + event + "','date':'" + date + "','methodBackEnd':'" + methodBackEnd + "'}",
            contentType: "application/json",
            async: false,
            success: function (data) {
                $("#response").html(data.d);
            }

        });
    });

It calls the BackEnd.jsp

<!-- this is the import of my .jar library -->
<%@page import="com.xyz.eventmanagement.*"%>
switch (request.getParameter("methodBackEnd").toString()){
        case "getEventByDate":
            // call getEventByDate method in the .jar and return a JSON object to the frontend
            break;
        case "getEventByDescription":
            // call getEventByDescription method in the .jar and return a JSON object to the frontend
            break;
        default:
            // don't call anything
            break;
    }

In your opinion is that correct? Are there different ways to achieve that? I know it's a bit weird but I cannot modify (for example) the web.xml to change url-patterns. Thank you.

  • What version of servlet you are using? If you are using Servlet 3.0 consider the option for `@WebServlet` annotation. In servlet 3.0 you don't need to modify the web.xml – Pradeep Kr Kaushal Jan 14 '14 at 16:37
  • I will try to do some tests. Thank you. I need to add servlet at Compile-Time. http://stackoverflow.com/questions/4879903/how-to-add-a-jar-in-netbeans –  Jan 14 '14 at 18:44
  • Dear @PradeepKrKaushal I tried, but I saw the web application has not support for 3.0. I changed the web.xml file of the application to support 3.0 and it worked. The main issue is that I shouldn't modify the web.xml file of the application. So should I use my first solution? Do you think is that a good idea? –  Jan 15 '14 at 10:48
  • If it's just work around and get the thing working somehow, then you can go with any solution. But it's always advisable to follow the right approach. And the right approach is use the Servlet. – Pradeep Kr Kaushal Jan 15 '14 at 14:41
  • Yes you are right. I have spent a bit of time using servlets 3.0 but as I told you I cannot modify the config files of the main web-application as it is a proprietary application. I can only add jars to the WEB-INF/lib directory. Thank you for your answers. –  Jan 15 '14 at 15:35

0 Answers0