Java Source Code For Sample Server for a Mobile Client
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import org.bouncycastle.crypto.*;
public class Servlet_TestServer extends HttpServlet
{
static final int iBUFFER_SIZE = 2048;
ServletOutputStream sous;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
defaultAction(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
defaultAction(req, res);
}
public void defaultAction(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
doServerFunctions(req, res);
}
public void doServerFunctions(HttpServletRequest req,
HttpServletResponse res) throws
ServletException, IOException
{
BufferedInputStream buffinFlexCubeLink = null;
ByteArrayOutputStream byteoutFlexCubeLink = new ByteArrayOutputStream();
boolean bIsValidXml = false;
String sRecievedText = "";
sous = res.getOutputStream(); //To send the response
String sFldUniqueId = "";
System.out.println(req.toString());
try
{
String message = req.getParameter("id");
System.out.println("Inside Servlet_TestServer:Step 1 : " + message);
buffinFlexCubeLink = new BufferedInputStream(req.getInputStream());
int iNoofTotalBytes = 0;
while (true)
{
System.out.println("Inside Servlet_Server:Step 2");
byte[] baInputData = new byte[iBUFFER_SIZE];
int iNoofBytesRead = buffinFlexCubeLink.read(baInputData, 0,iBUFFER_SIZE);
byte[] baRawData = new byte[iNoofBytesRead];
int iCounter = 0;
while (iNoofBytesRead > iCounter)
{
baRawData[iCounter] = baInputData[iCounter];
iCounter++;
}
sRecievedText = new String(baInputData);
iNoofBytesRead = baInputData.length;
System.out.println(new String(baInputData) + "********" +
baInputData.length);
if (iNoofBytesRead > 0)
{
byteoutFlexCubeLink.write(baInputData, 0, iNoofBytesRead);
}
else
{
System.out.println("Empty *****");
}
iNoofTotalBytes = iNoofTotalBytes + iNoofBytesRead;
if (iNoofBytesRead != iBUFFER_SIZE)
{
System.out.println("iNoofBytesRead : " + iNoofBytesRead);
break;
}
}
catch (IOException e)
{
System.out.println(
"Error in opening the OutputStream ");
}
}
}
}
catch (IOException e)
{
}
finally
{
try
{
byteoutFlexCubeLink.close();
// BufferedInputStream is not closed since it closes the Socket
// as well.
buffinFlexCubeLink = null;
}
catch (IOException e)
{
}
}
return;
}
}
If anyone need any help on this topic contact me by an email.
Always ready to help
:)
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.Socket;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import org.bouncycastle.crypto.*;
public class Servlet_TestServer extends HttpServlet
{
static final int iBUFFER_SIZE = 2048;
ServletOutputStream sous;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
defaultAction(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
defaultAction(req, res);
}
public void defaultAction(HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{
doServerFunctions(req, res);
}
public void doServerFunctions(HttpServletRequest req,
HttpServletResponse res) throws
ServletException, IOException
{
BufferedInputStream buffinFlexCubeLink = null;
ByteArrayOutputStream byteoutFlexCubeLink = new ByteArrayOutputStream();
boolean bIsValidXml = false;
String sRecievedText = "";
sous = res.getOutputStream(); //To send the response
String sFldUniqueId = "";
System.out.println(req.toString());
try
{
String message = req.getParameter("id");
System.out.println("Inside Servlet_TestServer:Step 1 : " + message);
buffinFlexCubeLink = new BufferedInputStream(req.getInputStream());
int iNoofTotalBytes = 0;
while (true)
{
System.out.println("Inside Servlet_Server:Step 2");
byte[] baInputData = new byte[iBUFFER_SIZE];
int iNoofBytesRead = buffinFlexCubeLink.read(baInputData, 0,iBUFFER_SIZE);
byte[] baRawData = new byte[iNoofBytesRead];
int iCounter = 0;
while (iNoofBytesRead > iCounter)
{
baRawData[iCounter] = baInputData[iCounter];
iCounter++;
}
sRecievedText = new String(baInputData);
iNoofBytesRead = baInputData.length;
System.out.println(new String(baInputData) + "********" +
baInputData.length);
if (iNoofBytesRead > 0)
{
byteoutFlexCubeLink.write(baInputData, 0, iNoofBytesRead);
}
else
{
System.out.println("Empty *****");
}
iNoofTotalBytes = iNoofTotalBytes + iNoofBytesRead;
if (iNoofBytesRead != iBUFFER_SIZE)
{
System.out.println("iNoofBytesRead : " + iNoofBytesRead);
break;
}
}
catch (IOException e)
{
System.out.println(
"Error in opening the OutputStream ");
}
}
}
}
catch (IOException e)
{
}
finally
{
try
{
byteoutFlexCubeLink.close();
// BufferedInputStream is not closed since it closes the Socket
// as well.
buffinFlexCubeLink = null;
}
catch (IOException e)
{
}
}
return;
}
}
If anyone need any help on this topic contact me by an email.
Always ready to help
:)
Comments