Solved

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

Posted on 2004-09-13
15
281 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
object methods vs static methods 2 100
jersey maven project error 7 142
Handle form fields in spring mvc controller 1 66
Market Place not working in Eclipse Luna 2 118
Some of the SEO trends we might expect in 2017.
There's a lot of hype surrounding blockchain technology. Here's how it works and some of the novel ways it' s now being used - including for data protection.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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