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.


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

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.write("Eligible for Voting....");
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 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() {
void getData()

// 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());

}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();


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

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

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

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


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

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

1 comment:

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