📜 ⬆️ ⬇️

Java web platform in 30 minutes

Experienced developers may not read further, since this article is intended more for newbies, 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 own entry-level web platform with minimal effort.

In the article, I will tell you how you can, from almost nothing, even without owning a huge stack of technologies, deploy your own cozy resource on a remote cloud. This could be your personal blog, some interesting service or just a resource 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 moments together or just laughing.
')
For businessmen or beginner businessmen, or owners of any sites, it will be interesting to learn that they don’t necessarily have to invest big money on a “cool” website at the beginning of their business, but rather using open libraries and the only thing to pay for rent a virtual server.

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

You should 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. Check it out here can check java . In 97% of cases this is the case, but if this is not the case, then following the instructions on this site you can easily fix it by installing and configuring the environment.

If, after all, this did not succeed, we will immediately agree with you, first of all you are trying to solve the problem on these sites:


Even if this did not help, then close this article, you need material for lower levels of training.

In general, I will reveal to you a secret in programming, when everything worked for you from 1 time - this means only one thing that something is not working. This is an indisputable fact. A lot of errors, incompatibility of versions, absence of classes in the library, etc. - this is normal.

Your obedient servant himself spent 3 weeks with an error, went through such nooks of the Internet, that for some time he lost touch with reality and spent several months in a mental hospital, but let's not talk about it ... This is a story for a separate article.

So, Java is worth - all is well.

Now we need a tool. Yes, we need an idea. We swing and we put JetBrains from here.

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

So, there is a development environment, Java is.

Let's start


Run the idea.

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

I often see questions from newbies about such interesting things as 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 “solve problem” skill you will be bogged down there for a very long time and it will be very difficult to get back.

Your alma mater at first is 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? But what about the Application server .

So, we need this server, we will use Tomcat . We download it, if you have downloaded what you need, and most likely downloaded some garbage, then check that the archive should be called “apache-tomcat-7.0.67.zip”, unzip it. Do not forget where unpacked, it is useful to you more.

Web resource concept


The essence of this server. What is a server? This is a program code that is “spinning around” in the system and listening to ports. This is a topic for another conversation. But in general, consider 2 options of what the server can do at all, it can give data (GET) - just return a number, page, or God knows what else. But there is also POST - it also returns data, but also receives it from the client before it.

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

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

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

In the welcome window of the idea there is an “import project” button - 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 do not start the server and can not open the site. Why? Because the idea does not know what to do, she is smart, but not so much.

Explaining how to do this would take a couple of pages and do you a disservice. The first difficulties are the first difficulties. This will be your "baptism of fire." Programming - do not bake 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 do them honor - but the fact - is the fact. Forward. Run - go back to reading.

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

Local service testing


You should have 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 SQLite database

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

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

As a result, we need to create our table, 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 ); 

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

Parsing code


Let's start looking at what we have in the code:

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

Our application is simple, so there are 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 servlet behavior in response to requests from the client. The essence is the same - the data came, the data is gone. Nothing complicated. Try to play with the methods.

 protected void doGet(HttpServletRequest request, HttpServletResponse response) 

 protected void doPost(HttpServletRequest request, HttpServletResponse response) 

Moving on to the class that implements JDBC:

 public static void addName(String name) 

 public static ArrayList<String> getAllNames() 

Here, too, everything is simple, the same SQL queries, only wrapped in Java code. A word of advice - 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 such things as serialization it is much easier to live (especially at first).

Everything is finished with the server part. Here you have to sit, read special articles and guides. There is no real practice here.

Client part


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

So, what do we have there with a client?

Again I will give a link to the excellent resource www.codecademy.com . The frontend there is disassembled very well and gives 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 answer. It's simple, gave the data and received from the server. In Russian, he says to the server “Give me the names of everyone who is in your database” or “Register this name in the database” and gives him 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 the 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 you to spend money on the server, so it is unlikely that someone will actually do this, especially at the very beginning of their programming career. But you should understand the general concept now. If someone decides - you know well done, it is likely that you can get out of it.

And according to tradition, several links at once:

nginx
tomcat in real world
nginx config
Ssh

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

The servers themselves can be purchased at amazone. I do not give a link, because advertising, you can search for yourself, it is not difficult. There is a free trial period. BUT! Be extremely careful, your humble servant himself heard stories of thousands of dollars being written off from accounts without the knowledge of the owner, because the system itself is able to re-buy power itself. 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, it is better not to do it at all.

The procedure is as follows:

  1. Rent a cloud
  2. Connect via ssh protocol
  3. Put the right packages and configure the system
  4. Collect a war package and deploy it to the server
  5. Solve a bunch of errors and problems

Immediately I say, from the first time it was possible 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-schnick like 74.125.224.72 and your server will be available on the Internet. It took me a few months to do this, can it be faster? I think so, try it. Then do not forget to buy yourself a beautiful domain and give a link to a friend or friend. If everything worked out - congratulations, you have become one step closer to becoming a professional.

Just in case, I'll leave these links here, all of a sudden, someone stalled at some point and decided to go down to the bottom of the article can help:

maven.apache.org
git-scm.com

Conclusion


So we got to the climax of our discussion here Java and web development on it. Complicated? Yes. Interesting? Yes. Everything, absolutely all services, be it a search engine with billions of indices, whether it is a video service with millions of stream channels - everything is built on the same principle. Give away - pick up 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 on in the long nights. The further you get into the wilds, the more you will understand that you know so little. Go on the road going, go ahead, go ahead.

Forward. It's time to act. It is time for aging Durov and Brin to retire, the time of their glory has passed, it is time to update the history textbooks and Forbs lists. And who knows, maybe this article is read by someone who at one time will write a great platform that will overshadow such giants as Google, Facebook and others. Good luck, thanks for reading to the end.

Source: https://habr.com/ru/post/274587/


All Articles