Search code examples
javajspbuttonscreenshot

invoking java function in jsp with buttion


I have a java function that I tested in a practice project that takes a screenshot of my jsp, how do I call the function from my JSP while using a button

this is the function I want to call

try{
    Robot robot = new Robot();
    BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    String home = System.getProperty("user.home");
    ImageIO.write(screenShot, "JPG", new File(home +"\\Desktop\\test.jpg"));
}
catch(IOException ex){
    System.out.println (ex.toString());
}

Solution

  • You can do like the following way

    inside JSP page submit a form with the same page with some temp data and check if the data is not null then there is action received for taking the photo.

    <%
    
    String a = request.getParameter("take");if(a!=null)
        {
    
            try {
                Robot robot = new Robot();
                BufferedImage screenShot = robot
                        .createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
                String home = System.getProperty("user.home");
    
                ImageIO.write(screenShot, "JPG", new File(home + "\\Desktop\\test.jpg"));
    
            } catch (IOException ex) {
                System.out.println(ex.toString());
            }
    
    }%>
    
    <form action="#"method="post">
    
    <input type="hidden"name="take"id="take"value="take_photo"/><input type="submit"/>
        </form>