The Artima Developer Community
Sponsored Link

Java Answers Forum
Applet Servlet Communication

1 reply on 1 page. Most recent reply: May 25, 2006 11:27 PM by nagasrikanth chalasani

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
Nagesh Dudam

Posts: 2
Nickname: dnagesh
Registered: May, 2006

Applet Servlet Communication Posted: May 24, 2006 4:54 AM
Reply to this message Reply
Advertisement
Hello Everybody,
I want to communicate to the servlet via applet...
From Applet I want to call Servlet with one parameter using get method..

I have written all the code but I am not getting any output from it. please help me...its very urgent please...

Below is the code... there are 3 files placed at..
1) C:\Tomcat 5.5\webapps\ROOT\MyHtml.html
2) C:\Tomcat 5.5\webapps\ROOT\MyApplet.java
3) C:\Tomcat 5.5\webapps\ROOT\WEB-INF\classes\MyServlet.java

1)MyHtml.html

<HTML> <HEAD> <TITLE>Hello!</TITLE> </HEAD> <BODY> <P>
<APPLET CODE=MyApplet.class WIDTH=200 HEIGHT=50> </APPLET>
</BODY> </HTML>

2)MyApplet.java

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

import java.util.*;
import java.io.*;
import java.net.*;
import java.net.URLConnection.*;

public class MyApplet extends Applet
{
public String s1;
public String location;

public void init()
{
try
{
location = "http://localhost:8080/WEB-INF/classes/MyServlet?UserOption=AppletDisplay";
URL testServlet = new URL( location );
System.out.println("testServlet= "+testServlet);
URLConnection servletConnection = testServlet.openConnection();
ObjectInputStream inputFromServlet = null;
inputFromServlet = new ObjectInputStream(servletConnection.getInputStream());

s1 = (String) inputFromServlet.readObject();
s1="nagesh";
}
catch(Exception e)
{ }

}
public void paint(Graphics g)
{
g.drawString(location,10,10);
}

}

3)MyServlet.java

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class MyServlet extends HttpServlet
{

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ObjectInputStream inputFromApplet = null;
//Student aStudent = null;
PrintWriter out = null;
BufferedReader inTest = null;

try
{ }
catch (Exception e)
{ }
}

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("in doGet(...)");

String userOption = null;

userOption = request.getParameterValues("UserOption")[0];

System.out.println("userOption == " + userOption);


ObjectOutputStream outputToApplet;

try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html><head><title> HCL InfoWall"+ userOption +" </title></head></html>");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}


nagasrikanth chalasani

Posts: 3
Nickname: cnsrikanth
Registered: May, 2006

Re: Applet Servlet Communication Posted: May 25, 2006 11:27 PM
Reply to this message Reply
http://localhost:8080/WEB-INF/classes/MyServlet?UserOption=AppletDisplay

Check this in the IE, does this work?

I am shure this will not work.

Flat View: This topic has 1 reply on 1 page
Topic: Learning Struts - Need Help Previous Topic   Next Topic Topic: Applet Servlet Communication

Sponsored Links



Google
  Web Artima.com   

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