JavaWeb study notes 5--JSP introduction and introduction (including Eclipse for Java EE and Tomcat configuration)

 ​【 Declaration】 

Welcome to reprint, but please keep the original source of the article →_→ 

Life One: http://www.cnblogs.com/smyhvae/

Article source: http://www.cnblogs.com/smyhvae/p/4046862.html

 

【Foreword】

JSP itself is knowledge in JavaWeb, but when learning the Android network, it must involve interaction with the server, so it is necessary to learn about JSP and other JavaWeb content, at least to understand that when the program accesses the server, Principles of the whole process.

In fact, before learning Android, knowledge of Java and JavaWeb must be learned first. I officially started research and study in the direction of Android in July 2014. Before that, I had no contact with any knowledge related to computer software (the only relevant thing is that I have taken a C language course for undergraduates, but I have forgotten all about it now).

Let's see the picture below:

The two pictures look right together. The reference is from Li Gang's "Crazy Java Learning Roadmap". The mobile phone pixel is slag, and the picture is not very clear. Let's take a look. To put it bluntly, you must have a certain Java foundation (Java SE, JavaWeb, database, etc.) before learning Android. If you want to study solidly and have a good job position, you need to have all the following basic knowledge: basic computer knowledge (operating system, computer network, data structure, database, design pattern, etc.), C/C++, Java, Android, Linux .

Therefore, considering the time factor, a beginner like me can only learn other knowledge while learning Android. Where I learn, my blog will be written , and I hope to share with other beginners and witness together!

 

【text】

1. Introduction to JSP

JSP: Java Server Pages. Adding Java program fragments (Scriptlet) and JSP tags (tag) to traditional HTML files (*htm, *.html) constitutes a JSP web page.

 

2. Learning focus:

  • JSP syntax (scripts, directives, actions)
  • JSP built-in objects
  • Create dynamic content
  • User session tracking

 

3. Use tomcat software to build a server locally:

Tomcat is an important sub-project in the Jakarta project organized by Apache. It is a container (engine) recommended by Sun to run Servlet and JSP, and its source code is completely open.

With this server, it is equivalent to having a website on the local computer, and then we can access this website through the browser.

Tomcat Soft Armor is an open source project under Apache. Software download link: http://tomcat.apache.org/

After downloading, unzip the archive:

Note that the directory name cannot contain Chinese characters and spaces. The directory is introduced as follows:

  • bin: binary executable file. The most commonly used file in it is startup.bat
  • conf: Configuration directory. The core file inside is server.xml . You can change the port number in it. The default port number is 8080, which means that this port number cannot be used by other applications.
  • lib: library file. The directory where the jar package required by tomcat is running
  • logs: logs
  • temp: Temporarily generated files, that is, cache
  • webapps: Web applications. The web application is placed in this directory and the browser can directly access it
  • work: The compiled class file.

Before running the software, make sure that the Java environment variables have been configured:

In the above figure, the variable name is JAVA_HOME, and the variable value is: the absolute path of the JDK installation.

Note: Catalina_Home environment variable: specify which tomcat tomcat starts at startup, which is generally not recommended.

Go back to the bin directory of tomcat, double-click startup.bat:

Then the following interface pops up:

At this time, the local server has been set up. If you want to shut down the server , you can directly close the above window, or enter Ctrl+C in it to disable the service.

First check the ip address of your computer. The ip address of my computer is: 192.168.1.112.

Enter http://192.168.1.112:8080/ in the browser (or http://localhost:8080/ is also possible). If the following interface pops up and enters the home page of the local server, it means that tomcat is successfully installed and started:

In the picture above, the version of my Tomcat is shown as: 8.0.14. Its version number follows the JDK version, so the recommended JDK version is 1.8.

Let's test it on the browser now:

First create a new jsp file in the D:\apache-tomcat-8.0.14\webapps\ROOT directory:

Fill in the following content in the jsp file:

 
<%

String name = request.getParameter("name");
String pwd = request.getParameter("password");

out.print("name:" + name + ",password:" + pwd); //Display the username and password in the input address on the browser

%> 
 

Now we randomly create an account with a username and password, such as the username smyhvae and the password smyh, and then enter the following in the browser:

http://192.168.1.112:8080/test.jsp?name=smyhvae&password=smyh

After entering this link, press Enter, and the following interface appears:

The above figure shows that we send such a request to the server. In the link, the content before the question mark represents the path of the request, and after the question mark is the parameter we want to transmit (the key is fixed, and the value is filled in by the user), and then The server returns us data like this.

 

3. Associate Tomcat with eclipse:

Open eclipse for Java EE, select Windows-->preferences in the menu bar, and the following interface will pop up:

In the figure above, click the "add" button, and the following interface will pop up:

In the above figure, select the corresponding Tomcat version and continue:

In the above figure, select the path of Tomcat and JRE, and click "Finish" to complete the configuration.

Create a new java project and build a dynamic project:

Note: Understanding of "dynamic": html is static, what is written is what it is. Dynamic refers to the dynamic generation of pages based on the data returned by the server. For example, when Zhang San logs in, you can see Zhang San's information; if you change it to Li Si's login, you can see Li Si's information.

Click on the red box in the figure above, and the following interface will pop up:

Configure according to the above figure, in which, the third red box is the installation path to load your own jdk:

Then, click finish. continue:

 

Project file structure:

In the above figure, deployment descriptor: The description of the deployment. Web App Libraries: Packages added by yourself can be placed in it. build: Put the compiled file. WebContent: Put into the written page.

