WebService - Swing_Servlet Communication

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SwingServlet {

  public static void main(String[] args) throws Exception {
 String username="raju";
 String password="raju123";
            URL url=new URL("http://localhost:8080/ServletDemo/LoginServlet?name="+username+"&&password="+password);
    String stringToReverse = URLEncoder.encode("http://localhost:8080/ServletDemo/LoginServlet", "UTF-8");

        System.out.println(stringToReverse);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);

        OutputStreamWriter out = new OutputStreamWriter(
                                         connection.getOutputStream());
        out.write("name=" + stringToReverse);
        out.close();

        BufferedReader in = new BufferedReader(
                                    new InputStreamReader(
                                    connection.getInputStream()));
        String decodedString;
        while ((decodedString = in.readLine()) != null) {
            System.out.println("**********"+decodedString);
        }
        in.close();
    }
   
}


============================================
Taking Response Back from Servlet.


  public static void main(String[] args) throws MalformedURLException, IOException {
       URL oracle = new URL("http://localhost:8080/ServletDemo/LoginServlet");
        URLConnection yc = oracle.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                                    yc.getInputStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
        in.close();
    }
}


===========================================


Web_Service

create Servlet and paste code in body tag ;

response.setContentType("text/html;charset=UTF-8");
        String user = request.getParameter("name");
        String pass = request.getParameter("password");
      
        System.out.println(user);
        System.out.println(pass);
        OutputStream os = response.getOutputStream();
        String str = "success";
        os.write(str.getBytes());

Comments

Popular posts from this blog

Filter In Javafx

Kotlin with StandAlone Complier