Tuesday, November 24, 2009

Connect Mysql Using GWT

Today I am going to write about how to connect GWT , Mysql Using Java servlet. It's a very ease process. I will show this with a simple example of login application and parsing the result 200 success message from the server.



Steps to follow:

Crate a gwt login application with login and password filed and a button.

loginbutton.addClickListener(new ClickListener(){

public void onClick(Widget sender) {

String txt =usrtxt.getText(); //This to get the username value form the first textbox

String pwd = pwdtxt.getText(); // This to get passwordbox value.

String url ="http://localhost:8080/servlet/ServletTest?user="+txt+"&pwd="+pwd;

//This     loclhost part defines the server url. I am using jboss server. Once you have completed the login GWT build a war file and deploy it in jboss. Please note that the url has the username and password as query string.

doPost(url); // Using this method we are sending request to the java servlet with username and password.    

//The method below is the way we can send request to the servlet class which deals with the database.  

public static void doPost(String url)

{

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);

try

{

Request response = builder.sendRequest(null, new RequestCallback()

{

public void onError(Request request, Throwable exception)

{

}

public void onResponseReceived(Request request,Response response)

{

if (response.getStatusCode() == 200)

{

Window.alert(response.getText()); // This gets the response message .

} else

{

Window.alert("error");  

}

}

});

} catch (RequestException e)

{

}

}

}



You have to import below these for this doPost mthod()

import com.google.gwt.http.client.Request;

import com.google.gwt.http.client.RequestBuilder;

import com.google.gwt.http.client.RequestCallback;

import com.google.gwt.http.client.RequestException;

import com.google.gwt.http.client.Response;


That's it from the client side gwt implementation.

**************************************************
Let's see the server side code of java servlet.
package com;
import java.io.IOException;

import java.io.PrintWriter;

import java.sql.DriverManager;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;

import com.mysql.jdbc.ResultSet;

import com.mysql.jdbc.Statement;


/**

* Servlet implementation class ServletTest

*/

public class ServletTest extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

// connecting to database

java.sql.Connection con = null;

java.sql.Statement stmt = null;

java.sql.ResultSet rs = null;

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

try {

con =DriverManager.getConnection ("jdbc:mysql://mysql url:3306/example",

"username", "password");

} catch (SQLException e) {

e.printStackTrace();

}

try {

stmt = con.createStatement();

} catch (SQLException e) {

e.printStackTrace();

}

try {

rs = stmt.executeQuery("SELECT * FROM servlet");

} catch (SQLException e) {

e.printStackTrace();

}

String username ="xxxx";

String password = "xxxx";

String result = "hey there"+username;

response.setContentType("text/html");

PrintWriter out = response.getWriter();

if((request.getParameter("user")!=null)&&(request.getParameter("pwd")!=null)){

if(request.getParameter("user").trim().equals(username)&&(request.getParameter("pwd").trim().equals(password))){

out.write(result);

}

}else{

out.write("get the hello out of here");

}

}

}


That's it you can now connect with Mysql using GWT with Servlet.

I will be posting a detailed description of retrieving values from database and sending response to the client using xml by defining xsd.

If you find any mistakes and any improvements need to be done in the code. Please post here. J

1 comment:

Anonymous said...

Could you upload the source code?