Thursday, July 25, 2013

Calling a JSP Page from Swing Using Http Library - [ sending & reciving data ]



Today we are going to see , how to call a jsp page which is running in the local server or remote server from swing application and how to sent values to the jsp page and retrieve the same and display to the user.

Requirements:

1. HttpClient Library - Download this library from the apache  site :

http://hc.apache.org/downloads.cgi

2. TomCat Server 

Any application server or web server is required , I have mentioned Tomcat here but you can use of your own.This is required to deploy a web application which contains some JSP Pages.

Once you download the http client library in the form of zip file ,extract the file it will have a folder called lib copy all the jars from lib folder and add it to your class path or if you are using Net beans ,eclipse or any other ID then paste it in your libraries folder.

Now , We need  to create a Web Application which have some jsp so that we can deploy the application and call this JSP from our swing application.I am using Netbeans ID to create a web application .

File -> New -> New Project - > Project Name : MyApp

Now i am going to  creating a JSP called  "http.jsp" in MyApp .   

http.jsp :

<%

int h=Integer.parseInt(request.getParameter("age"));
out.clearBuffer();

if(h>=18)
out.write("Eligible for Voting....");
else
out.write("Not eligible for Voting!!!!");

%>


In this JSP i have not followed any coding standard and enterprise approach for writing a JSP as JSP Document by moving the scriptlet in custom tags .

So this JSP will accept a Parameter called "age" and convert the age to an Integer and Prints the respective output based upon the conditions.Now deploy this application in to Tomcat and run this JSP by
passing age=some value in the URL otherwise it will give an Exception.

http://localhost:8083/ MyApp /http.jsp?age=17

Here i am referring my local host and my port you should refer your own port , other wise page not found error will occur.

Swing Code :

Now we are going to create a Simple Swing Application which will call this JSP and display the result .


package http;

import java.io.DataInputStream;
import javax.swing.JOptionPane;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class Connect extends javax.swing.JFrame {


public Connect() {
initComponents();
setSize(500,500);
}
void getData()
{
try
{

// Create a HttpClient
HttpClient client=new DefaultHttpClient();

// Set the value of TextField as a Parameter in the Request and made a call.
HttpGet get=new HttpGet("http://localhost:8083/scw/http.jsp?age="+jTextField1.getText());

// Get the Response
HttpResponse response=client.execute(get);

// Extract the Response
DataInputStream dataInputStream=new DataInputStream(response.getEntity().getContent());
jTextField2.setText("");
jTextField2.setText(dataInputStream.readLine());
dataInputStream.close();


}catch(Exception e){
JOptionPane.showMessageDialog(this, e.getMessage());}

}
 
private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);

jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("Calling JSP From Swing");
getContentPane().add(jLabel1);
jLabel1.setBounds(100, 30, 140, 20);

jLabel2.setText("Age :");
getContentPane().add(jLabel2);
jLabel2.setBounds(46, 110, 40, 14);
getContentPane().add(jTextField1);
jTextField1.setBounds(100, 110, 140, 20);

jLabel3.setText("Result :");
getContentPane().add(jLabel3);
jLabel3.setBounds(44, 160, 50, 14);
getContentPane().add(jTextField2);
jTextField2.setBounds(100, 160, 170, 40);

jButton1.setText("Connect >>");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(260, 110, 110, 23);

pack();
  }

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
getData();
}

public static void main(String args[]) {
new Connect().setVisible(true);
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}

Result :
























<< Prev                     Next>>                                               Index >>

-------------------------------------------------------------------------------------------- 




Technology Blogs
blogs

14 comments:

  1. sir
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;



    above all are show same error like package does not exist
    please help me sir please.............

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. hello,you have to download the httpClientLibrary from http://hc.apache.org/downloads.cgi.Once you download the http client library in the form of zip file ,extract the file it will have a folder called lib copy all the jars from lib folder and add it to your class path or if you are using Net beans ,eclipse or any other ID then paste it in your libraries folder.For seetting class path in netbeans right click-->properties-->libraries-->add jar/folder-->select path of ur project in which u copied jar. Then,there will be no error.

    ReplyDelete
  4. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    best java training institute in chennai |
    java training institutes in chennai

    ReplyDelete
  5. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Devops Training courses
    python Training in chennai
    Devops Training in Bangalore

    ReplyDelete
  6. This is quite educational arrange. It has famous breeding about what I rarity to vouch.
    Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved.
    This arrange is synchronous s informative impolite festivity to pity. I appreciated what you ok extremely here.


    Selenium interview questions and answers
    Selenium Online training
    Selenium training in Pune
    selenium training in USA
    selenium training in chennai

    ReplyDelete
  7. The main motive of the Hadoop consulting services is to spread the knowledge so that they can give more big data engineers to the world.

    ReplyDelete
  8. Nice blog, very informative content.Thanks for sharing, waiting for the next update…
    Features of Java Programming Language
    Features of Java

    ReplyDelete
  9. very intersting to read your blog and it makes the viewers to visit your blog and keep on updating.
    Tally Training in Bangalore
    Tally Course in Hyderabad

    ReplyDelete