Friday, 8 June 2012

Enable the query cache in MySQL to improve performance

If you want to get optimized and speedy response from your MySQL server then you need to add following two configurations directive to your MySQL server:
query_cache_size=SIZE
The amount of memory (SIZE) allocated for caching query results. The default value is 0, which disables the query cache.
query_cache_type=OPTION
Set the query cache type. Possible options are as follows:
0 : Don't cache results in or retrieve results from the query cache.
1 : Cache all query results except for those that begin with SELECT S_NO_CACHE.
2 : Cache results only for queries that begin with SELECT SQL_CACHE

Howto enable caching in MySQL

You can setup caching as follows:
$ mysql -u root –p
Output:
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16 to server version: 4.1.15-Debian_1-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>
Now setup cache size 16Mb:
mysql> SET GLOBAL query_cache_size = 16777216;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE 'query_cache_size'; 
Output:
 +------------------+----------+
| Variable_name    | Value    |
+------------------+----------+
| query_cache_size | 16777216 |
+------------------+----------+
You can setup them in /etc/my.cnf (Red Hat) or /etc/mysql/my.cnf (Debian) file:
# vi /etc/my.cnf
Append config directives as follows:
query_cache_size = 268435456
query_cache_type=1
query_cache_limit=1048576
In above example the maximum size of individual query results that can be cached set to 1048576 using query_cache_limit system variable. Memory size in Kb.

Sunday, 13 May 2012

How to stream audio and video media online on a shoe-string budget

             There are many ways that one can stream audio and video materials online and we found a few ways that are cost effective and efficient enough to handle most of the online streaming activities.

             Here we list some of the technologies that we used to put together a system that may help anyone with their audio and vide streaming needs with a low or no budget. It will take some time, reading/researching and effort to accomplish it. We will describe briefly how we accomplished our goal of setting up a system on a shoe string budget. You may be able to extract a few ideas/points and build or come up with your own way of doing the same thing. It is possible to accomplish anything nowadays, as long as the Internet and Google is there.

             The main system we have is a Pentium IV with 512MB RAM memory and 40GB hard drive. We have DSL high-speed connection to the Internet. The system runs on ubuntu Linux operating system (almost all the software that we have used will run on Windows based system as well but MS Windows costs). In this system we have three streaming servers like “Shoutcast”, “Darwin” and “Red5” running at the same time (it is our test server, gets abused a lot). All these streaming servers are capable of streaming audio and/or video media effectively. The difference is mainly the format they use to broadcast the video signal.

             Shoutcast server from www.shoutcast.com/ works with “NSV” (Nullsoft video) format.
Darwin streaming server from www.apple.com is for QuickTime and mpeg movies (“mov” & “mp4”). Red5 server http://osflash.org/red5 works with flash (“flv”) videos. On the audio side all these servers can stream mp3 files without any problem.

             Depending on the need, a particular combination of the above can serve in any situation. For example a radio broadcasting station is possible with the following combination.

             A Linux (or Windows) based computer system with a decent hardware specification as mentioned above running the Shoutcast streaming server as the main system. A Windows 2000 or XP system with software like Winamp player with necessary plug-in is all what is needed to setup a powerful radio broadcasting system. Can say that this is the installation of this setup is the easiest of all.

                 This Windows based system should have a working sound card. Adding and automation software like “Zarasoft” (no cost of course) can even automate the radio station when and if the DJ is missing. Call-in talk shows are possible with a phone system integrated into this setup. This option will need a little more homework and work.

                 If your need is to stream QuickTime or mpeg movies then the same Linux (or Windows) system with Darwin streaming server will be a good choice. Darwin streaming server is as same as the Apple’s commercial product QuickTime streaming server but available for free from Apple’s website. Installing it and getting it up and running is not very difficult. A web based admin tool is available to manage the playlist files and other streaming media.

                  Flash has a big advantage over the others. Mainly, according to some Internet source 98% of the web browsers are installed with flash plug-in to play flash videos. It is a popular choice for many including movie producers for their online movie trailer streaming. The open source flash software “Red5” is equivalent to Adobe’s commercial software Flash Communication server. The combination Linux and Red5 (all for free of charge) has a few other features that are desirable in certain situation such as audio or video chat systems for an example.

                  All these combinations are capable of streaming real time events if needed. For fun and testing purposes we stream live local TV channels over the Internet for our friends who live in other countries. That is, with a second Windows based source computer equipped with a TV tuner which connected to the cable TV and to the main streaming server on the LAN (Local Area Network), this can be easily achieved.

                   The Linux operating system comes with a web server (Apache) and servers like MySQL and PHP software. With these amazing tools the audio and video streaming can be integrated into a website. This website can optionally hosted out side your home or office with an ISP (Internet Service Provider).

                   We may have spent less than $200 for the setup with all the combinations mentioned here. It took us about 3hours to finish the whole setup (took us 6-8 months initially to learn and reach this point). All the software, that is the OS, the streaming server software, the web server software, sourcing software with appropriate plug-ins, the automation software, weather watcher/announcement software, audio editing software like “Audacity”, video encoding software like “Super” and screen casting (screen capture) software such “Camstudio” and Wink2 were all obtained for free. Thus we built our audio and video streaming system that streams online on a shoe-string budget.
