Javaweb Study Notes 5--jsp Introduction and Getting Started (including the configuration of Eclipse for Java EE and Tomcat)

Source: Internet
Author: User
Tags java se

? "declaration"

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

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

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

Objective

JSP itself is the knowledge in the Javaweb, but in learning the Android network, it is necessary to involve the interaction with the server, so it is necessary to learn the JSP and other javaweb content, at least to understand the program in the access to the server, the whole process of the principle.

In fact, before learning Android, Java and javaweb knowledge is to learn first. I was formally in July 2014 to start the study of the android direction, before this was not exposed to any computer software related knowledge (the only relevant is that this science has a C language course, but now forget the light).

Let's take a look at this picture below:

Two pictures together to see the right, for reference is Li Gang's "Crazy Java Learning Roadmap", cell phone pixel slag, the picture is not very clear, will do it. To be frank, you have to have a certain Java Foundation (Java SE, javaweb, database, etc.) before you learn Android. And if you want to learn solid and have a good job, the following basic knowledge needs to have all: basic knowledge of the computer (operating system, computer network, data structure, database, design patterns, etc.), C + +, Java, Android, Linux.

So, considering the time factor, beginners like me can only learn other knowledge while learning Android. I learned where, my blog will write where , but also hope to share with other beginners, witness together!

Body

I. INTRODUCTION of JSP

Jsp:java Server Pages. Adding Java program Fragments (scriptlet) and JSP tags (tag) to a traditional HTML file (*htm,*.html) constitutes a JSP Web page.

Second, the focus of learning:

    • JSP syntax (scripts, instructions, actions)
    • Built-in objects for JSPs
    • Create a dynamic content
    • User session Tracking

Third, Use the Tomcat software to build the server locally:

Tomcat is an important subproject in the Apache organization's Jakarta Project and is a container (engine) that Sun recommends for running Servlets and JSPs, and its source code is fully exposed.

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

Tomcat Soft Armor is an open source project owned by Apache. Software Download Link: http://tomcat.apache.org/

After downloading, unzip the package:

Note The directory name cannot have Chinese and white space. The catalogue is described below:

    • Bin: Binary execution file. The most commonly used documents are Startup.bat .
    • Conf: Configuration directory. The core of the document is Server.xml. You can change the port number in the inside. The default port number is 8080, which means that the port number cannot be used by other applications.
    • LIB: library file. The directory where the Tomcat runtime requires a jar package
    • Logs: Log
    • Temp: Files that are temporarily generated, that is, the cache
    • The Webapps:web application. Web apps placed in this directory can be accessed directly by the browser
    • Work: Compile the following class file.

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

, the variable is named Java_home and the value of the variable is: the absolute path of the JDK installation.

Note: Catalina_home environment variable: Specifies which tomcat to start when Tomcat is started, and is generally not recommended for configuration.

Back in the Tomcat Bin directory, double-click Startup.bat:

After that, the following screen pops up:

At this time, the local server has been built up. if you want to shut down the server , you can either close the window above or enter CTRL + C to disable the service.

First look at your computer's IP address, my computer's IP address is: 192.168.1.112.

Enter http://192.168.1.112:8080/in the browser (or enter http://localhost:8080/). If the following screen appears, enter the home page of the local server, indicating that the Tomcat installation was successful and started up:

In the picture above, the version that shows my tomcat is: 8.0.14. Its version number is followed by the JDK version, so it is recommended that the 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:

The JSP file fills in the following content:

<%string name = Request.getparameter ("name"); String pwd = request.getparameter ("password"); Out.print ("Name:" + name + ", Password:" + pwd); Display the user name and password in the input address on the browser%>

Now we have a random username and password account, such as user name smyhvae, password is 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, enter, the following interface appears:

, we send to the server such a request, the link, the question mark before the content represents the path of the request, the question mark is the parameter we want to transfer (the key is fixed, the value is the user fill in), and then the server returned to us such data.

Third, associate Tomcat with Eclipse:

Open Eclipse for Java EE, select Menu bar windows-->preferences, pop up the following interface:

, click on the "Add" button to bring up the following interface:

, select the corresponding Tomcat version to continue:

, select the Tomcat path, and the JRE, click "Finish" and configure.

Create a new Java project and build a dynamic project:

Note: The understanding of "dynamic": HTML is static, what is written, is what. Dynamic refers to dynamically generating pages based on the data returned from the server side. For example, Zhang San landing can see Zhang San information, John Doe Landing, you can see John Doe information.

Click on the Red box in the section, pop up the following interface:

Follow the configuration, where the third red box is the installation path to load your own JDK:

Then, click Finish. Go on:

Project file Structure:

, Deployment Descriptor: Description of the deployment. Web App Libraries: Add your own package can be placed inside. Build: Put the compiled file. WebContent: Put into the Write page.

