创建web项目
打开netbeans,在菜单栏依次选择“File”、“New Project...”,然后
1、选择java web:
2、项目名称、路径:
3、选择使用的web容器,编写ContextPath:
生成的web项目结构如下:
index.html的内容如下:
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</html>
context.xml
的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/HelloJSP"/>
apache-tomcat-8.0.15/conf/server.xml
中关于端口的配置如下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
运行项目后,netbeans的输出信息如下:
ant -f /data/Code/netbeans/HelloJSP -Dnb.internal.action.name=run -Ddirectory.deployment.supported=true -DforceRedeploy=false -Dnb.wait.for.caches=true -Dbrowser.context=/data/Code/netbeans/HelloJSP run
init:
deps-module-jar:
deps-ear-jar:
deps-jar:
library-inclusion-in-archive:
library-inclusion-in-manifest:
compile:
compile-jsps:
Incrementally deploying http://localhost:8084/HelloJSP
Completed incremental distribution of http://localhost:8084/HelloJSP
run-deploy:
Browsing: http://localhost:8084/HelloJSP
run-display-browser:
run:
使用浏览器访问http://localhost:8084/HelloJSP/
,能看到TODO write content
。
那么问题来了,为什么配置的是8080,访问时却用8084端口?
答案是:这个端口是netbeans启动tomcat时配置的。
看一下启动命令:
$ ps -ef | grep 'tomcat' | grep -v grep
letian 390 24314 0 09:24 ? 00:00:08 /data/Apps/jdk1.8.0_20/bin/java -Djava.util.logging.config.file=/home/letian/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dhttp.nonProxyHosts=localhost|127.0.0.1|myhost -Djava.endorsed.dirs=/data/Apps/apache-tomcat-8.0.15/endorsed -classpath /data/Apps/apache-tomcat-8.0.15/bin/bootstrap.jar:/data/Apps/apache-tomcat-8.0.15/bin/tomcat-juli.jar -Dcatalina.base=/home/letian/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base -Dcatalina.home=/data/Apps/apache-tomcat-8.0.15 -Djava.io.tmpdir=/home/letian/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/temp org.apache.catalina.startup.Bootstrap start
$ grep -r '8084' /home/letian/.netbeans/8.0.2 # 可以看到很多结果
...
也就是说,实际使用的是/home/letian/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/conf
下的配置。在`/home/letian/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/conf/Catalina/localhost/HelloJSP.xml
中有以下内容:
<Context antiJARLocking="true" docBase="/data/Code/netbeans/HelloJSP/build/web" path="/HelloJSP"/>
这意味着访问http://localhost:8084/HelloJSP/
时对应的web应用部署在HelloJSP项目
的build/web/
目录下。
基于JSP的hello world
删除index.html,新建index.jsp,内容如下:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String data="hello world";
boolean flag=true;
if (flag==true) {
out.println("<h1>" +data.toUpperCase()+ "</h1>");
}
%>
</body>
</html>
运行项目,访问http://localhost:8084/HelloJSP/
,可以看到HELLO WORLD
。
JSP在部署时会被转换成servlet,/home/letian/.netbeans/8.0.2/apache-tomcat-8.0.15.0_base/work/Catalina/localhost/HelloJSP/org/apache/jsp
中的index_jsp.java
就是对应的servlet。其内容如下: