Skip to main content

HTTP Status 500 - JBoss

No replies
blertadhimitri
Offline
Joined: 2013-02-02
Points: 0

Hello!
I created a dynamic web project in eclipse and need it to run with JBoss.
The version of Jboss I am using is JBoss.6.0.0 Final(I tried later versions but got the same error).
The java code is :
ImageServerServlet.java

<br />
package com.javasrc.webphotogallery.web;</p>
<p>// Import the JNDI classes<br />
import java.io.*;<br />
import javax.naming.*;</p>
<p>// Import the Servlet classes<br />
import javax.servlet.*;<br />
import javax.servlet.http.*;</p>
<p>// Import our database classes<br />
import java.sql.*;<br />
import javax.sql.*;</p>
<p>/**<br />
 * Returns the requested JPG from the database; input parameters are:<br />
 *<br />
 * @param imageid The id of the image to return<br />
 * @param size   "full" or "thumbnail", default is "full"<br />
 */<br />
public class ImageServerServlet extends HttpServlet<br />
{<br />
  /**<br />
   * Cache a reference to the datasource object that we will use later to obtain<br />
   * a connection to the database<br />
   */<br />
  private transient DataSource datasource = null;</p>
<p>  public void init() throws ServletException<br />
  {<br />
    try<br />
    {<br />
      InitialContext ic = new InitialContext();<br />
      this.datasource = ( DataSource )ic.lookup( "java:/WebPhotoGalleryDS" );<br />
    }<br />
    catch( Exception e )<br />
    {<br />
      throw new ServletException( e );<br />
    }<br />
  }</p>
<p>  public void service( HttpServletRequest req, HttpServletResponse res ) throws ServletException<br />
  {<br />
    String imageIdStr = req.getParameter( "imageid" );<br />
    if( imageIdStr == null )<br />
    {<br />
      throw new ServletException( "Image Id Required" );<br />
    }</p>
<p>    boolean fullImage = true;<br />
    String size = req.getParameter( "size" );<br />
    if( size != null && size.equalsIgnoreCase( "thumbnail" ) )<br />
    {<br />
      fullImage = false;<br />
    }</p>
<p>    Connection conn = null;<br />
    PreparedStatement ps = null;<br />
    ResultSet rs = null;<br />
    try<br />
    {<br />
      // Get a datbase connection<br />
      conn = getDBConnection();</p>
<p>      // Build a Prepared Statement<br />
      if( fullImage )<br />
      {<br />
        ps = conn.prepareStatement( "SELECT image_full FROM Image WHERE image_id = ?" );<br />
      }<br />
      else<br />
      {<br />
        ps = conn.prepareStatement( "SELECT image_thumb FROM Image WHERE image_id = ?" );<br />
      }<br />
      ps.setInt( 1, Integer.parseInt( imageIdStr ) );</p>
<p>      // Execute the query<br />
      rs = ps.executeQuery();<br />
      if( !rs.next() )<br />
      {<br />
        throw new ServletException( "There is no image defined with id: " + imageIdStr );<br />
      }</p>
<p>      // Get the image Blob from the database<br />
      Blob blob = rs.getBlob( 1 );<br />
      InputStream in = blob.getBinaryStream();</p>
<p>      // Output the blob to the HttpServletResponse<br />
      res.setContentType( "image/jpeg" );<br />
      BufferedOutputStream out = new BufferedOutputStream( res.getOutputStream() );<br />
      byte by[] = new byte[ 32768 ];<br />
      int index = in.read( by, 0, 32768 );<br />
      while ( index != -1 )<br />
      {<br />
        out.write( by, 0, index );<br />
        index = in.read( by, 0, 32768 );<br />
      }<br />
      out.flush();<br />
    }<br />
    catch( Exception e )<br />
    {<br />
      e.printStackTrace();<br />
      throw new ServletException( e );<br />
    }<br />
    finally<br />
    {<br />
      try<br />
      {<br />
        if( rs != null ) rs.close();<br />
        if( ps != null ) ps.close();<br />
        if( conn != null ) conn.close();<br />
      }<br />
      catch( SQLException sqlee ){}<br />
    }<br />
  }</p>
<p>  protected Connection getDBConnection()<br />
  {<br />
    try<br />
    {<br />
      return datasource.getConnection();<br />
    }<br />
    catch( SQLException se )<br />
    {<br />
      se.printStackTrace();<br />
    }<br />
    return null;<br />
  }<br />
}<br />

the web.xml file is:
<br />
<?xml version="1.0" encoding="UTF-8"?><br />
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN' 'http://java.sun.com/dtd/web-app_2_3.dtd'></p>
<p><web-app><br />
  <servlet><br />
    <servlet-name>ImageServerServlet</servlet-name><br />
    <servlet-class>com.javasrc.webphotogallery.web.ImageServerServlet</servlet-class><br />
  </servlet></p>
<p>  <servlet-mapping><br />
    <servlet-name>ImageServerServlet</servlet-name><br />
    <url-pattern>/image</url-pattern><br />
  </servlet-mapping><br />
</web-app><br />

After i build correctly the .war file and copied it in the deploy folder of Jboss and when i go to http://localhost:8080/webphotogallery/image?imageid=1 i get this error:
HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: java.lang.NullPointerException
com.javasrc.webphotogallery.web.ImageServerServlet .service(ImageServerServlet.java:102)
javax.servlet.http.HttpServlet.service(HttpServlet .java:847)
root cause

java.lang.NullPointerException
com.javasrc.webphotogallery.web.ImageServerServlet .service(ImageServerServlet.java:68)
javax.servlet.http.HttpServlet.service(HttpServlet .java:847)
note The full stack trace of the root cause is available in the JBoss Web/3.0.0-CR1 logs.