Create a new jsp file in the WebContent folder. Its default code can be seen in the image below:

In the above picture, this encoding method does not support Chinese. Let's modify the encoding method of the JSP file. As shown in the figure above, right-click the mouse, select "Preferences", and the following dialog box will pop up:

In the above picture, change the encoding method to UTF-8.

Similarly, we also need to change the encoding method of the text content to UTF-8 (this encoding has nothing to do with the program), select the menu bar Windows--preferences, open the following interface, change the encoding method to UTF-8, and click update:

Every time you create a new jsp file in the future, the default encoding method is UTF-8. The default code is as follows:

 
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10
11 </body>
12 </html>
 

Let's modify it in hello.jsp, change the title on line 7 above, and add an output statement on line 10. The final code is as follows:

 
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <title>hello JSP</title>
 8 </head>
 9 <body>
10     <%
11         out.println("hello,JSP");
12     %>
13 </body>
14 </html>
 

Line 11 above, out refers to the output stream. Get this output stream through the page and print it in the page. The System.out.println() previously learned refers to output to the console.

Before running the program, let's modify the browser options:

Now we start running the program:

When running, the following error pops up: (If there is no such error, please ignore)

The reason is that we clicked ​startup.bat in the Tomcat installation package before, so that the Tomcat server was manually opened, which is obviously redundant, because when the program is running, eclipse will automatically start the Tomcat server. So we first manually turn off the tomcat software , run the program again, and that's it. The console information is as follows:

The browser will automatically open, and the webpage information is as follows:

Now let's explain why the name of the above URL is displayed as http://localhost:8080/TomcatTest/

We select the project, right-click and select "properties", the following dialog box will pop up:

The above figure shows that the path we deployed is the root directory, and the name of the root directory defaults to the name of our newly created project, so the URL will be displayed as: host name + port number + project name.

The error displayed on the webpage is 404, that is, the webpage cannot be found. It can be seen that the newly created jsp file is not seen in the webpage. Let's find out the reason. Open the project file, the web.xml file in the WEB-INF directory:

The above figure explains: When the program runs, Tomcat will first read the configuration file of the project, and the name must be web.xml. When the default link entered by the system is: host name + port + project name, the server will find the page in the <welcome-file-list> tag in the above figure (if there are several pages, look for it in turn); There is no hello.jsp file in the tag <welcome-file-list>. Therefore, we need to enter: http://localhost:8080/TomcatTest/hello.jsp in the browser address bar to display the hello.jsp page. The effect is as follows:

Fourth, the principle of program operation:

Let us now analyze the principle of the operation of the above program.

When running on the server, a folder will be generated alongside the project file: Servers. As follows: (If the Servers folder is deleted, the folder will be automatically generated when it is re-run)

This folder is a basic configuration of the Tomcat server.

The above figure shows that our new project has been deployed to the Tomcat server, that is, we see that the TomcatTest project has been released (the publishing process is: package the written project and put it in Tomcat).

In fact, eclipse for EE already includes a plug-in for the Tomcat service, but it must also rely on Tomcat to start. We double-click the red box part of the above figure, and the following information is displayed:

The red box part of the above figure shows that the deployment of the service is in eclipse (by default it is placed in the .metadata folder in the workspace), not in Tomcat. Let's change it. The premise is that the project has not been released to Tomcat, then let's delete the previously released version (republished later):

Then you can modify the deployment path:

In the above figure, use the Tomcat installation directory as the deployment location, and modify the deployment path Deploy path (it is recommended to change it to Tomcat's webapps directory), and then save it. At this point, re-run the program . Now I come to Tomcat's webapps directory and find that there is an additional TomcatTest folder (ie the project file name), and the folder contains the content of the WebContent in the project file:

The above figure shows that this is the real release of the program to the server.

Let's go to Tomcat's work directory and look at the compiled files:

The above figure shows that Tomcat will first convert the jsp file into a java file, then compile the java file into a class file, and finally execute the class file . Now let's see how JSP works.

 

Fourth, the operating principle of JSP:

  • Only when the client requests the JSP for the first time, it needs to be converted and compiled (so the second time the same page is browsed, it will be faster)
  • When the web server encounters a request to access a JSP web page, it first executes the program fragment in it, and then returns the execution result to the client in HTML format.
  • Snippets can manipulate databases, redirect web pages, send emails, etc. This is what you need to build a dynamic website.
  • All program operations are performed on the server side, and only the results are sent to the client on the network, and the requirements for the client's browser are minimal.

Summary: Throughout the process of this article, we have learned how to configure Tomcat and deploy project files, and understand how jsp files are published on the server and finally displayed. Further study on JSP will be presented later.

 

 

 

5. Other problems with Tomcat:

1. Port occupancy problem:

Enter the netstat -ano command in cmd to view the pid of the process occupying the port, and then use the task manager to close the corresponding process.

We enter "www.baidu.com" in the browser, but we can still enter the web page without entering the port number. This is because the default port number of the browser is 80. If the other party's server is listening on port 80, it is browsing When entering the URL in the server, you can not enter the port number.

The port number that Tomcat listens to by default is 8080 (line 63 of the server.xml file), which can be modified in the configuration file conf /server.xml .

[Remarks] Tomcat associated help documentation Javadoc

If we want to use the Servlet class in the future, but want to check the source code and help documents inside, we find that we can't see it:

After holding down ctrl and clicking in, it is the following interface:

The tomcat we downloaded comes with the source code, but the help documentation needs to be downloaded separately:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325220229&siteId=291194637