Pastebin
Paste #115:
< previous paste - next paste>
Pasted by tdn
package dk.mobilenation.imagetool.web;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gif4j.GifDecoder;
import com.gif4j.GifEncoder;
import com.gif4j.GifImage;
import dk.mobilenation.imagetool.interfaces.Image;
import dk.mobilenation.imagetool.interfaces.ImageHome;
/**
* Servlet Class
*
* @web.servlet name="Resize"
* display-name="Name for Resize"
* description="Description for Resize"
* @web.servlet-mapping url-pattern="/Resize"
* @web.servlet-init-param name="A parameter"
* value="A value"
* @web.ejb-ref
* name="ejb/Image"
* type="Session"
* home="dk.mobilenation.imagetool.interfaces.FiboHome"
* remote="dk.mobilenation.imagetool.interfaces.Fibo"
* description="Reference to the Image EJB"
*
* @jboss.ejb-ref-jndi
* ref-name="ejb/Image"
* jndi-name="ejb/Image"
*/
public class ResizeServlet extends HttpServlet {
private ImageHome home;
public ResizeServlet() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
//super.init(config);
try {
Context context = new InitialContext();
Object ref = context.lookup("java:/comp/env/ejb/Image");
home = (ImageHome) PortableRemoteObject.narrow(ref, ImageHome.class);
} catch (Exception e) {
throw new ServletException("Lookup of java:/comp/env/ failed");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
IOException {
resizeImage(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
resizeImage(request, response);
}
private void resizeImage(HttpServletRequest request, HttpServletResponse response) {
try {
Image bean = home.create();
URL imageURL = new URL(request.getParameter("url"));
//URL imageURL = new URL("http://gif-animate.per-il-mio-sito.com/animali_pinguini/pinguino-03.gif");
System.out.println("doGet(): Got request");
if (imageURL != null) {
try {
boolean imageSmooth = Boolean.parseBoolean(request.getParameter("smooth"));
int imageWidth = Integer.parseInt(request.getParameter("width"));
int imageHeight = Integer.parseInt(request.getParameter("height"));
GifImage theImage = GifDecoder.decode(imageURL);
GifImage resizedImage = bean.resizeGifImage(imageWidth, imageHeight, theImage, imageSmooth);
bean.remove();
response.setContentType("image/gif");
OutputStream out = response.getOutputStream();
GifEncoder.encode(resizedImage,out);
} catch (Exception e) {
//e.printStackTrace();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("Error!");
out.println("</title></head>");
out.println("<body>");
out.println("<h1>");
out.println("Bad user input!");
out.println("</h1>");
out.println("</body></html>");
out.close();
}
} else {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("Error!");
out.println("</title></head>");
out.println("<body>");
out.println("<h1>");
out.println("Empty URL!");
out.println("</h1>");
out.println("</body></html>");
out.close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("doGet() request caught exception");
} finally {
}
}
}
OUTPUT fra GET:
16:51:57,691 INFO [STDOUT] doGet(): Got request
16:52:01,987 INFO [STDOUT] doGet(): Got request
OUTPUT fra POST:
16:53:19,288 INFO [STDOUT] doGet(): Got request
16:53:23,555 INFO [STDOUT] java.net.MalformedURLException
16:53:23,555 INFO [STDOUT] at java.net.URL.<init>(Unknown Source)
16:53:23,555 INFO [STDOUT] at java.net.URL.<init>(Unknown Source)
16:53:23,555 INFO [STDOUT] at java.net.URL.<init>(Unknown Source)
16:53:23,555 INFO [STDOUT] at dk.mobilenation.imagetool.web.ResizeServlet.resizeImage(ResizeServlet.java:82)
16:53:23,555 INFO [STDOUT] at dk.mobilenation.imagetool.web.ResizeServlet.doGet(ResizeServlet.java:69)
16:53:23,555 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
16:53:23,565 INFO [STDOUT] at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
16:53:23,565 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
16:53:23,565 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
16:53:23,565 INFO [STDOUT] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)
16:53:23,565 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
16:53:23,635 INFO [STDOUT] at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
16:53:23,635 INFO [STDOUT] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:157)
16:53:23,635 INFO [STDOUT] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
16:53:23,635 INFO [STDOUT] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
16:53:23,675 INFO [STDOUT] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
16:53:23,675 INFO [STDOUT] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
16:53:23,675 INFO [STDOUT] at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
16:53:23,675 INFO [STDOUT] at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
16:53:23,675 INFO [STDOUT] at java.lang.Thread.run(Unknown Source)
16:53:23,675 INFO [STDOUT] doGet() request caught exception
Mine requests bliver foretaget fra en formular, index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
MobileNation Image Tool
</title>
</head>
<body>
<h1>MobileNation Image Tool</h1>
<h2>Post form</h2>
<form action="Resize" method="POST" >
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
Image URL:
</td>
<td>
<input type="text" name="url" value="http://gif-animate.per-il-mio-sito.com/animali_pinguini/pinguino-03.gif">
</td>
</tr>
<tr>
<td>
Image width:
</td>
<td>
<input type="text" name="width" value="240">
</td>
</tr>
<tr>
<td>
Image height:
</td>
<td>
<input type="text" name="height" value="240">
</td>
</tr>
<tr>
<td>
Smooth scaling:
</td>
<td>
<select name="smooth">
<option value="true">True</option>
<oprion value="false">False</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="Resize" value="Resize">
</td>
<td>
<input type="Reset">
</td>
</tr>
</table>
</form>
<h2>Get form</h2>
<form action="Resize" method="GET" >
<table cellspacing="2" cellpadding="2" border="0">
<tr>
<td>
Image URL:
</td>
<td>
<input type="text" name="url" value="http://gif-animate.per-il-mio-sito.com/animali_pinguini/pinguino-03.gif">
</td>
</tr>
<tr>
<td>
Image width:
</td>
<td>
<input type="text" name="width" value="240">
</td>
</tr>
<tr>
<td>
Image height:
</td>
<td>
<input type="text" name="height" value="240">
</td>
</tr>
<tr>
<td>
Smooth scaling:
</td>
<td>
<select name="smooth">
<option value="true">True</option>
<oprion value="false">False</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="Resize" value="Resize">
</td>
<td>
<input type="Reset">
</td>
</tr>
</table>
</form>
</body>
</html>
New Paste
Go to most recent paste.