applet servlet communication with httpconnection.

hi

I have to connect applet to the servlet through http connection i am using httpurlconnection.

Problem is that i want to save time in connecting applet to servlet on every click of button i created the function to connect to servlet but i can't call this function in init() method i have to call this function every times i need to connect to servlet.So if anyone has idea how can i connect to servlet on time only and use the same url again and again.

here is the code.

private void createConnection() {

try {

ServletURL = new URL("http://localhost:8582/servlet/servlets.DispatcherRequest");

//ServletURL= new URL(getCodeBase(),"/servlet/servlets.DispatcherRequest");

ServletConnection = (HttpURLConnection) ServletURL.openConnection();

//set connection for input and out mode and set no cache for updated results.

ServletConnection.setDoOutput(true);

ServletConnection.setDoInput(true);

ServletConnection.setRequestProperty("Connection", "Keep-Alive");

ServletConnection.setUseCaches(false);

ServletConnection.connect();

} catch (Exception e) {

jTextAreaMsg.setText(e.getMessage());

}

}

so i have to call this function everytime i need to use servlet. and perform following task.

doProcessing(){

try {

//output stream to write the data

OutputStreamWriter outWrite = getHttpOutputStream();

String sTemp = new StringBuilder().append("{ProcessCode}").append(sBarCode).append("<").toString();

outWrite.write(sTemp);

outWrite.flush();

outWrite.close();

//input data stream

InputStream in = getHttpInputStream();

int chr;

while ((chr = in.read()) != -1) {

sBuff.append((char) chr);

}

in.close();

} catch (Exception e) { jTextAreaMsg.append(e.toString()); }

}

Is there any way that i can use createconnection() function as global object and use it when i need.

If i use createConnection() and then immidiatly doProcessing() function then it works but if i store createconnection() in init() block it work first time but on next click of button it gives error "java.net.ProtocolException: Cannot write output after reading input."

Please help me guys

[2292 byte] By [amitwaliaa] at [2007-10-2 5:53:39]
# 1

i am doing the same thing. but i m not able to invoke Servlet method.

Plz help..

Applet Code

URL cBase=getCodeBase();

String codeBase = cBase.toString();

int iSlash= codeBase.lastIndexOf("/");

codeBase= codeBase.substring(0,iSlash);

iSlash= codeBase.lastIndexOf("/");

codeBase= codeBase.substring(0,iSlash);

System.out.println("codebase= " + codeBase);

String location = codeBase + "/AttrEditorServlet";

URL url = new URL(location);

//URL url = new URL(cBase, "AttrEditorServlet");

//URL url = new URL("http","10.192.10.231",8080,"/attrEditorServlet/AttrEditorServlet");

HttpURLConnection con = (HttpURLConnection )url.openConnection();

con.setRequestProperty("Connection", "Keep-Alive");

con.setDoOutput(true);

con.setDoInput(true);

con.setUseCaches(false);

System.out.println("val of bad request = " + con.HTTP_BAD_REQUEST);

System.out.println("http url val bad request = " + HttpURLConnection.HTTP_BAD_REQUEST);

//con.connect();

System.out.println("get URL = " + con.getURL());

if(con.HTTP_BAD_REQUEST == HttpURLConnection.HTTP_BAD_REQUEST)

{

System.out.println("bad request");

}

System.out.println("URL = " + con.getURL());

OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());

out.write(connectionStr);

out.flush();

Server Code

protected void doPost (HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

ObjectOutputStream outputToApplet = null;

ObjectInputStream inputFromApplet = null;

Date DateToday = null;

try

{

System.out.println("inside dopost method");

//Servlet recieve connection string as an input from applet

//e.g. = "jdbc:oracle:thin:tifeur_ws_and/tifeur@10.192.252.27:1521:bacosdb"

/// and then return an image to applet.

inputFromApplet = new ObjectInputStream(request.getInputStream());

System.out.println("Read Connection String from Applet : " + inputFromApplet.readObject());

outputToApplet = new ObjectOutputStream(response.getOutputStream());

//String str = new String("- this message is from servlet-");

/**

* send awt.image object from servet to Applet

*/

outputToApplet.writeObject("Send Image to applet");

outputToApplet.flush();

outputToApplet.close();

inputFromApplet.close();

}

catch( Exception e)

{

e. printStackTrace();

}

}

Vishva_pratapa at 2007-7-16 2:03:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

to invoke a servlet u must set the web.xml file or u hav eto use servlet invoker.

use servlet mapping that is more secure.

what is ur context name and servlet name

what is this

attrEditorServlet/AttrEditorServlet

is attrEditorServlet ur context or folder or package..........?

did u do that...........?

amitwaliaa at 2007-7-16 2:03:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

thnks foy ur reply..

my web.xml is.

<web-app>

<description>Embedded OC4J</description>

<taglib>

<taglib-uri>http://xmlns.oracle.com/j2ee/jsp/tld/spatial/mvtaglib.tld</taglib-uri>

<taglib-location>/WEB-INF/mvtaglib.tld</taglib-location>

</taglib>

<servlet>

<servlet-name>AttrEditorServlet</servlet-name>

<servlet-class>mypackage.servlet.AttrEditorServlet</servlet-class>

<!--<load-on-startup>1</load-on-startup>-->

</servlet>

<servlet-mapping>

<servlet-name>AttrEditorServlet</servlet-name>

<url-pattern>/AttrEditorServlet/*</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>jsp/webmap.jsp</welcome-file>

</welcome-file-list>

</web-app>

URL is = " http://10.192.10.231:8080/AttributeEditor/AttrEditorServlet"

if i run this url from addrress bar of Browser it invokes first init() method and doGet() method()

so i think there is no problem with url. something is wrong in code. or should it depends on web server (tomcat)setting.

Vishva_pratapa at 2007-7-16 2:03:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hey Amit,

I am not sure if I have understood your requirement correctly.

But on preliminary analysis I think this would help:

Make a different class and have your createConnection() in it.

Make an object of this class in your applets init() and initialize it.

Rohan

Rohan__Kamata at 2007-7-16 2:03:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

ok if thr is no problem in calling the servlet then i can see only one possibility you are calling do post method and in my knowledge applet cannot call do post method directly , applet calls service method only so if you copy ur code in service method then it should work another thing in ur web.xml file if u change ,just give it a try, servlet mapping tag from "/AttrEditorServlet/*" to "/AttrEditorServlet" only it should definatly work .give it a go.

amitwaliaa at 2007-7-16 2:03:09 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...