0 Replies - 604 Views - Last Post: 17 June 2013 - 09:48 PM

#1 Emon_MQ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 06-August 09

Applet won't send data to Servlet

Posted 17 June 2013 - 09:48 PM

Well, I am trying to learn how to communicate to servlet from applet.I have build an applet containing a text field and a button.When I press the button The text field value would be sent to a servlet where it will be displayed.But when I press the button nothing happens. I am giving the layout of my files and code fragments where I suspect I have made mistakes.

Layout of files: I got a web dynamic project "Test" there I have a package"pkTwo" which contrains the servlet "MyServlet.java" For the Applet I used a normal java project and made a jar file and kept it in the "webContent"
along with my jsp which calls the applet using this jar.

heres the method(From my Applet) used for my buttons listener to send the string of text field

 public void sendData(String data) throws IOException{  
              
            URL url = new URL(getCodeBase(), "http://localhost:8080/Test/MyServlet");  //   here /servletlink is the link I gave for myservlet //from web.xml or should I use given name in web.xml?  
            URLConnection con = url.openConnection();  
              
            con.setDoOutput(true);  
            con.setUseCaches(false);  
            con.setRequestProperty("Content-Type","application/octet-stream"); //how does the content type effects?should I use same type if I send serialized object?  
              
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());  
            out.write("MyString=" + data);  
            out.flush();  
            out.close();  
        }  




here is the doGet() of my servlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
              
            String text=request.getParameter("MyString");  
            PrintWriter out=response.getWriter();  
            out.println(text);  
        }  




and heres is the web.xml code
    <web-app>   
      <servlet>  
      <servlet-name>mypage</servlet-name>  
      <servlet-class>pkTwo.MyServlet</servlet-class>  
      </servlet>  
    <servlet-mapping>  
    <servlet-name>mypage</servlet-name>  
    <url-pattern>/servletlink</url-pattern>  
    </servlet-mapping>  
    </web-app>  



What are the mistakes I have made?please let me know

I am using eclipse with tomcat 7 I have attatch the project's zip file(Since it wont me allow the war file) and the zip for the applet incase you want to look at the code
Thanks in advance

Attached File(s)


This post has been edited by Emon_MQ: 17 June 2013 - 09:53 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1