Java-for-beginners - writing our first code

E-mail Print PDF
Article Index
Java-for-beginners
first code
code 1 explanation
Executing the Program
Fetch Data from NCBI
NCBI fetch reference
All Pages

So what is the logic of the program? We need to find out the steps in which we can send the data from the user, in the format requested by the String db. Thus, we need to understand how Java communicates to an external URL for sending and receiving input. Next, you understand the flow of the logic and then replace it with Java statements to actually execute the same.

Thus, we have the following steps of logic for sending and receiving data from EMBL.

1) Get the input from the user

2) Create the URL for sending the data to the db

3) Get the proper authentication for sending data via the internet to the db

4) Send the input data to the String db API via the internet

5) Read the response that is received.

6) Print the output into the format that the user understands.

7) End the execution

Writing the logic helps in implementing the java program faster.

So now, we write the steps for implementing the logic mentioned above.

The following is the Java program that will actually send the request and get the response from the String EMBL.

1) Get the input from the user

This will be a simple html form in which we ask the user to input the parameters like search terms and required score. This will be just like the String EMBL system form that we see on the homepage of the latter. Please refer to the file ImportFromEMBLDb.jsp (ProteomDb project folder) for further details.

2) To Step -7) are implemented in the program below.

ImportFromEMBLPI.jsp

The comments are mentioned in ‘//’ to explain the logic that has been used here. They are not a part of the actual code. To get the working code, goto the ProteomDb project folder and find the file by this name.

Let us walk make the code step by step.

1) Open any HTML Editor or Java IDE (for beginners, simple Notepad or the former is preferred. Later on, you can use Eclipse or Netbeans IDE)

2) Save the file as ImportFromEMBLPI.jsp (you can give any other name as you want). Next, we add the java libraries that are to be needed for our code. You need to read the API to specifically understand which APIs will be needed for your proposed program.

3) Next, we mention the try-catch block and define the session. The session object is needed to hold variables across pages and limit the duration of any Java activity (check Java tutorial and API for further details).

4) Define the URL string by taking user input from the input form.

5) Define the System Properties. These are values that are stored in a file that contains information to log into the internet via your home network.

6) Define the URL object to hold the URL string. Next create a file name using the Random class object integer value.

7) Now define two handles to write to:-

BufferedReader in – this is to print the output from the NCBI server and get it to the Java Program handle

8) Declare the variables to hold the values.

9) Write the while loop to actually print the content to the browser. You could store it in an array and display it in a tabular form.

First we get the while loop which reads the output from NCBI line by line. Then we use string handling functions to print the different parts of the array. The EMBL text output is in text format with tab delimiters between them. Using string functions, we can parse the output and get the actual column values.

Write the while loop to actually print the content to the browser. You could store it in an array and display it in a tabular form.

9) Now we try to keep a counter for managing the number of records. Again, we close the loops and print the values. Lastly, we store the URL as a session attribute. You could redirect the page to another welcome page if wanted using the response.sendRedirect command.

 



You are here: Tutorials Languages Java