import java.io.BufferedInputStream;import java.io.BufferedWriter;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.StringWriter;import java.lang.Thread.UncaughtExceptionHandler;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;public class Errors { static public void install () { Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() { public void uncaughtException (Thread thread, Throwable ex) { submit("Uncaught", ex); } }); } static public void submit (String message) { submit(message, null); } static public void submit (String message, Throwable ex) { try { System.out.println("Error: " + message); if (ex != null) ex.printStackTrace(); URL url = new URL("http://example.com/something"); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestMethod("POST"); conn.connect(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()), 1024); writer.write("m="); writer.write(URLEncoder.encode(message, "UTF-8")); if (ex != null) { StringWriter buffer = new StringWriter(1024); ex.printStackTrace(new PrintWriter(buffer)); String stacktrace = buffer.toString(); writer.write("&e="); writer.write(URLEncoder.encode(stacktrace, "UTF-8")); } writer.close(); BufferedInputStream input = new BufferedInputStream(conn.getInputStream(), 256); byte[] buffer = new byte[256]; while (input.read(buffer) != -1) ; input.close(); conn.disconnect(); } catch (Throwable ex2) { System.out.println("Unable to submit exception!"); ex2.printStackTrace(); } }}
import java.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import javax.naming.Context;import javax.naming.InitialContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.sql.DataSource;public class ErrorServlet extends HttpServlet { protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Connection connection = getConnection(); try { String message = request.getParameter("m"); if (message == null) message = ""; String exception = request.getParameter("e"); if (exception == null) exception = ""; PreparedStatement statement = connection.prepareStatement("INSERT INTO errors SET date=NOW(), message=?, exception=?;"); statement.setString(1, message.trim()); statement.setString(2, exception.trim()); statement.executeUpdate(); statement.close(); } catch (Exception ex) { throw new RuntimeException("Error writing database entry.", ex); } finally { try { connection.close(); } catch (SQLException ignored) { } } } private Connection getConnection () { try { Context envContext = (Context)new InitialContext().lookup("java:comp/env"); DataSource dataSource = (DataSource)envContext.lookup("jdbc/arcanetactics"); return dataSource.getConnection(); } catch (Exception ex) { throw new RuntimeException("Error opening database connection.", ex); } }}
<web-app> <servlet> <servlet-name>somename</servlet-name> <servlet-class>com.example.YourServletClass</servlet-class> </servlet> <servlet-mapping> <servlet-name>somename</servlet-name> <url-pattern>/somepath</url-pattern> </servlet-mapping> <resource-ref> <res-ref-name>jdbc/YourResourceNameFromContextXml</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref></web-app>
import com.badlogic.gdx.ApplicationAdapter;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.InputAdapter;import com.badlogic.gdx.backends.lwjgl.LwjglApplication;import com.badlogic.gdx.graphics.Color;import com.badlogic.gdx.graphics.GL10;import com.badlogic.gdx.graphics.Pixmap;import com.badlogic.gdx.graphics.Pixmap.Blending;import com.badlogic.gdx.graphics.Pixmap.Format;import com.badlogic.gdx.graphics.Texture;import com.badlogic.gdx.graphics.g2d.SpriteBatch;import com.badlogic.gdx.math.MathUtils;public class MiniQuests extends ApplicationAdapter { private SpriteBatch batch; private Pixmap pixmap; private Texture texture; private Color color = new Color(1, 1, 1, 1); public void create () { Pixmap.setBlending(Blending.None); batch = new SpriteBatch(); pixmap = new Pixmap(40, 30, Format.RGB565); texture = new Texture(MathUtils.nextPowerOfTwo(pixmap.getWidth()), MathUtils.nextPowerOfTwo(pixmap.getHeight()), Format.RGB565); Gdx.input.setInputProcessor(new InputAdapter() { public boolean touchDown (int x, int y, int pointer, int button) { // Do something! return true; } }); } public void render () { for (int x = 0; x < pixmap.getWidth(); x++) { for (int y = 0; y < pixmap.getHeight(); y++) { color.r = MathUtils.random(0, 1); color.g = MathUtils.random(0, 1); color.b = MathUtils.random(0, 1); pixmap.setColor(color); pixmap.drawPixel(x, y); } } texture.draw(pixmap, 0, 0); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.disableBlending(); batch.begin(); batch.draw(texture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0, 0, pixmap.getWidth(), pixmap.getHeight(), false, false); batch.end(); } public static void main (String[] args) throws Exception { new LwjglApplication(new MiniQuests(), "", 800, 480, false); }}
public class MiniQuestsAndroid extends AndroidApplication { public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); initialize(new MiniQuests(), false); }}