hey i stucked in the problem for just Login
here is my servlet
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
*
* @author amolbh
*/
public class AreaServlet extends HttpServlet {
private Connection connection = null;
private PreparedStatement prepStmt=null;
private String farid,password,farid1;
private ResultSet rst=null;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
if(request.getServletPath().equals("/login"))
loginValidation(request,response);
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>
public void loginValidation(HttpServletRequest request, HttpServletResponse response)throws IOException
{
DataInputStream in = new DataInputStream((InputStream)request.getInputStream());
byte indata[] = new byte[request.getContentLength()];
in.readFully(indata,0,indata.length);
in = new DataInputStream(new ByteArrayInputStream(indata));
farid = in.readUTF();
password = in.readUTF();
DataOutputStream daos = new DataOutputStream((OutputStream)response.getOutputStream());
String s1,s2;
ByteArrayOutputStream baosdata = new ByteArrayOutputStream();
DataOutputStream doasdata = new DataOutputStream(baosdata);
try
{
connection=getConnection();
prepStmt=connection.prepareStatement("Select Username,Password from Login ");
rst=prepStmt.executeQuery();
int c = 0;
while(rst.next())
{
s1=rst.getString(1);
s2=rst.getString(2);
doasdata.writeUTF(s1);
doasdata.writeUTF(s2);
c++;
}
ByteArrayOutputStream baoshdata = new ByteArrayOutputStream();
DataOutputStream doashdata = new DataOutputStream(baoshdata);
if(c != 0)
{
doashdata.writeUTF("successfull");
// doashdata.writeInt(c);
doashdata.write(baosdata.toByteArray());
daos.write(baoshdata.toByteArray());
}
else{
doashdata.writeUTF("Invalid User");
daos.write(baoshdata.toByteArray());
}
}
catch(Exception e)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream daose = new DataOutputStream(baos);
daose.writeUTF(e.toString());
daos.write(baos.toByteArray());
}
}
public Connection getConnection() throws NamingException, SQLException
{
try {
/*InitialContext ic=new InitialContext();
* DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/ImageCapture");
* connection = ds.getConnection();
* return connection;*/
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// Step 2: Establish the connection to the database.
String url = "jdbc:odbc://localhost:8080/ImageCapture";
Connection conn = DriverManager.getConnection(url);
return conn;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
return null;
}
}
}
and the LoginSrv class where i am getting ther responce
GETTING “javaioEOF exception” EXCEPTION AT String su = indat.readUTF();
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.marico.model;
import com.sun.lwuit.Dialog;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import com.marico.esaff.AppConstants;
/**
*
* @author sandipp
*/
public class LoginSrv {
private ServCon srv;
public static String st1,st2,log,pas;
public static int it1;
public LoginSrv(String s1, String s2)
{
log = s1;
pas = s2;
it1=0;
try
{
Dialog.show("Successfull","i am in Loginsrv" , null,Dialog.TYPE_INFO,null,3000);
ByteArrayOutputStream baosdata = new ByteArrayOutputStream();
DataOutputStream daosdata = new DataOutputStream(baosdata);
daosdata.writeUTF(s1);
daosdata.writeUTF(s2);
srv = new ServCon(new AppConstants().str1,null,baosdata.toByteArray(),false,false,null);
ByteArrayOutputStream obj = (ByteArrayOutputStream)srv.startTransfer();
byte[] record = obj.toByteArray();
ByteArrayInputStream instr = new ByteArrayInputStream(record);
DataInputStream indat = new DataInputStream(instr);
if(srv.getRc() == 200)
{
Dialog.show("Successfull","i am in Loginsrv "+srv.getRc() , null,Dialog.TYPE_INFO,null,3000);
String su = indat.readUTF();
// if(su.equals("successfull"))
{
st1 =indat.readUTF();
st2 =indat.readUTF();
System.out.println("st1 and st2 "+ st1 + " , "+st2);
Dialog.show("Successfull",su , null,Dialog.TYPE_INFO,null,3000);
}
// else
{
// Dialog.show("Error",su , null,Dialog.TYPE_INFO,null,5000);
}
}
else
{
Dialog.show("Error",srv.getRc() + " " + srv.getRm(), null,Dialog.TYPE_INFO,null,5000);
}
}
catch(Exception e)
{
Dialog.show("Error","I am in catch "+e.toString(), null,Dialog.TYPE_INFO,null,5000);
System.out.println("Im from LoginSrv constructor: "+e);
}
}
}
here the ServCon.java where i am doing connection
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.marico.model;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.util.Resources;
import com.sun.lwuit.Label;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
/**
*
* @author sandipp
*/
public class ServCon {
private String url,sendparam,indata;
private byte sendparamb[];
private boolean instring,outstring;
private String error;
private int rc;
private String rm;
private Resources r;
//private View.DisplayProgress dp;
public static com.sun.lwuit.TextArea textobj;
public ServCon(String url,String sendparam,byte[]sendparamb,boolean instring,boolean outstring,Resources r)
{
this.url = url;
this.instring = instring;
this.outstring = outstring;
this.r = r;
//this.dp = dp;
if(this.instring)
{
this.sendparam = sendparam;
}
else
{
this.sendparamb = sendparamb;
}
}
public Object startTransfer() throws Exception
{
HttpConnection c = (HttpConnection)Connector.open(url);
c.setRequestProperty("User-Agent","Profile/MIDP-1.0, Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language","en-US");
c.setRequestMethod(HttpConnection.POST);
DataOutputStream os = (DataOutputStream)c.openDataOutputStream();
if(instring)
os.writeUTF(sendparam);
else
os.write(sendparamb,0,sendparamb.length);
rc = c.getResponseCode();//getResponseCode();
rm = c.getResponseMessage();
int len = (int)c.getLength();
System.out.println( getRm() + " " + len);
DataInputStream is =(DataInputStream)c.openDataInputStream();
Object outobj = null;
if(outstring)
{
StringBuffer rdata = new StringBuffer();
int ch;
while ((ch = is.read()) != -1)
{
rdata.append((char)ch);
}
indata = rdata.toString();
outobj = indata;
}
else
{
sendparamb = new byte[len];
is.readFully(sendparamb,0,sendparamb.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream((OutputStream)baos);
dout.write(sendparamb, 0, sendparamb.length);
outobj = baos;
}
is.close();
c.close();
os.flush();
os.close();
System.out.println("Reached at the end of the httpdatafetch");
return outobj;
}
/**
* @return the rc
*/
public int getRc() {
return rc;
}
/**
* @return the rm
*/
public String getRm() {
return rm;
}
}
In the sender you have basically
writeUTF("successfull")followed by twowrite()s of byte arrays. In the receiver you basically haveif readUTF() == "successfull"then if so you do two morereadUTF()s. HoweverreadUTF()can only read what awriteUTF()has written: see the Javadoc. It certainly can’t read the result of awrite()with an arbitrarybyte[]array. You need to sort out what your application protocol actually is.You also have too many
ByteArrayInputStreams andByteArrayOutputStreams for my liking. These just add latency and opportunity for errors such as the above. If you have something to write, write it directly to the output stream; if you think you have something to read, read it from the input stream.