Our “audio and video streaming online – beginner’s guide” is available at www.aduioandvideostreaming.com has little more information on the “ubuntu Linux/Shoutcast” combination. Have a look at the overview to get an idea of the ebook contents along with couple of video tutorials.
Thank you.

Thursday, 10 May 2012

Core Java Questions with answers

1)What is the purpose of serialization?
Answer: Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialised – converted into a replica of the original object.
Source | Example
2)What is the difference between JDK and JRE?
Answer: Java Development Kit (JDK) is the most widely used Java Software Development Kit. Java Runtime Environment (JRE) is an implementation of the Java Virtual Machine which executes Java programs.
Source | JDK Wiki | JVM Wiki 
3)What is the difference between equals() and “==” ?
Answer: Equals is intended to check logical equality and == checks if both references point to same object. (Thanks Sandeep)
a == b;        // Compares references, not values.
a.equals(b);  // Compares values for equality.
Source
4)When will you use Comparator and Comparable interfaces? 
Answer: java.util.Comparator and java.lang.Comparable java.util.Comparator compares some other class’s instances, while java.lang.Comparable compares itself with another object.
Source | Example
5)What is the wait/notify mechanism?
Answer: This deals with concurrent programming. The wait() and notify() methods are designed to provide a mechanism to allow a thread to be block until a specific condition is met.
However, java.util.concurrent should be used instead of wait() and notify() to reduce complexity.
Source | Java API | Java Technical Article
6)What is the difference between checked and unchecked exceptions?
Answer:
In general, unchecked exceptions represent defects in the program (bugs), which are normally Runtime exceptions.
Furthermore, checked exceptions represent invalid conditions in areas outside the immediate control of the program.
Source
7)What is the difference between final, finally and finalize?
Answer: “final” is the keyword to declare a constant AND prevents a class from producing subclasses. (Thanks Tom Ellis)
“finally” is a block of code that always executes when the try block is finished, unless System.exit() was called. finalize() is an method that is invoked before an object is discarded by the garbage collector.
Source | Final Usage |Finally Usage | Finalize()
8)What is the difference between web server and app server?
Answer: A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.
Source
9)Explain the Struts1/Struts2/MVC application architecture?
Answer: Struts was adopted by the Java developer community as a default web framework for developing web applications
The MVC(Model–view–controller) an application that consist of three distinct parts. The problem domain is represented by the Model. The output to the user is represented by the View. And, the input from the user is represented by Controller.
Source
10)What is the difference between forward and sendredirect?
Answer: Both method calls redirect you to new resource/page/servlet. The difference between the two is that sendRedirect always sends a header back to the client/browser, containing the data in which you wanted to be redirected.
Source
11)How does a 3 tier application differ from a 2 tier one?
Answer: Tiers are the physical units of separation or deployment, while layers are the logical units of separation.
Imagine that you’re designing an e-commerce website. A 3 tier architecture would consist of web pages, a web server and a database, with the corresponding 3 layers being the “Presentation”, “Business Logic” and “Database” layers.
If you take the database tier and layer out then your have a 2 tier architecture.
Source
12)How does the version control process works?
Answer: Initiate, pull, branch, merge, commit, push.
(Init) Make your own repository. (Pull) Download an existing repository from a url. (Branch / Merge )Make revisions. Commit then push your modifications.




 
13)What is the difference between JAR and WAR files?
Answer: JAR files (Java ARchive) allows aggregating many files into one, it is usually used to hold Java classes in a library.
WAR files (Web Application aRchive) stores XML, java classes, and JavaServer pages for Web Application purposes.
Source

