Java web platform in 30 minutes

Experienced developers may not read further, since this article is intended more for beginners, but still I would be very happy if someone left constructive criticism in my address or pointed out errors.

In this article, I would like to tell novice developers how to create their entry-level web platform with minimal effort.

In this article, I’ll tell you how, practically from scratch, you can, without even owning a huge stack of technologies, deploy your own cozy little resource on a remote cloud. It can be your personal blog, some interesting service or just a resource that you have been dreaming about for a long time. What could be nicer than sending a friend a link to your own resource and discussing some interesting points together or just laughing.

For entrepreneurs or start-up businessmen, or owners of any sites, it will be interesting to know that at the beginning of their business they don’t have to invest a lot of money on a “cool” site, but rather use open libraries and the only thing to pay is to rent a virtual server.

In the article I will show how to make a simple site that has simple functionality. Rather, it is an article dedicated precisely to showing the general concept and helping novice developers to understand how exactly such well-known resources as Google, Facebook, Vkontakte are created.

You must understand that all Internet services are created on the same principle, only the details and the implementation of some unique and specific things differ, but the essence remains the same.

For those who are interested:

Training


Setting what we need: We will

write our service in Java, as the most common language for web services.

Check that the latest version of Java is installed on your computer, now it is version 8. You can verify this here by checking Java . In 97% of cases, this is the case, but if it is not, then following the instructions on this site you can easily fix this by installing and setting up the environment.

If, nevertheless, this did not succeed, we will immediately agree with you so, the first thing you are trying to solve the problem on these sites:


If even this does not help, then close this article, you need material for a lower level of training.

In general, I’ll tell you a secret in programming, when everything worked for you from 1 time - this means only one thing, that something does not work. This is an indisputable fact. A bunch of errors, incompatibility of versions, the lack of classes in the library, and more - this is normal.

Your humble servant himself spent 3 weeks with a mistake, climbing around such nooks and crannies of the Internet that for some time he lost touch with reality and lay for several months in a psychiatric hospital, but we will not talk about it ... This is a story for a separate article.

So, Java is worth it - everything is fine.

Now we need a tool. Yes, we need an idea. Download and install JetBrains from here .

Just keep in mind that you need the Ultimate version. The simple version does not allow the development of web applications. There is a free trial period of 30 days, I think this will not be a problem.

So, the development environment is, Java is.

Let's start


We launch the idea.

At one time I re-read a bunch of articles and other things, and decided that in this article I will exclude the maximum picture and the visual component, it usually only distracts, I may have a different version, a different order of modules and so on.

