Java web Learning notes-jsp, web Learning notes-jsp
1. java web Overview
1.1 static and dynamic pages
| |
Representation |
Required Technology |
| Static webpage |
The webpage content is fixed and will not be updated |
Html, css |
| Dynamic Web Page |
The webpage content is dynamically displayed by the program and automatically updated |
Html, css, DB, java/c #/php, javascript, xml, mainstream dynamic web scripts (jsp, asp.net, php) |
1.2 build a java web Development Environment
Jdk1.7 + tomcat7.0 + MyEclipse10. For installation and configuration of MyEclipse, see http://blog.sina.com.cn/s/blog_907043b301016jtp.html. The Tomcat server is an open-source project of Apache Jakarta and a Jsp/Servlet container. To install Tomcat, you only need to extract the zip package to the specified directory. Create an environment variable CATALINA_HOME. The variable value is Tomcat's root directory D: \ Program Files (x86) \ apache-tomcat-7.0.57. All environment variables are as follows:
| Variable name |
Variable value |
| JAVA_HOME |
D: \ Program Files (x86) \ Java \ jdk1.7.0 _ 40 |
| Path |
C: \ Program Files \ Microsoft SQL Server \ 100 \ DTS \ Binn \; % JAVA_HOME % \ bin; D: \ Program Files \ Sublime Text 3; D: \ Program Files \ MySQL Utilities 1.3.6 \ |
| Classpath |
.; % JAVA_HOME % \ lib \ rt. jar; % JAVA_HOME % \ lib \ tools. jar; |
| CATALINA_HOME |
D: \ Program Files (x86) \ apache-tomcat-7.0.57 |
Then we will test the Tomcat homepage:
Go to the bin directory under the root directory of the Tomcat server and run startup. bat as an administrator. If the running result is as follows, the Tomcat server is successfully started.
Note:Do not close this window (closing the window means closing the Tomcat server and minimizing it ).
Enter http: // localhost: 8080/in the address bar of the browser and press enter to obtain the following page:
1.3Tomcat directory structure
1.4 manually write the first web application
Create an index. jsp in the project folder:
1 <! DOCTYPE html> 2
Create a WEB-INF directory in the project directory copy/webapps/examples/WEB-INFO/web. xm to your project/myJspProject/WEB-INFO, create two folders in/myJspProject/WEB-INFO: classes and lib. The final project directory should be as follows:
Test: Enter http: // localhost: 8080/myJspProject/index. jsp in the browser and press Enter. The running result is as follows:
Solution: Change the browser encoding to the specified encoding:
Web-INF directory details
This directory is the security directory of java web applications. The so-called security directory is the directory that the client cannot access only the server. Specifically, web. xml is the project deployment file, the classes Directory: stores the *. class file, and the lib directory stores the required jar package. For example, if we create a test.html file in web-inf, we can access it through a browser:
Web. the xml configuration file can be used to configure the welcome page. The default welcome page is the index under the project. jsp, add the haha under the project. jsp, in/WEB-INF/web. add the following code to the xml web-app Tag:
1 <welcome-file-list>2 <welcome-file>/haha.jsp</welcome-file>3</welcome-file-list>
Running result:
1.6 compile the first web application in Eclipse
Note: If you want to use Eclipse to write java web applications, you should use Eclipse's J2EE version. Configure the Tomcat Server Window-Preference-Server-Runtime Environment-Add in Eclipse, and create a jsp file in the WebContent directory, press Ctrl + F11 (or right-click the project-Run On Server) to access the website built in a built-in browser.
1.7 use MyEclipse to write the first web application
Configure jre and tomcat in MyEclipse before creating a project. Step Window-Preference-Java-Install JREs-Add; Window-MyEclipse-Servers-Tomcat (Note: Set the jre of tomcat and set the server to Enabled ).
Start the Tomcat server in MyEclipse:
Test homepage http: // localhost: 8080/proves that Tomcat is started properly, and we can start and release Web applications in MyEclipse.
A New WebProject generates the following directory structure (by default, there is an index. jsp under the WebRoot directory ).
Publish the WebApp.
1.8 understand the virtual path of a project
This virtual path is the right-click property-MyEclipse-Web on the project that can be modified.
Redeployment: The Browser needs to use http: // localhost: 8080/hello/index. jsp for access.
1.7 modify Tomcat default port
Modify the following mark of server. xml in the conf directory:
<Connector connectionTimeout="20000" port="8888" protocol="HTTP/1.1" redirectPort="8443"/>
2. Introduction to jsp syntax 2.1jsp
The full name of jsp is Java Server Page, which is a simplified Servlet design. It implements the use of html tags in java. Jsp is a dynamic web page technology that complies with J2EE standards. Jsp and Servlet are also executed on the server.
2.2 comparison of common dynamic website development technologies
| Platform |
Features |
| Jsp |
Cross-platform, high security, suitable for developing large-scale, enterprise-level Web applications and distributed applications (Hadoop ). For example: 12306, 10086.cn, and online banking |
| Asp.net |
Easy to learn, poor security and cross-platform |
| Php |
Simple, efficient, low cost, and short development cycle. It is suitable for the Web Application Development (LAMP) of small and medium-sized enterprises) |
2.3jsp page element introduction and page instructions
Page command syntax:
<% @ Page attribute 1 = "attribute value" attribute 2 = "attribute value 1, attribute value 2" attribute n = "attribute value n" %>
| Attribute |
Description |
Default Value |
| Language |
Script Language used for jsp pages |
Java |
| Import |
Reference the class files to be used in the script language |
None |
| ContentType |
Code of the specified jsp page |
Text/html, ISO-8859-1 |
To create a java web project, the default jsp page starts with a page command:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
The default encoding is ISO-8859-1 and does not support Chinese characters. Another property contentType is recommended here. Change the first line to the following:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
In this way, Chinese characters are supported.
2.4jsp comments
There are three types: html comments, jsp comments, and jsp script comments. Syntax:
1 <! -- Html comment --> 2 <% -- jsp comment -- %> 3 <% 4/* There are two jsp script Annotations */5 6 // single line comment 7 8 /* multi-line comment */9%>
For example:
1 <% @ page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8
Deploy the project to Tomcat, and use the browser to view the source code:
2.5jsp script
Java code executed on the jsp page. Syntax:
<% Java code %>
1 <% @ page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8 2.6jsp Declaration
Define variables or methods on the jsp page. Syntax:
<%! Java code %>
For example:
1 <%! 2 String s = "Zhang San"; // declare a String type variable 3 int add (int x, int y) {// declare a method 4 return x + y whose return value is int type; 5} 6%>2.7jsp expression
The expression executed on the jsp page. Syntax (note = There is no semicolon at the end of the expression next to the percent sign ):
<% = Expression %>
1 <% @ page language = "java" import = "java. util. * "contentType =" text/html; charset = UTF-8 "%> 2 <% 3 String path = request. getContextPath (); 4 String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; 5%> 6 7 <! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"> 8
Running result:
2.8jsp page lifecycle Stage 1 Project 3. jsp built-in object 4. java beans5.jsp Status Management 6. jsp commands and action elements 7. jsp case project