14)What is a Left outer join?
Answer: This deals with SQL. Left outer join preserves the unmatched rows from the first (left) table, joining them with a NULL row in the shape of the second (right) table.
Source | Joins Wiki

15)What is the difference between UNION and UNION ALL?
Answer: This deals with SQL. UNION only selects distinct values, UNION ALL selects all values.
Source | Example

Wednesday, 25 April 2012

Tuning Performance of Java Web Application

Best Practices to improve performance in Servlets

1. Use init() method to cache static data
2. Use StringBuffer/StringBuilder rather than using + operator when you concatenate multiple strings
3. Use print() method rather than println() method
4. Use ServletOutputStream rather than PrintWriter to send binary data
5. Initialize the PrintWriter with proper size
6. Flush the data partly
7. Minimize code in the synchronized block
8. Set the content length
9. Release resources in destroy() method.
10. Implement getLastModified() method to use browser cache and server cache
11. Use application server caching facility
12. Use Mixed session mechanisms such as HttpSession with hidden fields
13. Remove HttpSession objects explicitly in your program whenever you finish the task
14. Reduce session time out value as much as possible
15. Use 'transient' variables to reduce serialization overhead if your HttpSession tracking mechanism uses serialization process.
16. Disable servlet auto reloading feature.
17. Use thread pool for your servlet engine and define the size as per application requirement.

Best Practices to improve Performance in JSP
1. Use jspInit() method to cache static data
2. Use StringBuffer rather than using + operator when you concatenate multiple strings
3. Use print() method rather than println() method
4. Use ServletOutputStream instead of JSPWriter to send binary data
5. Initialize the 'out' object (implicit object) with proper size in the page directive.
6. Flush the data partly
7. Minimize code in the synchronized block
8. Set the content length
9. Release resources in jspDestroy() method.
10. Give 'false' value to the session in the page directive to avoid session object creation.
11. Use include directive instead of include action when you want to include the child page content in the translation phase.
12. Avoid giving unnecessary scope in the 'useBean' action.
13. Do not use custom tags if you do not have reusability.
14. Use application server caching facility
15. Use Mixed session mechanisms such as 'session' with hidden fields
16. Use 'session' and 'application' as cache.
17. Use caching tags provided by different organizations like openSymphony.com
18. Remove 'session' objects explicitly in your program whenever you finish the task
19. Reduce session time out value as much as possible
20. Use 'transient' variables to reduce serialization overhead if your session tracking mechanism uses serialization process.
21. Disable JSP auto reloading feature.
22. Use thread pool for your JSP engine and define the size of thread pool as per application requirement.

Best practices to improve performance in JDBC

1. Get database connection from connection pool rather than getting it directly
2. Use batch transactions.
3. Choose right isolation level as per your requirement. TRANSACTION_READ_UNCOMMITED gives best performance for concurrent transaction based applications. TRANSACTION_NONE gives best performance for non-concurrent transaction based applications.
4. Your database server may not support all isolation levels, be aware of your database server features.
5. Use PreparedStatement when you execute the same statement more than once.
6. Use CallableStatement when you want result from multiple and complex statements for a single request.
7. Use batch update facility available in Statements.
8. Use batch retrieval facility available in Statements or ResultSet.
9. Set up proper direction for processing rows.
10. Use proper getXXX () methods.
11. Close ResultSet, Statement and Connection whenever you finish your work with them.
12. Write precise SQL queries.
13. Cache read-only and read-mostly tables data.

Performance improvement techniques in Object creation

1. Avoid creating objects in a loop.
2. Use String literals instead of String objects (created using the 'new' keyword) if the content is same.
3. Whenever you are done with an object make that reference null so that it is eligible for garbage collection.
4. Never keep inheriting chains long since it involves calling all the parent constructors all along the chain until the constructor for java.lang.Object is reached.
5. Use primitive data types rather than using wrapper classes.
6. Whenever possible avoid using class variables, use local variables since accessing local variables is faster than accessing class variables.
7. Another technique is Lazy object creation: i.e. delaying the memory allocation to an object till it is not being put into use. This way a lot of memory is saved till the object is actually put in to use.
Performance improvement techniques in Exceptions
1. In a catch block avoid using the generic class Exception. For each try block use specific catch blocks based on what can go wrong in your code.
2. Whenever you are using a throws clause always use the specific subclass of Exception like FileNotFoundException rather than using throws Exception.
3. Always use the finally block to release the resources like a database connection, closing a file or socket connection etc. This prevents resource leaks even if an exception occurs.
4. Do not use Exception handling in loops. It is better to place loops inside try/catch blocks than vice versa. Here is an code snippet that gives bench mark.

