嘿,我只是因为登录而陷入问题

这是我的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;
        }
    }

}


和我正在获得响应的LoginSrv类

在字符串su = indat.readUTF();中获取“ javaioEOF异常”。

 /*
 * 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);
        }

    }

}


这是我正在连接的ServCon.java

    /*
 * 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;
    }





}

最佳答案

在发送方中,您基本上具有writeUTF("successfull"),后跟两个write()字节数组。在接收器中,您基本上具有if readUTF() == "successfull",如果是,则再执行两个readUTF()。但是readUTF()只能读取writeUTF()编写的内容:请参阅Javadoc。它肯定无法读取具有任意write()数组的byte[]的结果。您需要理清您的应用程序协议实际上是什么。

您也喜欢我的ByteArrayInputStreamByteArrayOutputStream太多。这些只会增加延迟和出现上述错误的机会。如果您有东西要写,请将其直接写到输出流;如果您认为有什么要阅读的内容,请从输入流中阅读。

09-27 20:56