Solved

Can I use java swings on the client side to  connect it to the tomcat server?

Posted on 2004-09-13
15
278 Views
Last Modified: 2010-04-01
Hi ,

I am creating a userinterface using javaswings,I hava a doubt while  using swings can we connect to the server or not
 
0
Comment
Question by:sdushyanth
  • 3
  • 3
  • 3
  • +2
15 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 12046018
no
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 12046023
unless in applet of course
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 12046483
or if using webstart
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12047804
or a standalone application calling a servlet
0
 

Author Comment

by:sdushyanth
ID: 12048211
Hai TimYates,

Can u please tell me how to call servlet from standalone application?

dushyanth.
0
 
LVL 5

Expert Comment

by:Daij-Djan
ID: 12048663
which would be the most complicated solution...
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 35

Accepted Solution

by:
TimYates earned 250 total points
ID: 12052406
   URL url = new URL("http://localhost:8080/servlet/MyServlet");
    HttpURLConnection con = (HttpURLConnection)url.openConnection();            
    con.setStuff(...............................check out api
    con.setDoOutput(true);
    con.setDoInput(true);
    con.connect();

    // write stuff
    PrintWriter pw = new PrintWriter(con.getOutputStream());
    pw.println(depart);
    pw.flush();
    pw.close();

    // read reply
    BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String res = br.readLine();

Of course, if the servlet relies on cookies, this gets harder and you may want to consider using Apache's HttpClient code (which handles that for you)
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12055103
Yes. Like Time said you can connect it via servlet/jsp.

For example, if you want to do calculator on server side by sending two operands and one operator to server you can send it to server by using URL

YourApp.java
=========
URL url = new URL("http://localhost:8080/calculate.jsp?operand1=1&operator=plus&operand2=2");
InputStream is = url.openStream();
byte[] b = new byte[100]; // the size of buffer depend on the length of jsp output
is.read(b);
System.out.println("Result is " + new String(b));

calculate.jsp
========
<%
  String operator = request.getParameter("operator");
  int operand1 = Integer.parseInt(request.getParameter("operand1"));
  int operand2 = Integer.parseInt(request.getParameter("operand2"));

  if ( operator.equals("plus") ) {
    out.println(operand1+operand2);
  }
  else if (operator.equals("minus") ) {
    out.println(operand1-operand2);
  }
  else if (operator.equals("multiply") ) {
    out.println(operand1*operand2);
  }
  else if (operator.equals("divide") ) {
    out.println(operand1/operand2);
  }
%>

Umm.. This is difference from Tim ???. Although it may talk to the same point, but I just want to show you more simple example. :)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12055150
> Umm.. This is difference from Tim ???

You are doing HTTP GET's I am doing POSTs ;-)
0
 

Author Comment

by:sdushyanth
ID: 12075511
I am writing a simple standalone application using javaswing ,
i,e i am using frame  in the frame there are two text boxes
one for user name and one for password..
and submit.
can u please send me a code how to connect to it to servlet..
how servlet will take request.getParameters...

dushyanth
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12075978
You can apply from my code above.
calculate.jsp may change to

checkPassword.jsp
============
<%
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  // do authenticate, you must code by yourselft depending on how to authenticate.
  // the following code is just sample
  boolean pass = Authenticator.checkPassword(username, password);
  if ( pass ) {
    out.println("valid");
  }
  else {
    out.println("invalid");
  }
%>

and in your swing class

YourApp.java
=========
//At the listening method of clicking submit button.
String username = txtUser.getText();
String password = txtPass.getText();
URL url = new URL("http://localhost:8080/checkPassword.jsp?username="+username+"&password="+password);
InputStream is = url.openStream();
byte[] b = new byte[100]; // the size of buffer depend on the length of jsp output
is.read(b);
System.out.println("The user login is " + new String(b));

If the user can login, it will print "The user login is valid".
If not, it will print "The user login is invalid".
0
 

Author Comment

by:sdushyanth
ID: 12078489
Hi sampol,

Thanks for sending me the solution,
actually after successful user authentication,i have to launch the homepage in webbrowser.can u please give solution for this,
thanks
dushyanth
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 12081746
Do you want to open web browser by java code?

If so, please look in the api of java.lang.Runtime there are methods you can use to execute command in a separated process.

Here is an example code.

java.lang.Runtime.getRuntime().exec("iexplore http://www.google.co.th");
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
A Short Story about the Best File Recovery Software – Acronis True Image 2017
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now