Performance improvement techniques in loops

1. Always use an int data type as the loop index variable whenever possible because it is efficient when compared to using byte or short data types. because when we use byte or short data type as the loop index variable they involve implicit type cast to int data type.
2. When using arrays it is always efficient to copy arrays using System.arraycopy() than using a loop.
3. Always avoid anything that can be done outside of the loop like method calls, assigning values to variables, or testing for conditions.
4. Method calls are very costly and you only make it worse by putting them in a loop. So as far as possible avoid method calls in a loop.
5. It is better to avoid accessing array elements in a loop the better option would be to use a temporary variables inside the loop and modify the array values out of the loop. It is fast to use a variable in a loop than accessing an array element.
6. Try to compare the terminating condition with zero if you use non-JIT or HotSpot virtual machine, here is an example to prove the point. JIT or HotSpot virtual machines are optimized for general loops so you do not have to bother about the terminating condition.

Reference: www.precisejava.com

Tips For Measuring Application Performance
Here are some of important points which should be considered while measuring application performance.

- Test systems should match production as closely as possible. Same hardware, OS, and software.
- Populate databases with the number of records expected in production.
An application may perform well with a small number of records in the database. Performance could degrade a great deal as the number of records in the table(s) increases
- Test HTTP requests with different request parameters.
Test with the extremes. A page which performs a search may perform well when the search criteria returns a small result set, but perform poorly when the search criteria returns a large result set.
- Simulate expected traffic over time including short term spikes.
A page which performs well with low request volume can cause the server to fail under higher request volume.
- Use Server load testing tools (like Apache Jakarta JMeter) to simulate heavy load.

Tomcat - Jboss Performance Tuning Tips

Apache Tomcat is an implmentation of the Java Servlet and JavaServer Pages technologies.
In production Enviroment, number of resources which can all impact application overall performance. Here are some important point which can help to improve Tomcat Performance.

Performance Tuning - Tomcat Configuration

Configure server.xml parameters as following.

-Set reloadable to false

When reloadable is true Tomcat tries to detect web application class changes and automatically reload any classes which change. Setting this to false removes a lot of unnecessary overhead in production.

-Set liveDeploy to false

liveDeploy controls whether your webapps directory is periodically checked for new web application directories and war files. This is done using a background thread. Set this to false and use the manager application or ant deploy tasks instead.

-Set debug to 0

Disable all debug output, unnecessary logging just adds overhead.

-Set swallowOutput to true

This makes sure all output to stdout or stderr for a web application gets directed to the web application log rather than the console or catalina.out. This makes it easier to troubleshoot web application problems.


Performance Tuning - Jasper 2 Configuration

Configure web.xml parameters as following.

-Set development to false

Disables JSP page out of date checks on each request and enables JSP background recompiles. development is set to true by default.

-Use JSP custom tag pooling

Object pooling of classes which implement custom tags significantly improves performance of JSP pages which use custom tag libraries. JSP custom tag pooling is enabled by default.

-Configure Jasper to fork javac compiles. (Do not set fork to true on MS Windows)

The JVM compiler javac has known memory leaks. Eliminates JVM memory usage and GC overhead of javac by configuring Jasper to fork javac when compiling JSP pages.

Make Textbox hidden/visible based on the DropDown selection using JavaScript

SOLUTION:

the following function will help you to solve this ......

1
2
3
4
5
6
7
8
function hideTextBox(dropDown1){
var valFromDropDown = dropDown1.value;
if(valFromDropDown == 'WRITE'){
document.forms[0].textBox1.style.visibility = "";
} else{
document.forms[0].textBox1.style.visibility = "hidden";
}
}

in the above example hideTextBox is the function name .....
and the input for this function is dropDown value selected......
in this example we are hiding the the text box if the drop down value is 'WRITE' else it will show the textbox......


