Friday, February 28, 2014

Remote Debugging Tomcat 6 using SSH tunnel

My current IDE is Intellij Idea 12 and it is running on Windows 7 machine. However, server exist on one of the RedHat Enterprise machines. The main challenge is to debug the remote tomcat server via SSH port 22 only because that is the only allowed path. Even I cannot ping the server.

Instead of catalina.sh, I have to always run the "sudo service tomcatx start|stop" because tomcat is installed as a service using redhat yum. From this blog I found the details to setup the tomcat. I changed the TOMCAT_HOME/conf/tomcat6.conf file to have the following JAVA_OPTS

JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,address=15005,server=y,suspend=n"

This is just a one line.

As shown in the above, the port is address that is 15005. Now I have to create the SSH connection using Putty as shown in the below

source port: 15005
Destination: localhost:15005

After add as shown in the above screenshot, you have to start the session. 

Now you have to configure the new remote debugging session in the Intellij Idea 12.0.6.

Now you are ready to debug the remote tomcat server.
However, currently I am facing a problem that is :
"each time I need to restart the server, I have to remove the above JAVA_OPTS line from the tomcat6.conf file". After I stop the server, again insert the line and start the server.  

Wednesday, September 11, 2013

JavaScript 1: history object

The top most object in the Browser Object Model (BOM) is the “window”. I have three files

  1. history.html:  Start
  2. file1.html: Middle
  3. file2.html: Last

In the Start, you can click the “go to middle” to visit the page file1.html. In this page you have two buttons, when you click “Back” it should take you back to the history.html, otherwise you can go to the last page click by “go to end”. In the “End” page you can go to the page entering -2 for “Start” or –1 for “Middle”.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Start</h1>
<a href="file1.html">go to middle</a>
</body>
</html>

As shown in the above code, there is just one link to go to the middle page.

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1> In the Middle, one to go</h1>
<a href="file2.html">go to end</a>
<input type="button" value="back" onclick="return window.history.back()">
<input type="button" value="forward" onclick="return window.history.forward()">
</body>
</html>

As shown in the above code, you have two option to going back link to go to the “End” page. In the first round you cannot use the “forward” button because still you haven’t visit the next page.

<!DOCTYPE html>
<html>
<head>
<title></title>
<title></title>
<script type="text/javascript" language="JavaScript">
function goPage(){
var pageToGo = parseInt(pg.value);
window.history.go(pageToGo)
}
</script>
</head>
<body>
<h1>End</h1>
<input type="button" value="back" onclick="return window.history.back()">
<br/>
<label>enter the page to go</label><input type="text" id="pg">
<input type="button" value="Go" onclick="goPage()">
</body>
</html>

As shown in the above listing, you can go back or the any page recorded in the history, for example first page entering the value –2. Back always – and forward always +.

Saturday, July 27, 2013

JAX-RS access via JSON using jQuery

This is the continuation of the blog  JSF 2.0 Ajax.

In the blog JSF 2.0 Ajax, I’ve considered the Ajax access using JSF 2.0. In JSF, you have to always use the Managed bean. As I see managed bean is over managed therefor more code need to be added to avoid the unnecessary side effects such as save twice. The REST approach is simple but unsecure.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">

<h:head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
</h:head>
<body>
<form action="/eafirstweb/rest/dept" id="deptForm">
Department:
<input type="text" name="dept_Name"/>
<input type="submit" value="Add"/>
</form>
<span id="msg"></span>
<script type="text/javascript">
$("#deptForm").submit(function(event){
//stop from submitting normal way
event.preventDefault();

var $form = $(this);
//var deptName = $form.find('input[name="dept_Name"]').val();
var url = $form.attr('action');

//send the data using POST
/*var posting = $.post(url, deptName).done(function(data){
alert(data);
});*/
$.ajax({
type : 'POST',
url : url,
data : $form.serialize(),
success : function(data){
var table ='<table><tr><th>Dept Id</th><th>Dept Name</th></tr>';
$.each(data, function(index, item){
table += '<tr><td>'+item.deptId+'</td><td>'+item.name+'</td></tr>';
});
table += '</table>';
$('#msg').html(table)
}
});

});
</script>

</body>
</html>

In the above code, form data is serialized and send to the url where the REST service is available. The following code shows the services which are written in JAX-RS

package au.com.blogspot.ojitha.eafirstweb.rest;

import java.util.List;

import javax.ejb.EJB;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import au.com.blogspot.ojitha.eafirstejb.DepartmentService;
import au.com.blogspot.ojitha.eafirstejb.domain.Department;

@Path("dept")
public class DeptRestService {

@EJB(beanName="deptService")
DepartmentService deptService;

@SuppressWarnings("unused")
@Context
private UriInfo context;

/**
* Default constructor.
*/
public DeptRestService() {

}

/**
* PUT method for updating or creating an instance of Hello
* @param content representation for the resource
* @return an HTTP response with content of the updated or created resource.
*/
@PUT
@Consumes("text/html")
public void putHtml(String content) {

}

@GET
@Path("{deptId}")
@Produces("application/json")
public Department getDepartment(@PathParam("deptId") Long deptId){
Department dept =this.deptService.findDepartmentById(deptId);
return dept;
}
@Produces("application/json")
@GET
public List<Department> getAllDepartements(){
return this.deptService.getAllDepartments();
}
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces(MediaType.APPLICATION_JSON)
public List<Department> addDept( @FormParam("dept_Name") String deptName){
this.deptService.save(deptName);
return this.deptService.getAllDepartments();
}

}

As shown in the above code, the target method is addDept. In that method, form parameter “dept_Name” is extracted using @FormParam annotation process. In the next line save the new department. As a return, send back all the available department as a JSON type back to the page. In the page, in the success function, these data being written in to the table.

 

image

Above picture shows the resulting web page with Firebug xhr requests and response.

The source code of this blog is available in the GitHub.