Solved

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

Posted on 2004-09-13
15
283 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here's how to start interacting with our community through Post.
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

728 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