How to display a default image when image source is a servlet?


In my JSP/ Servlet setup, for displaying user profile images I am directing the img tag to get the image from servlet by specifying servlet URL in src tag and then returning image in response from servlet.

Sevlet does below things upon receiving an image request,

<ol><li>It gets the image from database in BLOB format</li> <li>Prepares image from the byte[] and </li> <li>returns the image in response object with appropriate headers.</li> </ol>

My problem is since Signup page doesn't has image upload option so new users don't have their image in database i.e. so the BLOB is NULL. <strong>How can I display a default image for all such users ?</strong>

<strong>EDIT</strong> Based on comments received I just wanted to note that. I am aware of the solutions where we set the default value either in database (while creating the user) or return the default image array when there is NULL in database. <strong>I am already using the later case.</strong> By this question I just wanted to discuss about <strong>any other possible solutions</strong>.


One option could be to return HTTP error 404 "Not found" when the user don't have an image. It seems quite logical to me, since there really is no image for the user.

public void doGet(HttpServletRequest request, HttpServletResponse response) { response.sendError(HttpServletResponse.SC_NOT_FOUND); }

Then in your HTML you can handle this case using the <a href="http://www.w3schools.com/jsref/event_onerror.asp" rel="nofollow">onerror Event</a> to set the default image:

<img src="servletUrl" onerror="this.src='default.png'" width="150" height="150" />

In this way the servlet only sends user images and does not do any special logic if the user doesn't have one.


  • Selecting a subset of data in ServiceStack.OrmLite
  • Using Nested Table variables / Collections in SQL inside PL/SQL blocks
  • Getting NullPointer exception with File.listfiles()
  • Convert Type Decimal to Hex (string) in .NET 3.5
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • Content-Length header not returned from Pylons response
  • copying resource to sdcard gives a damaged file in android
  • Set the selected item in dropdownlist in MVC3
  • D3 nodes and links from JSON with nested arrays of children
  • Is calc() supported in html email?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Submit form in a displaytag pagination
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • How to extract text from Word files using C#?
  • How to check if every primary key value is being referenced as foreign key in another table
  • Sending data from AppleScript to FileMaker records
  • Fill an image in a square container while keeping aspect ratio
  • Join two tables and save into third-sql
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • How to model a transition system with SPIN
  • Symfony2: How to get request parameter
  • ActionScript 2 vs ActionScript 3 performance
  • To display the title for the current loaction in map in iphone
  • ORA-29908: missing primary invocation for ancillary operator
  • How do you troubleshoot character encoding problems?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • AT Commands to Send SMS not working in Windows 8.1
  • Traverse Array and Display in markup
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • log4net write single file for each call to log.info
  • Getting error when using KSoap library to consume .NET web services
  • How to set the response of a form post action to a iframe source?
  • How do I configure my settings file to work with unit tests?
  • Is it possible to post an object from jquery to bottle.py?
  • Can't mass-assign protected attributes when import data from csv file
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)
  • Converting MP3 duration time