• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Communication between applet and servlet Java Code

April 13, 2011 by ProjectsGeek Leave a Comment

                        Communication between applet and servlet Java Code

Today i will discuss about the communication between applet and servlet’s,  the most common problem faced by beginner’s during Java  development . So taking the applet as front end , our servlet at middle and database at back end most probably MYSQL.

Beginning at the applet end put the given below code in applet class outside paint function (must) . Change the  URL urlServlet = new URL(getCodeBase(), “points”) ,if your  servlet location is different than default location. You can send any object through this connection .just create the  object and send it through  oos.writeObject(your object) .

Communication between applet and servlet Java Code –Applet Code

import java.applet.Applet;  
 import java.awt.*;  
 import java.awt.event.*;  
 import java.io.*;  
 import java.net.*;  
 public class tank extends Applet {  
   private URLConnection ServletConnection()  
     throws MalformedURLException, IOException {  
     URL urlServlet = new URL(getCodeBase(), "points");  
     URLConnection con = urlServlet.openConnection();  
     con.setDoInput(true);  
     con.setDoOutput(true);  
     con.setUseCaches(false);  
     con.setRequestProperty("Content-Type", "application/x-java-serialized-object");  
         return con;  
   }  
   private void SendData() {  
     try {  
       String input="hi hello !!";  
      // sending string from applet to servlet  
       URLConnection con = ServletConnection();  
       OutputStream outstream = con.getOutputStream();  
       ObjectOutputStream oos = new ObjectOutputStream(outstream);  
       oos.writeObject(input);  
       oos.flush();  
       oos.close();  
       // receive string from servlet  
       InputStream instr = con.getInputStream();  
       ObjectInputStream inputFromServlet = new ObjectInputStream(instr);  
       String data = (String) inputFromServlet.readObject();  
       inputFromServlet.close();  
       instr.close();  
       outputField.setText(result);  
     } catch (Exception ex) {  
       ex.printStackTrace();  
          }  
   }  
 }

Communication between applet and servlet Java Code-Servlet Code

Now moving to our servlet whose name is points in root folder of our workspace(Server folder apache). The code given below simply accept the string send by the applet and send it back to applet.

 import java.io.*;  
 import javax.servlet.ServletException;  
 import javax.servlet.http.*;  
 public class points extends HttpServlet {  
   public void doPost  
   (  
     HttpServletRequest request,  
     HttpServletResponse response)  
     throws ServletException, IOException  
      {  
       try {  
           response.setContentType("application/x-java-serialized-object");  
           InputStream in = request.getInputStream();  
           ObjectInputStream inputFromApplet = new ObjectInputStream(in);  
           String abc = (String) inputFromApplet.readObject();     // receiving string from applet  
           OutputStream outstr = response.getOutputStream();  
           ObjectOutputStream oos = new ObjectOutputStream(outstr);  
           oos.writeObject(abc);                                  // sending string back to applet  
           oos.flush();  
           oos.close();  
         } catch (Exception e) {  
       e.printStackTrace();  
     }  
   }  
 }

 

 

Other Projects to Try:

  1. To Implement Web Crawler in Java BE(IT) CLP-II Pratical
  2. Design an applet that displays the string “Hello College Name” moving from left to right. When it reaches to right, it scrolls back to left.
  3. GET vs POST in PHP | easy example
  4. Java Applet Tutorial for Beginners
  5. 100+ Free Java mini projects with Source Code

Filed Under: Java Tutorials

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy