Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-09-13
15
Medium Priority
?
287 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
13 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
Loops Section Overview
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

782 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