Trying to understand more about web servers and client.
Hello.
Basically, I would like to understand where I should start regarding my problem, and would appreciate some simple examples.
My application is like that, I have a log in, where the user would key in their userID and their password. For authentication, it would connect to a simple HTTP web server to see if their userID and password match/exist.
After that, my application is able to retrieve a person's particular from the HTTP web server as well.
And one more function is that my user is able to add a person's particular onto the HTTP web server.
I would like to know where I should start reading and if you would be kind enough to provide me one simple example, I'm sure I'll be able to do the rest myself.
Re: Trying to understand more about web servers and client.
You can google for things like Java servlet or JSP tutorials. That will give you tons of results.
Re: Trying to understand more about web servers and client.
I'm actually not trying to implement it in servlet form, if that is possible.
Re: Trying to understand more about web servers and client.
When you are using Java the basic thing that you need to create a simple web application is at least a servlet or JSP. If not in this form what then you are trying to implement?
Re: Trying to understand more about web servers and client.
Quote:
Originally Posted by
wsaryada
When you are using Java the basic thing that you need to create a simple web application is at least a servlet or JSP. If not in this form what then you are trying to implement?
Oh. Sorry for the confusion.
I'm actually not developing a web application, but more of a java Swing Application that connects to to a HTTP server that holds a different "webpages" of information.
It's something like that.
Code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class LoginFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private String NRIC;
private String password;
private static String host = "http://localhost:9999/testing.html";
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look
// and feel.
}
try {
URL url = new URL(host);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection(); // connection.connect();
connection.setAllowUserInteraction(true);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setUseCaches(false); //
connection.setRequestProperty("Content-Length",
"application/x-www-form-urlencoded");
URLConnection hpCon = url.openConnection();
// Get the response
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String str;
if ((reader.readLine().length() > 0)) {
while ((str = reader.readLine()) != null) {
// Process str...
}
connection.disconnect();
} else {
System.out.println("NOT CONNECTED");
}
} catch (Exception e) {
e.printStackTrace();
}
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrame frame = new LoginFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(123, 94, 188, 125);
contentPane.add(panel);
panel.setLayout(null);
textField = new JTextField();
textField.setBounds(80, 0, 108, 31);
panel.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(80, 42, 108, 31);
panel.add(passwordField);
JButton btnNewButton = new JButton("Login");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
NRIC = textField.getText();
password = passwordField.getText();
}
});
btnNewButton.setBounds(48, 102, 89, 23);
panel.add(btnNewButton);
JLabel lblNric = new JLabel("NRIC:");
lblNric.setBounds(0, 8, 70, 14);
panel.add(lblNric);
lblNric.setFont(new Font("Calibri", Font.PLAIN, 14));
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(0, 50, 70, 14);
panel.add(lblPassword);
lblPassword.setFont(new Font("Calibri", Font.PLAIN, 14));
}
}
basically, stuffs like the userID (NRIC) and passwords are stored inside this "webpage" and I would like to retrieve them and store it in to my java, and then check if it exist from my Swing application itself. I'm not needed to go to any webpages on a browser at any point of time. :)
You could say that this webpage is similar to a notepad that one would retrieve information from if stored inside the same directory as my codes.
Re: Trying to understand more about web servers and client.
So your question is how to read / download the testing.html so that you swing application can use it content. You can retrieve the file like this:
Code:
URL url = new URL("http://localhost:9999/testing.html");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
Re: Trying to understand more about web servers and client.
Ah yes, something like that, thank you.
May I ask, is it possible to do a check prior to the reader to see if the server is up? And also, how should I go about if I want to add something to this information? Like say, if there is no such user, I'll add the user in. :)
Re: Trying to understand more about web servers and client.
To detect if the server is up just wait until your server return a data or you'll get a connection error.
If you want to modify the file content on the server you need to use servlet. There's no way to modify the file on the server without a help from a servlet. Why don't you just store your user information in a database server instead?
Re: Trying to understand more about web servers and client.
Quote:
Originally Posted by
wsaryada
To detect if the server is up just wait until your server return a data or you'll get a connection error.
If you want to modify the file content on the server you need to use servlet. There's no way to modify the file on the server without a help from a servlet. Why don't you just store your user information in a database server instead?
Ahh. Alright. Thank you.
Initially, I was doing it on NetBeans IDE, using it's built in database. However, my lecturer told me that that was not necessary as my project only concentrates on the front end, and I only need dummy data for testing purposes, so I'm not required to build a database.
My lecturer did talk about using JSON, but I'm not too sure how to use it in Java. Maybe I'll try it in the next few days. :)
Just a quick question about JSON, do I still do the same connection as above to establish a connection to the server? Since the file is no longer .html
Re: Trying to understand more about web servers and client.
If what you mean is just retrieving a text file in JSON format then it will be the same. But you could also implement a servlet to return the JSON string. And set the correct content type of the response object as application/json.
Re: Trying to understand more about web servers and client.
Quote:
Originally Posted by
wsaryada
If what you mean is just retrieving a text file in JSON format then it will be the same. But you could also implement a servlet to return the JSON string. And set the correct content type of the response object as application/json.
I will read up more on that.
I tried a little and realized that I could just add some text to the JSON file.
Code:
OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream("C:\\test1.json", true), "UTF-8");
writer.write(obj.toJSONString());
writer.flush();
writer.close();
something like that. Maybe I could do the same to the testing.html as well?
Re: Trying to understand more about web servers and client.
If the file is in your local machine you can do that. But I assume that your html file is in another machine. The way you can update it is to use servlet. This servlet can have the code you post above to update the content.
Re: Trying to understand more about web servers and client.
Quote:
Originally Posted by
wsaryada
If the file is in your local machine you can do that. But I assume that your html file is in another machine. The way you can update it is to use servlet. This servlet can have the code you post above to update the content.
Ah. My files are in my local machine as of now. If it is on another machine, I will have to use a servlet no matter what? Even using .json file?
I've did servlet before, but that was almost 6 years ago and I kind of forgotten how to use it exactly, and I don't think I'll have enough time to learn it again.
Anyway, thank you so much wsaryada. You've been of great help.
Re: Trying to understand more about web servers and client.
Quote:
Originally Posted by
rhexis
Ah. My files are in my local machine as of now. If it is on another machine, I will have to use a servlet no matter what? Even using .json file?
To be clear that in previous code snippet that you've posted you read from a location in your own machine. Which you can locate locally using the file directory. But when you have a file that are stored in other server and can be access only through a web server that means you need another way to access it. One way in to use an http servelt to read and modify the file.
In your case instead of storing the file on the other server why don't you just use a local file? If this file is need to be secured you might need to hide the information by encrypting its content.
Re: Trying to understand more about web servers and client.
Hello again. Thank you for your reply.
Ah, I understand what you mean now.
I have no choice but to use a web server because I have to "simulate" that my application is connecting to a web server to retrieve the information, as seen from my first code.
How about if I do not wish to retrieve data from a webserver, but instead would like to save the file into my local machine, and then upload it to the webserver? Would that be possible?
I do not really need to implement anything at the server side to retrieve these uploaded file. I just want my file to go from say, C:\project to http://localhost:9999/upload folder.
If I do need to implement a servlet here, I guess I'll have to skip this entire upload thing for this project.
Re: Trying to understand more about web servers and client.
To upload the file if you don't want to use a servlet then using the FTP protocol to upload the file is your another choice.
Re: Trying to understand more about web servers and client.
Quote:
Originally Posted by
wsaryada
To upload the file if you don't want to use a servlet then using the FTP protocol to upload the file is your another choice.
I just managed to do it. Thank you so much for your help! :)-: