Servlet & Jsp dynamic website development_2. Implementation of Servlet & Jsp HelloWorld

One, create a project

Before creating the project, we first get tomcat to our eclipse. Of course, if you use idea, you don’t need to do this operation, just skip it.

We click on the window to find the show view, then we select other, find Servers, and then select, click open

After that, we can see that there is a server in the shortcut box below

After that, we click on the No servers are available... this, add the tomcat that we downloaded last time

Finish directly

At that time, we can start our tomcat through here, and the province has to go to the directory to start

 

With this, we go to create a Web project, here, we need to create a Java project that can be deployed in tomcat

Here we choose Dynamic Web Project (Dynamic Web Project), and then next 

Here we give this project a name, and then we choose the server at runtime, and another is a web module version, this we choose 2.5! ! ! (Because a web.xml file is missing after 2.5, to avoid manual creation, we use this)

Then finish directly

This is a directory structure just created for our entire web project

src puts our Java source code

There is only one file in meta-inf, this is the sticky note information, you can Baidu for details

There is a lib folder in web-inf, in which we will put some jar packages in the future. For example, we need to connect to the database in the future. We need to put the jar packages in the lib, and then add them to the build path, then we can use Some classes or class methods in the jar package

Mainly let's look at web.xml now, this is a key point

As you can see, there are very few tags in this initialized web.xml, just a few. We will configure many things here in the future. We will use them frequently. I hope everyone can pay attention to it.

Second, the realization of HelloWorld

Next, let's implement HelloWorld, we create an html under the WebContent folder, the name is index.html

Then we write some content in our body

Then we add this item to our tomcat, right-click the tomcat we just added, and select Add and Remove

Then select our project in Available, click Add, we can see our project in Confired, and then click Finish

However, this is not enough, we still need to start our tomcat

Similarly, we right-click tomcat and select Start

In the case of no error, we see this Server startup in xxx ms, it means the startup is complete, and then we proceed to visit our project

http://localhost:8080/HelloWorld (Ip + port + project name)

As you can see, in this way we have successfully built a most basic dynamic website.

Three, Jsp

We just created a html, we will now create a Jsp

If we create it, we still create it in the same directory as the html

Then we look at our Jsp file

We can find that there is an additional <%@ page... thing on the top. Let’s ignore it. Let’s talk about it later and look down. In fact, it’s not difficult to find that the following are our html codes. Next , We also write some content here

Then the question comes, let’s save

You can see such an error. Here, we need to modify the encoding format of the page. The default is ISO-8859-1 . We change them to UTF-8.

There are three places in total, let’s save it again

Is ok

Then we restart the server

Here we can choose Stop, and then go to Start, or directly choose Restart

After restarting, let’s visit the path just now

As you can see, it is still OK, and then we will visit the jsp page (here we need to specify the path to the specific page)

We click

Ok, no problem

Four, packaging

We packaged our HelloWorld project just now

Packaging is to facilitate delivery and deployment. We can't put an eclipse on the server, and then start and access, we directly generate a file, then as long as there is an environment, we can continue to run directly.

For packaging, we can use a simple export. Before that, we stop the tomcat server

Right-click our project and select export

Find war

We need to package a file in this format, select next

Then we choose this Browse, this is where the packaged files are placed, I choose to go to the desktop here

Click Finish, then switch to our desktop, you can see a file with a .war suffix

Then we put it in our local tomcat webapps

Let's start our server

After the startup is complete, let's take a look at our webapps directory

We can see that there is an additional HelloWorld directory, let's click in and take a look

As you can see, it is actually very similar to the project in our development tool. Let’s open WEB-INF and take a look.

There is one more classes folder in this one. We will put our Java code in this one. But because we haven’t written it yet, this one is empty now. The others are the same as our project. Everyone is left with their own take a look

Next, let's open the work directory again

And then keep going in

As you can see, there is also a HelloWorld directory, but now it is an empty directory, let’s request our project now

It’s no problem to request the html page first. At this time, the directory in the work is still empty.

Let's request another Jsp page

It's also OK, and there are things in our work folder

Click inside

As you can see, there are two files, this is what we said in the previous lesson, Jsp is actually Java, and then we look at the .Java file

Below we can see this thing, is it very familiar, this is our page, and finally it will be converted into Java code, and then output

 

Let's stop here for this section, everyone digest it, we will continue to talk about it later.

If you don’t understand, you can chat privately on my QQ: 2100363119, and you are also welcome to visit my website: https://www.lemon1234.com\ Thank you all

 

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/110294561