Create a new JSP file under the WebContent folder. You can see its default code in:

, this encoding does not support Chinese. We will modify the JSP file encoding, as shown, the right mouse click, select "Preferences", the following dialog box appears:

, change the encoding method to UTF-8.

Similarly, we also need to change the text content encoding to UTF-8 (this encoding and program-independent), select the menu bar windows--preferences, open the following interface, the encoding method to UTF-8, and click Update:

Each time you create a new JSP file, 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 

We'll change it in hello.jsp, change the title of line 7th above and add the output statement on line 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      <%11         out.println ("hello,jsp"),     %></body>14 

The 11th line above, out refers to the output stream. This output stream is obtained from the page and printed on the page. Previously learned System.out.println () refers to the output to the console.

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

Now let's start running the program:

At run time, the following error pops up: (Ignore if you do not have this error)

The reason is that we clicked on the "Startup.bat" in the Tomcat installation package, so we opened the Tomcat server manually, which is obviously redundant because eclipse automatically opens the Tomcat server when the program is running. So let's start by manually shutting down the Tomcat software and running the program again. The console information is as follows:

The browser opens automatically and the page information is as follows:

Now let's explain why the name of the top URL shows http://localhost:8080/TomcatTest/.

We select the project, right click "Properties", the following dialog box appears:

Shows that the path we are deploying is the root directory, the name of the root directory defaults to our new project name, so the URL will be displayed as: hostname + port number + project name.

The page display error is 404, that is, cannot find the Web page, the visible page does not see our new JSP file, we look for the reason. Open the Web. xml file under the Web-inf directory in the project file:

Explanation: When the program is running, Tomcat will first read the project's configuration file, and the name must be Web. Xml. When the system enters by default the link is: Host name + port + project name, the server will find in the <welcome-file-list> tag page (there are several pages, then look down), while the label <welcome-file-list> There are no hello.jsp files in the. Therefore, we need to enter in the browser address bar: http://localhost:8080/TomcatTest/hello.jsp, the hello.jsp page will be displayed. The effect is as follows:

Four, the principle of program operation:

Let's now analyze how the above program works.

When run on the server, a folder is generated alongside the project file: Servers. as follows: (If you delete the Servers folder, the folder is automatically generated when you rerun)

This folder is a basic configuration of the Tomcat server.

Shows that our new project has been deployed to the Tomcat server, which means that the project has been published (the process of releasing it: putting the written project into Tomcat after it is tomcattest).

In fact, Eclipse for EE already contains plug-ins for the Tomcat service, but it must also rely on Tomcat to boot. We double-click on the Red Box section to display the following information:

The Red box section indicates that the deployment of the service is within eclipse (by default in the. metadata folder in the workspace), not inside Tomcat. Let's change it, if the project isn't posted to tomcat, let's delete the previously released version (republish later):

You can then modify the path of the deployment:

, use Tomcat's installation directory as the location for the deployment, and modify the deployment path to deploy to (WebApps the Tomcat directory), and then save. At this point, rerun the program . Now, under the WebApps directory of Tomcat, I found a tomcattest folder (the project file name), and the folder contains the contents of WebContent in the project file:

Description, which shows that this is the actual release of the program to the server.

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

?

Indicates thatTomcat 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 look at how JSP works.

Four, the operation Principle of JSP:

    • Only when the client requests the JSP for the first time will it need to be converted, compiled (so the second time you browse the same page, the speed will be faster)
    • When a Web server encounters a request to access a JSP Web page, it first executes the fragment of the program, and then returns the execution result to the customer in HTML format.
    • Program fragments can manipulate databases, redirect Web pages, send email, and so on, which is what you need to build a dynamic website.
    • All program operations are performed on the server side, and the only results that are delivered to the client on the network are the minimum requirements for the client's browser.

Summary: Throughout this article, let's learn how to configure Tomcat and deploy the project files to see how the JSP files are posted to the server and eventually displayed. Further learning about JSPs will be presented later.

V. Other issues with Tomcat:

1, port occupancy issues:

Enter the Netstat-ano command in CMD to view the process PID that occupies the port, and then close the process with Task Manager.

We entered "www.baidu.com" in the browser, but did not enter the port number will still be able to access the Web page, because the browser default port number is 80, if the other server is listening on the 80 port, then enter the URL in the browser, you can not enter the port number.

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

Remarks Tomcat Association help document Javadoc

If we want to use the Servlet class later, but want to see the source code and help document inside, we find that we can't see:

After holding down the CTRL key, it is the following interface:

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

Javaweb Study Notes 5--jsp Introduction and Getting Started (including the configuration of Eclipse for Java EE and Tomcat)

Related Article

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.