JavaWeb Learning (1) ---- JSP introduction and entry-level (including Tomcat usage), javaweb ---- jsp

Source: Internet
Author: User

JavaWeb Learning (1) ---- JSP introduction and entry-level (including Tomcat usage), javaweb ---- jsp

 [Statement] 

Reprinted, but keep the original source of the article → _ →

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

Source: http://www.cnblogs.com/smyhvae/p/4046862.html

Contact: smyhvae@163.com

 

[Preface]

JSP itself is a Java Web knowledge, but when learning Android network, it must involve interaction with the server. Therefore, it is necessary to learn JSP and other JavaWeb content, at least understand the principle of the entire process when the program accesses the server.

In fact, before learning Android, Java and Java Web must be learned first. I started my research on Android in July 2014 and did not have any knowledge about computer software before. (the only thing that is related to this science is that I have taken a C language course, but I have forgotten it now ).

Let's take a look at the figure below:

The two figures are exactly the same. I used Li Gang's "Crazy Java learning road map" for reference. The cell phone pixel scum is not very clear. I will try it on later. To put it bluntly, you must have a certain Java Foundation (Java SE, Java Web, database, etc.) before learning Android ). If you want to learn a solid foundation and have a good job, you must have the following basic knowledge:Basic computer knowledge (operating system, computer network, data structure, database, design mode, etc.), C/C ++, Java, Android, Linux.

Therefore, considering the time, beginners like me can only learn other things while learning Android.Where I learned, my blog will writeWe also hope to share with other beginners and witness it together!

 

[Body]

I. Introduction to JSP

JSP: Java Server Pages. Adding Java program snippets (Scriptlet) and JSP tags to traditional HTML files (* htm, *. html) constitutes a JSP webpage.

 

Ii. Learning focus:

  • JSP syntax (script, command, Action)
  • Built-in JSP objects
  • Create dynamic content
  • User session tracking

 

III,Use tomcat software to build a local server:

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. Its source code is completely open.

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

Tomcat software A is an open-source project under apache. Download link: http://tomcat.apache.org/

After downloading the package, decompress it:

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

  • Bin: Binary execution file. The most common file in it is startup. bat.
  • Conf: configuration file. The core file is server. xml. You can change the port number. The default port number is 8080, that is, this port number cannot be used by other applications. Application usage.
  • Lib: Library File
  • Logs: Log
  • Temp: temporary files, that is, Cache
  • Webapps: web applications. If the application program is compiled, you can put it in this directory for Tomcat to run.
  • Work: compiled class files.

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

The variable name is JAVA_HOME, and the variable value is: the absolute path of JDK installation.

Return to the bin directory of tomcat and double-click startup. bat:

The following page is displayed:

At this time, the local server has been set up.If you want to disable the server, You can close the window above, or enter Ctrl + C 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 enter http: // localhost: 8080 ). If the following page is displayed, go to the home page of the local server, indicating that tomcat is successfully installed and started:

The above figure shows that my Tomcat version is 8.0.14. Its version number follows the JDK version. Therefore, it is recommended that the JDK version be 1.8.

Now let's test it in the browser:

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

Enter 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 in the browser %>

Now we can start an account with the username and password, for example, the username smyhvae and the password smyh, and enter the following content in the browser:

Http: // 192.168.1.112: 8080/test. jsp? Name = smyhvae & password = smyh

Enter the link and press Enter. The following page is displayed:

In, we send such a request to the server. In the link, the content before the question mark represents the request path, and the question mark is followed by the parameters we want to transmit (the keys are fixed, the value is entered by the user), and the server returns the data to us.

 

3. Associate Tomcat with eclipse:

Open eclipse for Java EE and select Windows --> preferences from the menu bar. The following page is displayed:

Click "add" to bring up the following interface:

Select the corresponding Tomcat version and continue:

Select the Tomcat path and JRE, and click "finish". The configuration is complete.

Create a java project and a dynamic project:

Note: Understanding of "dynamic": html is static and what is written. Dynamic refers to dynamically generating pages based on the data returned by the server. For example, if you log on to Michael Jacob, you can see Michael Jacob's information. If you log on to Michael Lee, you can see information about Michael Lee.

Project file structure:

, Deployment descriptor: deployment description. Web App Libraries: you can add your own package to it. Build: Put the compiled files. WebContent: Enter the written page.

