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 !!! :)