Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6854561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:38:55+00:00 2026-05-27T01:38:55+00:00

hey i stucked in the problem for just Login here is my servlet /*

  • 0

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





}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:38:55+00:00Added an answer on May 27, 2026 at 1:38 am

    In the sender you have basically writeUTF("successfull") followed by two write()s of byte arrays. In the receiver you basically have if readUTF() == "successfull" then if so you do two more readUTF()s. However readUTF() can only read what a writeUTF() has written: see the Javadoc. It certainly can’t read the result of a write() with an arbitrary byte[] array. You need to sort out what your application protocol actually is.

    You also have too many ByteArrayInputStreams and ByteArrayOutputStreams 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I am getting this error: error: conversion to non-scalar type requested Here are
Hey guys! I have this little problem: I have one ViewController which adds 2
Hey am just curious to know if this can be done! I want to
Hey, I am Just Learning CoffeeScript and I Keep getting Errors. Here is my
Hey, PHP newbie here when I try to run this to get an IP
Hey guys, I've been bashing my head around for hours now, over this login
hey all.i'm newbie at this problem.i have this data in table result: item range_code
hey guys having this really simple problem but cant seem to figure out have
Hey guys here is my php: $x = array(one, two, three); foreach ($x as
Hey all here are my tables i have placed in a DIV for each.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.