Often I see questions from beginners about interesting things like Spring and Hibernate (https://spring.io/, hibernate.org ). In 96% of cases, you don’t need it yet, and without good preparation and a good skill “solve problem” you will get stuck there for a very long time and it will be very difficult to get back.

Your alma mater at first - these are 2 technologies:


You must have at least a general idea of ​​these things, otherwise it will be almost impossible to move on.

"Application server". What it is? And here is the Application server .

So, we need this server, we will use Tomcat . Download it, if you downloaded what you need, but most likely downloaded some garbage, then check if the archive should be called "apache-tomcat-7.0.67.zip", unzip it. Do not forget where you unpacked, it will come in handy for you again.

Web resource concept


The bottom line is the server. What is a server? This is a program code that “loops” around the system and listens on ports. This is a topic for another discussion. But in general, we will consider 2 options for what the server can do in general, it can give data (GET) - just return a number, page, or else God knows what. But, there is also POST - it also returns data, but also receives them from the client before that.

If nothing is clear, read here.wikipedia.org/wiki/REST .

The article begins to grow too much. Now I will try to write more briefly.

We go to my repository . We assume that the person does not understand at all what a version control system is, so we go by the amateur, there is a button (“Download ZIP” - download and unzip it).

In the welcome window of the idea there is a button “import project” - click. Select the downloaded and unzipped project.

We press further, further and further, until the project opens.

First difficulties


The project is open, but you will not start the server and you will not be able to open the site. Why? Because, the idea does not know what to do, it is smart, but not so.

An explanation of how to do this would take a couple of pages and would do you a disservice. The first difficulties are the first difficulties. This will be your "baptism of fire." Programming - not oven pies. The links above will help you. Spending 1-2 hours on this is normal. I know good and experienced programmers who have been sitting for several days, but have not managed to start the server correctly. It does not honor them - but fact - is a fact. Forward. Run - go back to reading.

Do not forget to tell the idea that we are using tomcat (how? The links at the top, there is an answer for them).

Local service testing


You should get something like this:



But there is a problem - it does not work! Well, that was to be expected. We have not created a table in the database. In the project we use the SQLite database. Explaining

what it will be with 0 is not easy. Try reading about it on specialized resources.

I can advise a good service: www.codecademy.com (Java, SQL, Git, JavaScript and other super useful things are there), so I beg you.

As a result, we need to create our table, we do it like this:

cd Path/apache-tomcat-7.0.64/bin/
sqlite3 SimpleDatabse

CREATE TABLE NAMES(
   ID INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL,
   NAME TEXT NOT NULL
);

The table is created. You can check how our system works. Add a couple of names and see how they will be returned to you from the base itself.

Code parsing


Let's start to look at what we have in our code:

It just so happened that in Java all configurations are written in xml files, there is no exception - the web.xml file controls which servlet is responsible for what. Take a good look at the code and try changing the link or servlet name.

Our application is simple, so we have only 2 classes (SQLiteClass and MainServlet).

Obviously, I think that the first is responsible for working with the database, and the second is our notorious servlet.

And here are our POST and GET requests mentioned above, these functions are handlers and set the behavior of the servlet in response to requests from the client. The bottom line is that the data has come, the data has gone. Nothing complicated. Try playing around with methods.

protected void doGet(HttpServletRequest request, HttpServletResponse response)

protected void doPost(HttpServletRequest request, HttpServletResponse response) 

We pass to the class that implements JDBC:

public static void addName(String name)

public static ArrayList getAllNames()

Everything here is also simple, the same SQL queries only wrapped in Java code. A quick tip - beware of any add-ons and frameworks. They are good only in large projects, when you have millions of records, and complex transactional operations. But, you only get real control when you write a request manually, without things like serialization, it's much easier to live (especially at first).

Everything is done with the server side. Here you have to sit, read special articles and manuals. There is no real practice here.

Client part


So the time has come to take a look at what is happening with the client in the browser. But nothing supernatural. But, I tell you directly, of course it all depends on the project, but the client side is usually much more difficult to implement than the server side. And it's mostly due to JavaScript. Very quickly, the client code turns into a set of "sheets", patches, hardcode and other fun. JavaScript is harsh and merciless. It’s very hard to write on it. Therefore, we use jQuery . There are a bunch of other frameworks and other creations, but we will not touch on them here. There is a saying that name any word and this will be the name of the JS framework. Mocha framework is known in narrow circles , I can’t imagine what thoughts visited a person when he came up with his name, well, that’s his right, of course.

So what have we got there with the client?

Again, I will give a link to a great resource www.codecademy.com . The frontend there is very well disassembled and provides the necessary base for beginners.

Here we consider only the function

function serverConnectFunc(serverUrl, jsonData) {
    $.ajax({
        url: serverUrl + "/",
        type: 'POST',
        data: jsonData,
        dataType: 'json',
        async: true,
        success: function (event) {
            //do somehting
        },
        error: function (xhr, status, error) {
            alert(error);
        }
    });
}

What she does? That's right, it sends the same POST request and parses the response. It's simple, I gave the data and received it from the server. In Russian, he tells the server, “Give me the names of everyone in your database” or “Put that name in the database” and give it a name.

That's the whole client part.

We send a resource to the real world


As you can see, our service is spinning on a local host. In other words, on our own computer. It's time to fix it.

Here I will describe only the general principle of this action. This one way or another will require money from you on the server, so it is unlikely that anyone will really do this, especially at the very beginning of their programming path. But you must understand the general concept now. If anyone decides - you know well done, it is likely that you can get a good sense.

And by tradition, several links at once:

nginx
tomcat in real world
nginx config
SSH

We read what is written there, form in our head a general concept of how the programmer and the remote server interact.

The servers themselves can be purchased on amazone. I don’t give a link, because advertising, you can search yourself, it’s not difficult. There is a free trial period. BUT! Be extremely careful, your humble servant himself heard the stories, how thousands of dollars were debited from the accounts without the knowledge of the owner, because the system itself can buy itself the power. Do not get caught, I myself have already paid several times for incomprehensible services, everything is in English. If you are not sure what you are doing, do not do it better.

The procedure is as follows:

  1. Rent a cloud
  2. Connect via ssh
  3. Install the necessary packages and configure the system
  4. Gather a war package and deploy it to the server
  5. Solve a bunch of mistakes and problems.

I must say right away, the first time it was possible to units. There is always something that does not want to be installed, run or work. This is normal. Sooner or later you will have a beautiful ip-shnik like 74.125.224.72 and your server will become available on the Internet. It took me several months to do this, can it be faster? I think so, try it. Then do not forget to buy a beautiful domain for yourself and give the link to a friend or acquaintance. If everything worked out - congratulations, you are one step closer to becoming a professional.

Just in case, I will leave these links here, suddenly, someone stalled at some point and decided to go down to the very bottom of the article, it might help:

maven.apache.org
git-scm.com

Conclusion


So we got to the climax of our discussion of Java and web development on it here. Difficult? Yes. Interesting? Yes. Everything, absolutely all services, whether it’s a search engine with billions of indexes, whether it’s a video service with millions of stream channels - everything is built on the same principle. Give - take the data. Understanding this concept, you can write any system, service or platform.

I will not dissemble and deceive you dear readers. It is unlikely that in 2 or 3 weeks you will become super professionals and will be free to write code. This can only be learned later and long nights. The further you climb into the wilds, the more you will realize that you know so little. The road will be overpowered by the going forward, go ahead.

Forward. It's time to act. Aging Durov and Bryn should retire, the time of their glory has passed, it's time to update the history books and Forbs lists. And who knows, maybe this article is read by someone who at one time will write an excellent platform that will eclipse such giants as Google, Facebook and others. Good luck, thanks for reading to the end.

Also popular now: