The Artima Developer Community
Sponsored Link

Java Answers Forum
applet-servlet socket communication

1 reply on 1 page. Most recent reply: Oct 13, 2008 5:14 AM by Guy Guy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
gini gini

Posts: 1
Nickname: sakshi
Registered: Mar, 2006

applet-servlet socket communication Posted: Mar 10, 2006 2:43 AM
Reply to this message Reply
Advertisement
plzz help me out to make servlet applet socket communication .I am getting null pointer exception
I am doing at tomcat5.0.28,using j2sdk1.4._03,using serialization
the code for APPLET IS AS:-

import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.security.*;
import java.io.*;

public class applet extends JApplet
{
static int port;

public void init()
{
showStatus("Initialising...");
Socket sock = null;
//port=Integer.parseInt(getParameter("Port"));
ObjectOutputStream out;
ObjectInputStream in;
showStatus("Creating Socket...");
try
{
sock=new Socket(getCodeBase().getHost(),9999);
out=new ObjectOutputStream(sock.getOutputStream());
in=new ObjectInputStream(sock.getInputStream());
}
catch(Exception e)
{
showStatus("Error in opening Client Socket..."+ e.getClass().getName() + ": " + e.getMessage());
}

}//init()
}
******************************************
CODE FOR SERVLET IS:-
**************************************************
import java.awt.*;
import javax.swing.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.security.*;

public class servlet extends HttpServlet
{
ServerSocket serverSock;
Socket clientSocket;
int port=9999;
ObjectOutputStream oos;
ObjectInputStream ois;
public void init(ServletConfig sc) throws ServletException
{
super.init(sc);
try
{
serverSock = new ServerSocket(port);
}
catch (IOException e)
{
System.out.println("Could not listen on port: 6001");
}
}


public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title> HCL InfoWall </title></head>");

out.println("<APPLET CODE=applet.class WIDTH=760 HEIGHT=375>");

//out.println("<PARAM NAME=Port VALUE = 6001>");
out.println("</APPLET></HTML>");

try
{
clientSocket = serverSock.accept();
}
catch (IOException e)
{
out.println("Accept failed: 6004");
}
try{
oos=new ObjectOutputStream(clientSocket.getOutputStream());
ois=new ObjectInputStream(clientSocket.getInputStream());
}
catch(Exception e)
{
out.println("Objects in[put-output not Initialize becoz:"+e);
}
out.close();

}//doget
}//class



THANKS IN ADVANCE..PLZZ HELP ME OUT..OR SEND THE SIMPLE EXAMPLE WHICH I CAN DIRECTLY TRY OUT

THANKS AGAIN


Guy Guy

Posts: 1
Nickname: guyso
Registered: Oct, 2008

Re: applet-servlet socket communication Posted: Oct 13, 2008 5:14 AM
Reply to this message Reply
hi boy try to send to content on the servlet side first

urlCon.setRequestProperty("Content-Type", "application/x-java-serialized-object");

Flat View: This topic has 1 reply on 1 page
Topic: Going 32 on 64 Previous Topic   Next Topic Topic: guess the number game

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use