the following Example.jsp page will clearly explain you how to implement this and please ignore this Symobl(--)inside the every tag Starting. i put this (--) for displaying html tags only kindly ignore that symbol


Example.jsp(please ignore this(--)symbol and try it it will surely work
--------------------------------------------------------------------

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<%@taglib uri="/includes/struts-html.tld" prefix="html"%>
<%@taglib uri="/includes/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/includes/struts-logic.tld" prefix="logic"%>



<script language="javascript">

function hideTextBox(dropDown1){
var valFromDropDown = dropDown1.value;
if(valFromDropDown == 'WRITE'){
document.forms[0].textBox1.style.visibility = "";
} else{
document.forms[0].textBox1.style.visibility = "hidden";
}
}

</script>




<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>
<td width="20%">SelectProcess:
<td width="30%">
<html:select property="dropDown1" onchange="javascript:hideTextBox(this)">
<html:option value="READ">Read
<html:option value="WRITE">Write
<html:option value="OVERWRITE">OverWrite
</html:select>
<td width="20%">
<td width="30%">
</tr>

<tr>
<td width="20%">TextBox1:
<td width="30%">
<td width="30%"><html:text property="textBox1" maxlength="10" size="10">
</td>
<td width="20%">
<td width="30%">

</table>

</html:html>

--------------------------------------------------------------------------------------
from the above Example.jsp you can clearly understood how the function is implemented

note:

you have to declare these property in the form bean this is the example used in struts Frame work.

if you still having doubts write a comment on this.

Thanks

Download the CSV file using Servlets


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Csv
 */
@WebServlet("/Csv1")
public class Csv1 extends HttpServlet {
 private static final long serialVersionUID = 1L;

 /**
  * @see HttpServlet#HttpServlet()
  */
 public Csv1() {
  super();
  // TODO Auto-generated constructor stub
 }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doGet(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // PrintWriter out = response.getWriter();
  Connection con = null;
  Statement stmt = null;
  ResultSet rs = null;
  try {
   Class.forName("com.mysql.jdbc.Driver");
   con = DriverManager.getConnection(
     "jdbc:mysql://localhost:3306/hibernate", "root", "root");
   stmt = con.createStatement();
   rs = stmt.executeQuery("SELECT * FROM employee");
   String filename = "d:\\csv\\myfile.csv";
   FileWriter fw = new FileWriter(filename);
   fw.append("Employee Code");
   fw.append(',');
   fw.append("Employee Name");
   fw.append('\n');

   while (rs.next()) {
    fw.append(rs.getObject(1).toString());
    fw.append(',');
    fw.append(rs.getObject(2).toString());
    fw.append('\n');
    fw.flush();
    // fw.close();
   }
   // out.println("<b>Csv file Successfully created.</b>");
   if (fw != null) {
    fw.close();
   }

  } catch (SQLException e) {
   throw new ServletException("Servlet Could not display records.", e);
  } catch (ClassNotFoundException e) {
   throw new ServletException("JDBC Driver not found.", e);
  } finally {
   try {
    if (rs != null) {
     rs.close();
     rs = null;
    }
    if (stmt != null) {
     stmt.close();
     stmt = null;
    }
    if (con != null) {
     con.close();
     con = null;
    }
   } catch (SQLException e) {
   }
  }

  File f = new File("d:\\csv\\myfile.csv");
  int length = 0;
  ServletOutputStream op = response.getOutputStream();
  ServletContext context = getServletConfig().getServletContext();
  String mimetype = context.getMimeType("text");
  //
  // Set the response and go!
  //
  //
  response.setContentType((mimetype != null) ? mimetype
    : "application/octet-stream");
  response.setContentLength((int) f.length());
  response.setHeader("Content-Disposition",
    "attachment; filename=d:\\csv1.csv");

  //
  // Stream to the requester.
  //
  byte[] bbuf = new byte[1024];
  DataInputStream in = new DataInputStream(new FileInputStream(f));

  while ((in != null) && ((length = in.read(bbuf)) != -1)) {
   op.write(bbuf, 0, length);
  }

  in.close();
  op.flush();
  op.close();
  // TODO Auto-generated method stub
 }

 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 protected void doPost(HttpServletRequest request,
   HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
 }

}


web.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>csv1</servlet-name>
<servlet-class>Csv1</servlet-class>

</servlet>
<servlet-mapping>
<servlet-name>csv1</servlet-name>
<url-pattern>/csv1</url-pattern>

</servlet-mapping>


</web-app>

Thursday, 12 April 2012

How to write our own result type in struts2

Action Class


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.lkr.action;


import com.lkr.service.UserService;
import com.lkr.vo.User;
import com.opensymphony.xwork2.ActionSupport;


public class GetImageAction extends ActionSupport {


 private static final long serialVersionUID = 1L;
 private UserService userService;
 private Long id;
 private User user;


 public Long getId() {
  return id;
 }


 public void setId(Long id) {
  this.id = id;
 }


 public UserService getUserService() {
  return userService;
 }


 public void setUserService(UserService userService) {
  this.userService = userService;
 }


 @Override
 public String execute() throws Exception {

  System.out.println("this is come from after display image jsp");
  user = userService.getUserById(id);
  return "imageBytesResult";
 }


 public String getContentDisposition() {
  return "";
 }

 public int getBufferSize() {
  return 4096;
 }
}

Result Type Class


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.lkr.action;


import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;


public class ImageBytesResult implements Result {


 private static final long serialVersionUID = 1L;


 @Override
 public void execute(ActionInvocation invocation) throws Exception {
  GetImageAction action = (GetImageAction) invocation.getAction();
  HttpServletResponse response = ServletActionContext.getResponse();


  //response.setContentType(action.getContentType());
  //response.setContentLength(action.getContentLength());
  response.getOutputStream().write(action.getImageInBytes());
  response.getOutputStream().flush();
 }


}

Service


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.lkr.service;


import java.util.List;

import com.lkr.dao.UserDAO;
import com.lkr.vo.User;


public class UserService{
 private UserDAO userDao;


 public UserDAO getUserDao() {
  return userDao;
 }


 public void setUserDao(UserDAO userDao) {
  this.userDao = userDao;
 }


 
 public User save(User user) throws Exception {


  return userDao.save(user);
 }



 public List<User> getAllUsers() throws Exception {


  return userDao.getAllUsers();
 }


 
 public User getUserById(long id) throws Exception {
  return userDao.getUserById(id);
 }
}

DAO


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.lkr.dao;


import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

import org.hibernate.Transaction;

import com.lkr.vo.User;


public class UserDAO {


 
 public User save(User user) throws Exception {
  Transaction tx=getSession().beginTransaction();
  getSession().save(user);
  
  tx.commit();
  return user;
 }


 
 public List<User> getAllUsers() throws Exception {
  List<User> allUsers = getSession().createQuery("from User").list();
  return allUsers;
 }


 
 public User getUserById(long id) throws Exception {
  User user = (User) getSession().get(User.class, new Long(id));
  // Convert BLOB to byte array
  user.setImageInBytes(getImageInBytes(user));
  user.setContentLength((int) user.getImage().length());
  return user;
 }


 public byte[] getImageInBytes(User user) {


  if (user.getImage() == null) {
   return new byte[0];
  }
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  try {
   InputStream is = user.getImage().getBinaryStream();
   byte[] buf = new byte[1024];
   int i = 0;
   while ((i = is.read(buf)) >= 0) {
    baos.write(buf, 0, i);
   }
   is.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return baos.toByteArray();
 }
}

struts.xml


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">


<struts>
 <constant name="struts.devMode" value="false" />
 <constant name="struts.custom.i18n.resources" value="global" />
 <constant name="struts.objectFactory" value="spring" />


 <package name="commonPackage" namespace="/" extends="struts-default">


  <result-types>
   <result-type name="imageBytesResult"
    class="com.lkr.action.ImageBytesResult" />
  </result-types>


  <action name="addUserData" class="com.lkr.action.UserAction">
   <result name="input">
    WEB-INF/jsp/imageUploader.jsp
   </result>
  </action>
  <action name="displayFullImage" class="com.lkr.action.FullImageAction">
   <result name="input">
    WEB-INF/jsp/dispalyFullImage.jsp
   </result>
  </action>


  <action name="getImage" class="com.lkr.action.GetImageAction">
   <result name="imageBytesResult" type="imageBytesResult">
   </result>
  </action>
 </package>


</struts>

If you are directly getting Byte[] from the database then you don't need to convert Blob to Byte[]