0

I am trying to call a java method('method1') that runs some r code(using RCaller) when a specific button is clicked on a JSP page('button1'), but when i click on the button it doesn't do anything and I am getting no errors in the output. I'm trying to use a servlet to run the java method from a JSP file. I'd appreciate any help.

Analysis.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Analysis Page</title>
    </head>
    <body>
    <center><h2>
            Final year Prototype
        </h2></center>

    <center> <table border="0">
            <thead>
                <tr>
                    <th>Disabilities Analysis</th>
                </tr>
            </thead>
            <tbody>
                <tr>
            <center><td></td></center>
            </tr>
            <tr>
                <td>
            <center>
                <a href="index.jsp" class="myButton">Current HSE Support Centre Locations</a>
            </center>
            </td>
            </tr>
            <tr>
                <td>
            <center>
                <a href="index.jsp" class="myButton">New HSE Support Centre Locations</a>
            </center>
            </td>
            </tr>
            <tr>
                <td>
            <center>
                <form action="${pageContext.request.contextPath}/myservlet" method="POST">
                    <button type="submit" name="button" value="button1">Button 1</button>
                    <button type="submit" name="button" value="button2">Button 2</button>
                    <button type="submit" name="button" value="button3">Button 3</button>
                </form>
            </center>
            </td>
            </tr>
            <tr>
                <td>
            <center>
                <a href="index.jsp" class="myButton">Return</a>
            </center>
            </td>
            </tr>
            </tbody>
        </table></center>
</body>
</html>

myServlet.java:

package webJava;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        TestMethods t = new TestMethods();
        String button = request.getParameter("button");

        if ("button1".equals(button)) {
            t.method1();
        } else if ("button2".equals(button)) {
            t.method2();
        } else if ("button3".equals(button)) {
            t.method3();
        } else {

        }

        request.getRequestDispatcher("/Analysis.jsp").forward(request, response);
    }

}

TestMethods.java:

package webJava;

import java.io.File;
import javax.swing.ImageIcon;
import rcaller.RCaller;
import rcaller.RCode;


public class TestMethods {

    public void method1() {    
        try {
            RCaller caller = new RCaller();
            RCode code = new RCode();
            caller.setRscriptExecutable("/Library/Frameworks/R.framework/Versions/3.3/Resources/Rscript");
            caller.cleanRCode();
            caller.setRCode(code);

            // load the ggplot2 library into R
            code.R_require("ggplot2");

            // create a data frame in R using x and y variables
            code.addRCode("df <- data.frame(x,y)");

            // plot the relationship between x and y
            File file = code.startPlot();
            code.addRCode("ggplot(df, aes(x, y)) + geom_point() + geom_smooth(method='lm') + ggtitle('y = f(x)')");
            caller.runOnly();
            ImageIcon ii = code.getPlot(file);
            code.showPlot(file);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

    public void method2() {
        //some code
    }

    public void method3() {
        //some code
    }

}

My project directory:

Screenshot

Flex89
  • 13
  • 9
  • Have you executed the code in your debugger? What do you expect to happen, since the servlet just calls a method, and then shows the analysis page again? – JB Nizet Mar 26 '17 at 14:52
  • After my test your code is no problem I use the JDK1.7 tomcat7.I think it is TestMethods.java: method1 method inside the error – lpgad Mar 26 '17 at 15:02
  • Hi @JBNizet, thanks for replying. What I am looking to happen is that the r code that runs when 'button 1' is clicked should produce a chart, but this chart is not showing. When 'button1' is clicked it just stays on Analysis and no chart is showing up. I've tested the r code separately and it works, but not in the above example. – Flex89 Mar 26 '17 at 15:21
  • You're not generating any HTML containing any image from your servlet. You're just redisplaying Analysis.jsp again. So **if** you expect the chart to be displayed in the browser, that can't happen. if you expect something else, then what is it? And what happens when you run your code through your debugger, step by step? Or at least when you replace `System.out.println(e.toString());` by `throw new RuntimeException(e);`? – JB Nizet Mar 26 '17 at 15:27
  • @D3C1989 Are you sure the path "/Library/Frameworks/R.framework/Versions/3.3/Resources/Rscript" is working? – Suresh A Mar 26 '17 at 15:39
  • Hi @Surace, yes that path is working fine. Thanks. – Flex89 Mar 26 '17 at 15:41
  • Well debug line by line after that line . – Suresh A Mar 26 '17 at 15:45

0 Answers0