I have been playing around with Processing and java and I'm trying to use some of the Processing functionality within a java servlet (serving generated images basically). I realize there is the "headless" issue, but I have no intention of running this on a headless server. For now it lives on my laptop.
Here's the code for my servlet:
applet.java
Code:package test;
import processing.core.*;
public class applet extends PApplet {
public void setup() {
PImage background2 = loadImage("url to some image", "gif");
PImage foreground2 = loadImage("url to some image", "png");
size(background2.width, background2.height);
image(background2, 0, 0);
image(foreground2, 0, 0);
}
public java.awt.Image someCrap(){
return g.getImage();
}
public static void main(String[] args) {
PApplet.main(new String[]{"test.applet"});
}
public applet(){
}
public void draw(){
}
}
File NewServlet.java
Code:
package test;
import java.awt.Image;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/gif");
try {
OutputStream os = response.getOutputStream();
applet apL = new applet();
javax.imageio.ImageIO.write((RenderedImage)apL.someCrap(),"GIF86", os);
} catch (Exception e) {
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
This results in a nullPointerException
Any help would be greatly appreciated.