JavaTechie

Its all about Technology

Moved to TechiePark.com – My Personal site August 20, 2009

Filed under: Uncategorized — javatechie @ 10:26 am

Hello friends,

I have been moved from here to my personal site – TechiePark.com. Do visit for more information…

 

Display specific length string without truncating word in Java June 25, 2009

Filed under: Java, PHP — javatechie @ 5:29 am
Tags:

public static String textSubString(String text, int start, int end){

String str=”";
int limit=0;
limit=end;

if(text.length()!=0 && text!=”"){

String substr=text.substring(end,end+1);

if(substr!=”"){

while(!substr.equals(“”)){

substr=text.substring(limit,++limit);
substr=substr.trim();

}
}

str=text.substring(start,limit);

}

return str;
}

 

Java method similar to nl2br in PHP June 25, 2009

Filed under: Java, PHP — javatechie @ 5:26 am

public static String nl2br( String text){

return text.replaceAll(“\n”,”<br />”);

}

 

Directory index forbidden by Options directive June 8, 2009

Filed under: Apache — javatechie @ 12:29 pm
Tags: ,

I was going through error log and saw this error in error log file. To solve this problem, In /etc/httpd/conf.d you will see a file entitled welcome.conf

It looks like this:

<LocationMatch “^/+$”>
Options -Indexes
ErrorDocument 403 /error/noindex.html
</LocationMatch>

Change it to this:

<LocationMatch “^/+$”>
Options Indexes
ErrorDocument 403 /error/noindex.html
</LocationMatch>

Just remove the hyppen(-) before the Indexes, that’s it. I hope this will solve your problem.

 

Make sshd listen to a specific address June 8, 2009

Filed under: Uncategorized — javatechie @ 12:10 pm

In file /etc/ssh/sshd_config,
use

ListenAddress 192.168.0.1

to make sshd listen to only that address.

 

PHP- session Example June 3, 2009

Filed under: PHP — javatechie @ 12:33 pm

<?php
// Initialize the session.
session_start();
//store values
$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

//print stored values

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// Unset all of the session variables.
$_SESSION = array();

// Finally, destroy the session.
session_destroy();

?>

 

Outlook 2002 – The messaging interface has returned an unknown error May 28, 2009

Filed under: Uncategorized — javatechie @ 12:46 pm
Tags: , ,

Basically this message comes when the outlook storage capacity is full

Outlook, It can’t hold one more message.

You can’t delete because the message is only going to another folder inside the file cabinet.
Can’t send – no space to hold the message.

Super easy fix is -

In any folder, deleted, sent, inbox click on a message you want to delete. Start from the deleted folder. You have already deleted it once

Using…. shift+delete… get rid of the messages. This method blows it out of Outlook completely. If you click on a message and holding the shift key down you can highlight many messages and get rid of them.

After you get rid of about 30 messages you have enough room for the delete key to work.

Keep dumping the deleted items folder as you are working in sent or the inbox.

You’ll get your Outlook Program working back to normal. 1.8 GB is the capacity of default outlook storage

 

Serving Image from Absolute Path in Java/J2EE April 27, 2009

Filed under: Apache, Java — javatechie @ 11:20 am
Tags: , , ,

import java.io.*;
import java.net.URLDecoder;
import javax.servlet.*;
import javax.servlet.http.*;

/**
* Serving Image from Absolute Path.
*/

public class ImageServAbsolutePath extends HttpServlet {

private static final long serialVersionUID = 1L;

private static final int BUFFER_SIZE = 10240;

private String imagePath;

public void init() throws ServletException {

/*——- Image Base Path ———–*/
this.imagePath = “/images”;

}

protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Get requested image by path info.
String requestedImage = req.getPathInfo();

// Check if file name is there in request URI.
if (requestedImage == null) {
// if file name is not there in request URI, send 404 Error, or show default image.
res.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Decode the file name (might contain spaces and on) and prepare file object.
File image = new File(imagePath, URLDecoder.decode(requestedImage, “UTF-8″));

// Check if file actually exists in filesystem.
if (!image.exists()) {
// if file not exists in filesystem send 404 Error, or show default image.
res.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Get content type of Image by filename.
String contType = getServletContext().getMimeType(image.getName());

// Check if file is actually an image.
if (contType == null || !contType.startsWith(“image”)) {
// if the file is not a real image send 404 Error, or show default image.
res.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Reset servlet response.
res.reset();
res.setBufferSize(BUFFER_SIZE); //set buffer size
res.setHeader(“Content-Type”, contType); //set content type
res.setHeader(“Content-Length”, String.valueOf(image.length())); //set image size
res.setHeader(“Content-Disposition”, “inline; filename=\”" + image.getName() + “\”"); //set image name

// Create streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(image), BUFFER_SIZE);
output = new BufferedOutputStream(res.getOutputStream(), BUFFER_SIZE);

// Write image contents to response.
byte[] buffer = new byte[BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}

output.flush();
} finally {

if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

Add the following entries to the web.xml file

<servlet>
<servlet-name>ImageServAbs</servlet-name>
<servlet-class>ImageServAbsolutePath</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ImageServAbs</servlet-name>
<url-pattern>/image/*</url-pattern>
</servlet-mapping>

HOWTO USE :

<img src=”image/img.jpeg” />

 

How to Find and Replace Text in MySQL March 25, 2009

Filed under: Mysql — javatechie @ 5:55 am

MySQL database has a simple string function REPLACE() that allows table data with the matching string (from) to be replaced by new string.

The syntax of REPLACE is REPLACE(text_string, from_string, to_string)

example:

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);

 

Apache 2.x + Tomcat 4.x + Load Balancing February 12, 2009

Filed under: Apache, Java — javatechie @ 10:41 am
Tags: , ,

This article contains step by step instructions for configuring an Apache 2.x web server which handles static content and delegates JSP (Java Server Pages) and Servlet requests to two Tomcat 4.x servers using AJP 13 connectors and a load balancing worker.

Introduction

Apache 2.0 is a standards compliant, fast and mature web server which excels at delivering static content such as static HTML pages and images. The Tomcat web server is great for serving Java Server Pages and servlets, but it is not as fast as Apache for delivering static content.

In order to build a fast, scalable web application, the requirements call for an Apache server that delegates servicing of JSP and servlet requests to multiple tomcat servers by using an Apache module, mod_jk, that performs load balancing with session affinity, also known as “sticky” sessions.

Session affinity explained. When a client browser requests a JSP page for the first time, the load balancer redirects the request received by Apache to one of the two tomcat servers; further requests originating from the same client session will be automatically forwarded to the same tomcat server, so that the user’s session data is retrieved.

This document describes how I configured Apache 2.x to dispatch JSP and servlet requests to two Tomcat 4.x instances listening on different ports. This setup was done on a Linux system. Your mileage may vary.

Read more