## 創建web項目
打開netbeans,在菜單欄依次選擇“File”、“New Project...”,然后
1、選擇java web:
[](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/00-03/01.png)
2、項目名稱、路徑:
[](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/00-03/02.png)
3、選擇使用的web容器,編寫ContextPath:
[](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/00-03/03.png)
生成的web項目結構如下:
[](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/00-03/04.png)
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/`目錄下。
## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/00-03.md#基于jsp的hello-world)基于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。其內容如下:
~~~
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/8.0.15
* Generated at: 2015-09-18 02:43:43 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
final java.lang.String _jspx_method = request.getMethod();
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSPs only permit GET POST or HEAD");
return;
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html;charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\n");
out.write("<!DOCTYPE html>\n");
out.write("<html>\n");
out.write(" <head>\n");
out.write(" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n");
out.write(" <title>JSP Page</title>\n");
out.write(" </head>\n");
out.write(" <body>\n");
out.write(" ");
String data="hello world";
boolean flag=true;
if (flag==true) {
out.println("<h1>" +data.toUpperCase()+ "</h1>");
}
out.write("\n");
out.write(" </body>\n");
out.write("</html>\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
~~~
感受一下這段代碼吧~
關于JSP就介紹這么多。**需要記住的是:JSP最合適的用途是用作MVC中的視圖,而不是和HTML一起混合編碼 (例如把從數據庫拉取數據也放入JSP中寫)**
要了解更多,請參考下面的相關資料。
## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/00-03.md#jsp相關資料)JSP相關資料
[JSP 教程](http://www.runoob.com/jsp/jsp-tutorial.html)
- JSP & Servlet
- 00-00、序
- 00-01、相關軟件的安裝
- 00-02、理解HTTP
- 00-03、從JSP開始
- 00-04、理解Servlet
- 00-05、過濾器與監聽器
- 00-06、使用velocity模板引擎
- 00-07、使用數據庫連接池
- 00-08、Tomcat的運行機制
- Spring MVC
- 01-00、Spring與依賴注入
- 01-01、Spring與面向切面編程
- 01-02、使用Spring MVC構建Hello World
- 01-03、JdbcTemplate
- 01-04、基于注解的URL映射
- 01-05、JSON
- 01-06、校驗器
- 01-07、國際化
- 01-08、攔截器
- 01-09、文件上傳
- 01-10、轉換器與格式化
- Book
- Online Tutorial
- Q & A
- Learn More
- Supplement