Build a Web environment, JSP acquaintance
1. master environment to build and use of Tomcat
1.1 understand the mainstream web server
Server (hardware):
在公网上用来运行特别的程序,或者此程序需要的运行环境的,能够给广大用户提供服务的一台计算机。
一般配置比较高,同样也有人专门进行维护(运维)。
Server (software):
如果只有硬件那么用户也是无法访问资源内容,例如使用ServerSocket技术提供接受请求并处理然后响应的能力。
But ServerSocket technology is too close to the bottom, so there are teams and organizations have these things directly to a good package (developed a mature product), we only need to deploy such software on server hardware, the user can then start to provide for the service.
Mainstream server:
- Apache static server
- Static server Nginx
- Jetty
- Jboss
- WebLogic
- Tomcat
- …
Master Tomcat 1.2 environment to build
- Download 8.5 https://tomcat.apache.org/download-80.cgi
- Extract the zip file
- Under the start bin directory
startup.bat
- In the browser address bar enter the
localhost:8080
Tomcat default port number is 8080 - bin directory
shutdown.bat
off the server / close the window
Installation Contents Introduction:
- bin can run the batch file
- startup.bat/sh
- shutdown.bat/sh
- catalina.bat/sh
- conf server configuration file
- server.xml server configuration information
- Web.xml configuration information in the server project
- Context.xml configuration information within the server resources
- lib server needs to run a number of related jar library / package
- el-api.jar
- jasper.jar
- jasper-el.jar
- jsp-api.jar
- servlet-api.jar
- tomcat-dbcp.jar
- tomcat-jni.jar
- logs Server log folder
- catalina. .log date information about the server is running
- localhost_access_log. Date .log server access information
- temp server needs at runtime temporary file storage directory
- (Important) webapps : Web store directory project
- ROOT (root directory /)
- work: it is about and JSP , JSP code will be stored in the translation here
1.3 master common configuration with Tomcat
-
Modify the port number:
Modify ports server.xml installation directory under the conf (line 69)
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
-
Modify the welcome page:
The final piece in the web.xml under the conf directory
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
1.4 Tomcat deployment project
- Find
webapps
directory - In addition to the ROOT directory, we can create a new folder (in English) in the webapps directory
- Your project will be unified on the resource which can be
- Input will be finished in the browser address bar
localhost:8080
to continue to write anywhere in the webapps directory of your project is located after- If your project is in a directory under the webapps
test
directory, you need to access thelocalhost:8080/test/
prefix
- If your project is in a directory under the webapps
- You can create a new folder within your project files in a
WEB-INF
directory, which can be used to store a new web.xml to configure your specific project application, it has higher priority than the web.xml in the server conf directory.
2. Create a web master Eclipse project
-
new -> others -> Search dy ... find a dynamic web project
-
According to fill in the content below
- title
- Runtime (Java EE Library)
- Project Templates 2.5 because 2.5 template automatically creates web.xml and 3.
-
web 2.5 version of the project directory structure is as follows:
-
In order to be able to deploy a web project we created, we need to make some server configurations IDE
- window -> show view -> servers tab
- 点击链接创建新的服务器(这个服务器其实是Eclipse基于真实tomcat去创建的临时/镜像服务器,为的是以免影响到真实tomcat目录)
- 为了能够使服务器部署项目时正常被我们所观察,我们需要将其默认的配置目录进行更改,如下。(Ctrl+S)
!
- 接下来可以将我们的项目放到此服务器中了
- 右键服务器 -> add and remove -> 添加项目
- 启动服务器(start)
3. 掌握JSP的基本使用
3.0 了解动态网页技术
传统的静态网页技术无法满足动态数据的需求,我们的HTML无法嵌入数据库查询的代码,所以我们需要将HTML结合Java代码进行使用,那么产生了JSP(Java Server Pages)
Java服务器页面。 HTML + Java = JSP。
扩展名.jsp
,在Eclipse创建JSP小心编码。(window -> 首选项 -> jspfiles -> encoding)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
3.1 掌握JSP的指令
<!-- page指令是用来配置JSP页面的信息的,它可以出现多次,也可以出现在任意位置,一般放在头部
language:标识此JSP页面可以嵌套的脚本语言
contentType:设置响应头信息
pageEncoding:设置本页面的编码
import:导入本页面Java代码所需要的包
-->
<%@ page language="java" contentType="text/html; charset=UTF-8" import=""
pageEncoding="UTF-8"%>
<%@include %>
<%@taglib %>
3.2 掌握JSP的脚本
<% Java代码 %>
能写一部分Java代码,但是无法定义方法,它可以定义在JSP页面的任意位置。
它的代码是被放到了JSP转成的.java文件的_jspService方法内。
3.3 掌握JSP的表达式
<%=Java代码 %>
能写一部分Java代码,但是调用方法时,不能调用无返回值的方法,不能在其中添加;
号,当然也不能定义方法等。=号后面的内容,可以在页面输出。
它的代码是被放到了JSP转成的.java文件的_jspService方法内,并且转化为了out.print() 向页面输出的代码
。
3.4 掌握JSP的声明
<%! Java代码 %>
Write part of Java code, but although the method can be defined, but can not call method .
Its code is placed in the position of a member of the JSP turn into a .java file.
3.5 understand and master the implementation of the principle of JSP
Java code you want to run: .java -> .class to run
The Tomcat work directory (and its related JSP): storing the contents of JSP translation.
JSP at runtime, you need to go through three steps.
- Translation: the JSP is essentially a template, to Jasper when a program Tomcat in this template will be translated, translated into a Java class. If the content has changed, you need to be translated.
- Compile: translated Java classes to be compiled
- Run: compiled byte code file to run