Create a jsp file in the WebContent folder. You can see its default code in:

. Let's modify the encoding method, as shown in. Right-click and select "Preferences". The following dialog box is displayed:

, Change the encoding method to UTF-8. After each new jsp file, the default encoding method is the 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 

In hello. jsp, modify the title of row 7th above and add the output statement to row 10th. 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  7 <title>hello JSP</title> 8 10     <%11         out.println("hello,JSP");12     %>13 </body>14 

The top 11th rows indicate the output stream. Obtain the output stream from the page and print it on the page. The previously learned System. out. println () refers to the output to the console.

Before running the program, modify the browser options:

Now we start to run the program:

During running, the following error is displayed: (ignore this error if it does not exist)

The reason is that we have previously clicked startup. bat in the Tomcat installation package to manually open the Tomcat server. This is obviously redundant because eclipse will automatically enable the Tomcat server when running the program. So firstManually turn off tomcat SoftwareRun the program again. The console information is as follows:

The browser automatically opens the web page. The webpage information is as follows:

Now I want to explain why the above URL Name displays http: // localhost: 8080/TomcatTest/

Right-click the project and choose properties. The following dialog box is displayed:

Show that the path we deploy is the root directory. The name of the root directory is the name of the project we created by default, so the URL is displayed as: Host Name + port number + project name.

The Error 404 is displayed on the webpage, that is, the webpage cannot be found. The newly created jsp file is not displayed on the webpage. Let's look for the cause. Open the web. xml file in the WEB-INF directory in the project file:

Explanation: when the program runs, Tomcat first reads the configuration file of the project, and the name must be web. xml. When the default access link is host name + port + project name, the server will find the page in the <welcome-file-list> tab (if there are several pages, and the tag <welcome-file-list> does not contain hello. jsp file. Therefore, you must enter http: // localhost: 8080/TomcatTest/hello. jsp in the address bar of your browser to display the hello. jsp page. The effect is as follows:

Iv. program running principle:

Let's analyze the running principle of the above program.

After running on the server, a folder named Servers is generated in parallel with the project file. As follows: (if the Servers folder is deleted, the folder is automatically generated when it is re-run)

This folder is a basic configuration of the Tomcat server.

It indicates that the new project has been deployed on the Tomcat server, that is, the TomcatTest project has been released (the release process is: package the written project and put it in Tomcat ).

In fact, eclipse for EE already contains the Tomcat service plug-in, but it must also be started with Tomcat. Double-click the red box to display the following information:

The red box shows that the service is deployed in eclipse (by default, it is placed in the. metadata folder of the workspace), rather than in Tomcat. Let's change it. If the project is not released to Tomcat, delete the previous version (release later ):

Then you can modify the deployment path:

, Use the Tomcat installation directory as the deployment location, modify the deployment path Deploy path (we recommend that you change it to the Tomcat webapps directory), and then save it. At this time,Re-run the program. Now, go to the webapps directory of Tomcat and find that there is an additional Tomcat test folder (that is, the project file name), and the folder contains the content of WebContent in the project file:

Note: this means that the program is actually published to the server.

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

Indicates,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 take a look at how JSP runs.

 

Iv. operating principles of JSP:

  • Only when the client requests JSP for the first time must it be converted and compiled (so the second time you browse the same page, the speed will be faster)
  • When the Web server encounters a request to access the JSP webpage, it first executes the program fragment and then returns the execution result to the customer in HTML format.
  • Program snippets can operate databases, redirect webpages, and send emails. This is the function required to build a dynamic website.
  • All program operations are performed on the server. The results are only obtained when the network is uploaded to the client, which has the lowest requirements on the client browser.

Summary: The entire process in this article shows us how to configure Tomcat and deploy engineering files, understand how jsp files are published to the server, and finally display them. Further learning about JSP will be presented later.


How should I deploy a common java project (not a web project) containing jsp pages to tomcat?

Deploy to tomcat. The web-inf directory and web. xml will be available.
 
Learning JavaWeb: Learning Servlet first or JSP first?

They're too complicated. Listen to me! Didn't you finish learning the Java basics? You just need to start reading this book. No matter what you talk about first, the book is gradual and easy to understand!

If you don't want to read a book, you can watch the video step by step! Come on!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.