Sunday, October 11, 2009

Configure debug port in Tomcat 5.5

I have installed Tomcat 5.5 using setup.exe. After the installation I get following two executable files in my %TOMCAT_HOME%/bin directory:
  • tomcat5.exe - which launches the server.
  • tomcat5w.exe - which allows you to configure tomcat.
Launch tomcat5w.exe and click on Java tab. Under java options add the following line:

-Xrunjdwp:transport=dt_socket,address=8453,server=y,suspend=n

where 8453 is your socket number. Now in eclipse or any other IDE you can easily configure options to debug the application.

I haven't tried this on tomcat's other distribution in which you simple unzip the files for installation. In this case you have to edit catalina.bat file. That's all information I got from various forums.

Smile !!! :)
-- Updated 23rd August, 2011--
In case if you have unzipped tomcat or your are on Linux machine, then "tomcat5w.exe" won't work. In that case, instead of starting tomcat with "startup.sh" script, execute the following command.
catalina.bat jpda start
You can do the same on windows as well. By default your debug port will be 8000.
Thanks.

Monday, September 21, 2009

ERROR 2003: Can't connect to MySQL server (10060)

ERROR 2003: Can't connect to MySQL server (10060)

Well, finally I rectified this error after doing google and research for 3.5 Hours.

I installed mysql database on my machine and I was trying to access it from the remote machine using MySQL query browser. But it was giving the error. If you are also facing the same problem then here are couple of things you can try.

  • If you have antivirus/firewall installed then make sure that it is not blocking traffic for port 3306(default port for mysql).

  • I have installed/configured MySQL to run as a service in windows box. If you have the same configuration, then open the Computer Management by Manage.

    Go to Service under Services And Applicaton.

    Look for mysql service and double click it to check the properties.

    The executable path should be like this: "C:\Program Files\MySQL\MySQL Server 5.0\bin\mysqld-nt" --defaults-file="C:\Program Files\MySQL\MySQL Server 5.0\my.ini" MySQL

    Now it uses "my.ini" file for the configuration. Open this file on text editor. Under [mysqld] section add following line:

    bind-address=0.0.0.0

    You can give any ip address, if you want to listen only to localhost then you can give 127.0.0.1. The above configuration will listen to all ip addresses.

    If you are on linux machine, then you have to edit /etc/mysql/my.cnf file.


Hope this helps !!!
Enjoy !! :)

Thursday, June 18, 2009

Special characters in web application

I've been adding text resources in my application so as to display the web application in differnt languages. The problem I faced was that whenever a special characted comes the browser display's "?". The browser is not able to display the special character inspite of selecting UTF-8 language.

So I made some changes in the code to handle UTF-8 character set.

Make sure that all JSP pages have
<%@ page contentType="text/html; charset=UTF-8"%>

In case if you are having html pages, use meta tag to add character set information.
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

Second, Make changes in the server's configuraton file. I am using Sun's web server. So in my sun-web.xml file I added <parameter-encoding default-charset="UTF-8"/> to look like

<sun-web-app>
<parameter-encoding default-charset="UTF-8"/>
.
.
</sun-web-app>

In case of apache tomcat, you have specify URIEncoding attribute in Connector tag in server.xml.

But this is what something server will be handling, and it's in not our hand. To make sure that application uses UTF-8 character set, a filter can bge added to web application.

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;

import java.io.IOException;

public class CharacterEncodingFilter implements Filter
{

private FilterConfig fc;

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException
{

HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");

chain.doFilter(request, response); //do it again, since JSPs will set it to the default

response.setContentType("text/html; charset=UTF-8");
request.setCharacterEncoding("UTF8");
}

public void init(FilterConfig filterConfig)
{

this.fc = filterConfig;
}

public void destroy()
{

this.fc = null;
}
}

Once you are done writiing the java class, add the filter configuration to the web.xml file.

<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.its.struts.action.CharacterEncodingFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>


That's all !!! :)

Saturday, March 14, 2009

Array Binding In Spring

Sending data from JSP to controller in easy no matter whether it is string, integer or an array. Problem occurs in displaying JSP page if the data in present in command class and it is an array.
Following is sample JSP code to bind an array of names to the command class in spring. The code will both send and display the data.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="core" uri="http://java.sun.com/jstl/core" %>
<html>
<head>
<title>Test</title>
</head>
<body>
<form:form commandName="test" method="post" action="welcome.do" >
    <core:set var="flag" value="false" />
<!-- Create the n number of name fields if data is present -->
    <core:forEach items="${test.name}" var="n" varStatus="status">
        <form:input path="name[${status.index}]"  />
        <core:set var="flag" value="true" />
    </core:forEach>
<!-- If data is not present then create 2(default) name fields -->
    <core:if test="${flag eq false}">
            <form:input path="name"  />
            <form:input path="name"  />
    </core:if>
    <input type="submit" value="submit" />
</form:form>
</body>
</html>


In the above example String[] name; is used in the command class.
That All !!! :)

Saturday, February 28, 2009

XML Parsing

There is a high rise in information exchange between application and XML has become the standard. Here is an simple basic XML parsing example which gives an basic idea of fetching information from an XML file.

The sample XML file:
<?xml version="1.0"?>
<company name="my comapany">
<employee id="1">
<name>Mike</name>
</employee>
<employee id="2">
<name>Mark</name>
</employee> </company>
Java code to read the XML file:

package pkg;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;import org.w3c.dom.Element;
import org.w3c.dom.NodeList;import org.xml.sax.SAXException;

public class XMLReader {
public void readXML(String fileName) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.parse(new File(fileName));
//GET THE ROOT ELEMENT
Element root = document.getDocumentElement();
System.out.println("Root: " + root.getNodeName());
System.out.println("Company Name: " + root.getAttribute("name"));
//GET THE LIST OF CHILD ELEMENTS UNDER ROOT
NodeList employeeList = root.getElementsByTagName("employee");
System.out.println("Total employees: " + employeeList.getLength());
Element employee;
String name = null, id = null;
for(int i=0;i<employeeList.getLength();i++) {
employee = (Element)employeeList.item(i);
//FETCH ID OF EMPLOYEE
if(employee.hasAttributes()) {
id = employee.getAttributes().getNamedItem("id").getNodeValue();
}
//GET NAME OF THE EMPLOYEE name = employee.getElementsByTagName("name").item(0).getTextContent();
System.out.println("Id: " + id + "\tName: " + name);
}
}
}
Happy parsing !!! :)