application - servlet communication

Discussions

Web tier: servlets, JSP, Web frameworks: application - servlet communication

  1. Dear friends,
    I like to call a servlet from an application.
    But an exception is thrown which i am unable to resolve.
    Please help me.Thanks in advance.
    Application code(client):
    *************************
    import java.net.*;
    import java.io.*;

    class client
    {public static void main(String a[])
    {
    try{
    URL u=new URL("http","sun",7001,"servtest");
    URLConnection cu=u.openConnection();
    cu.setUseCaches(false);
    cu.setDoOutput(true);
    OutputStream o=cu.getOutputStream();
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    DataOutputStream dos1=new DataOutputStream(bos);
    dos1.writeUTF("hello");
    dos1.writeInt(-1);
    dos1.flush();
    byte buf[]=bos.toByteArray();
    cu.setRequestProperty("Content-type","application/octet-stream");
    cu.setRequestProperty("Content-length",""+buf.length);
    DataOutputStream dos=new DataOutputStream(o);
    dos.write(buf);
    dos.flush();
    dos.close();
    cu.setDoInput(true);
    InputStream i=cu.getInputStream();
    DataInputStream dis=new DataInputStream(i);
    String ss=dis.readUTF();
    dis.close();
    }catch(Exception e){e.printStackTrace();}
    }}

    Exception in client:
    ********************
    Exception in thread "main" java.lang.IllegalAccessError: Already connected
            at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpUR
    LConnection.java:804)
            at client.main(client.java:21)


    Servlet:
    *********

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class servtest extends HttpServlet
    {
    public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException
    {
    System.out.println("testing");
    InputStream is = req.getInputStream();
    byte b[]=new byte[1000];
    int j=0;
    int c=0;
    int con=req.getContentLength();
    DataInputStream dis=new DataInputStream(is);
    String s=dis.readUTF();
    int i=dis.readInt();
    dis.close();
    res.setContentType("application/octet-stream");



    OutputStream os = res.getOutputStream();
    DataOutputStream dos=new DataOutputStream(os);
    dos.writeUTF("hi");
    dos.close();
    }}

    Exception in server side:
    *************************
    java.net.SocketException: Connection reset by peer: JVM_recv in socket input str
    eam read
            at java.net.SocketInputStream.socketRead(Native Method)
            at java.net.SocketInputStream.read(SocketInputStream.java:90)
            at weblogic.socket.ResettableSocket.getPrefix(ResettableSocket.java, Com
    piled Code)
            at weblogic.socket.JVMSocketT3.claimSocket(JVMSocketT3.java, Compiled Co
    de)
            at weblogic.socket.JVMSocketT3.claimSocket(JVMSocketT3.java:97)
            at weblogic.socket.JVMSocketManager.accept(JVMSocketManager.java:379)
            at weblogic.t3.srvr.ListenThread$RJVMListenRequest.execute(ListenThread.
    java:506)
            at weblogic.kernel.ExecuteThread.run(ExecuteThread.java, Compiled Code)
  2. hi see the small changes.........


    import java.net.*;
    import java.io.*;

    class client
    {
    public static void main(String a[])
    {
    try
    {
    URL u=new URL("http://localhost:8080/servlet/servtest");
    URLConnection cu=u.openConnection();
    cu.setUseCaches(false);
    cu.setDoOutput(true);
    cu.setDoInput(true);
    ByteArrayOutputStream bos=new ByteArrayOutputStream();
    DataOutputStream dos1=new DataOutputStream(bos);
    dos1.writeUTF("");
      dos1.write(-1);
    /* don't use writeInt or readInt i don't know but it is giving error for negative number */


    dos1.flush();
    dos1.close();
    byte buf[]=bos.toByteArray();
    System.out.println("Buf length " +buf.length);
    cu.setRequestProperty("Content-type","application/octet-stream");
    cu.setRequestProperty("Content-length",""+buf.length);
    OutputStream o=cu.getOutputStream();

    DataOutputStream dos=new DataOutputStream(o);
    dos.write(buf);
    dos.flush();
    dos.close();

    InputStream i=cu.getInputStream();
    DataInputStream dis=new DataInputStream(i);
    String ss=dis.readUTF();
    System.out.println(ss);
    dis.close();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }




    for servlet

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    public class servtest extends HttpServlet
    {
    public void doPost(HttpServletRequest req,HttpServletResponse res)
    {
    System.out.println("testing123");
    try
    {
    System.out.println("before input stream");
    InputStream is = req.getInputStream();
    System.out.println("after input stream "+ is);
    byte b[]=new byte[1000];
    int j=0;
    int c=0;

    int con=req.getContentLength();
    DataInputStream dis=new DataInputStream(is);
    String s=dis.readUTF();
    int i=dis.read();
    System.out.println("The client val "+ s +" inttdfdd " + i);
    dis.close();
    res.setContentType("application/octet-stream");
    OutputStream os = res.getOutputStream();
    DataOutputStream dos=new DataOutputStream(os);
    dos.writeUTF("hi");
    dos.close();
    }
    catch(Exception ee)
    {
    System.out.println("Error "+ ee);
    }
    }}



    if any problem let me know
    bye
    hope this should solve ur problem & if u can find out something abt readInt & writeInt let me know its not working with negative number i tested this application in tomcat server.
    bye
    -kiran
  3. Thank you kiran.
    As you mention i placed cu.setDoInput(true) before getting outputstream and code was working.read and writeInt() methods work fine with negative numbers.I am using weblogic server.
  4. Hello Shankar,
        I am not sure if I am right. But I guess the problem U have is because, after u open the connection to the URL and using it(for writing the byes), ur are changing the attributes of the same session and trying to read the bytes. Which is the reason, u r getting that exception. U can set up the properties of ur connection before writing any thing on to stream. In the example code below, I have done just that and it works perfectly.
    I hope this helps u.

    Code is As Below:


                URL url = new URL(servletURL);
                URLConnection uc = url.openConnection();
                uc.setUseCaches(false);
                uc.setDoOutput(true);
                uc.setDoInput(true);
                uc.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

                OutputStream os = uc.getOutputStream();
                DataOutputStream dos = new DataOutputStream(os);
                dos.writeBytes(paramBuf.toString());
                
                dos.flush();
                dos.close();
                os.close();
                Logger.println("Sent the message");
                
                //read the response
                InputStreamReader in = new InputStreamReader
                    uc.getInputStream());
                StringBuffer resBuf = new StringBuffer();
                String response;
                int chr = in.read();
            
                while(chr != -1)
                {
                    resBuf.append(String.valueOf((char) chr));
                    chr = in.read();
                }
           
                in.close();



    regards,
    Raghu.K.N.