Solved

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

Posted on 2004-09-13
15
279 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When we talk about DevOps toolchains, I sometimes wonder how many people really get what we’re talking about. I don’t know if it’s just semantics or tone or something else, but sometimes I think it just sounds like buzzword sausage. So it’s always …
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

16 Experts available now in Live!

Get 1:1 Help Now