JSP 开发环境搭建
Tomcat 目录、JSP 放哪里、如何部署与访问、常见启动问题。
Key PointsJSP 放在 Web 应用的根目录或子目录下即可直接访问;放进 WEB-INF 则只能被内部转发。
Tomcat 目录
Text
tomcat/
├── bin/ # startup.sh / shutdown.sh / catalina.sh
├── conf/ # server.xml、web.xml、tomcat-users.xml
├── lib/ # 容器自身的 jar,对所有应用可见
├── logs/ # catalina.out 等日志
├── temp/ # 编译 JSP 的临时目录
├── webapps/ # 应用部署目录
│ ├── ROOT/ # 根应用,访问 http://localhost:8080/
│ └── myapp/ # 访问 http://localhost:8080/myapp/
└── work/ # JSP 翻译后的 .java 与 .classUnder work/ you will find index_jsp.java, the servlet your JSP was translated into. Reading it beats guessing when something behaves oddly.
最小可运行应用
Text
webapps/myapp/
├── index.jsp
└── WEB-INF/
└── web.xmlJSP
<%-- index.jsp --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html><body>
<h1>部署成功</h1>
<p>上下文路径:<%= request.getContextPath() %></p>
</body></html>Bash
curl -s http://localhost:8080/myapp/Output
<!DOCTYPE html>
<html><body>
<h1>部署成功</h1>
<p>上下文路径:/myapp</p>
</body></html>启动与停止
Bash
# Linux / macOS
bin/startup.sh
bin/shutdown.sh
# Windows
bin\startup.bat
bin\shutdown.batA successful start prints Server startup in ... ms. If that line never appears, startup failed — look for SEVERE in logs/catalina.out.
常见启动问题
| Symptom | Cause |
|---|---|
| Port already in use | 8080 上还有别的进程 |
| 404 on the JSP | 文件不在 webapps/<app>/ 下,或文件名大小写不符 |
| Blank page | JSP 里有编译错误,容器只写日志不报给浏览器 |
| Chinese garbled | 未设 pageEncoding / contentType |
| Changes not applied | work/ 缓存了旧的翻译结果 |
JSP compile errors are never shown in the browser — you just get a blank page. Always check
logs/catalina.out or logs/localhost.<date>.log first; it names the exact line.Tomcat 目录
Text
tomcat/
├── bin/ # startup.sh / shutdown.sh / catalina.sh
├── conf/ # server.xml、web.xml、tomcat-users.xml
├── lib/ # 容器自身的 jar,对所有应用可见
├── logs/ # catalina.out 等日志
├── temp/ # 编译 JSP 的临时目录
├── webapps/ # 应用部署目录
│ ├── ROOT/ # 根应用,访问 http://localhost:8080/
│ └── myapp/ # 访问 http://localhost:8080/myapp/
└── work/ # JSP 翻译后的 .java 与 .classwork/ 目录里能看到 index_jsp.java——这就是容器把你的 JSP 翻译出来的 Servlet 源码。调试诡异问题时读它,比猜有用得多。
最小可运行应用
Text
webapps/myapp/
├── index.jsp
└── WEB-INF/
└── web.xmlJSP
<%-- index.jsp --%>
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html><body>
<h1>部署成功</h1>
<p>上下文路径:<%= request.getContextPath() %></p>
</body></html>Bash
curl -s http://localhost:8080/myapp/Output
<!DOCTYPE html>
<html><body>
<h1>部署成功</h1>
<p>上下文路径:/myapp</p>
</body></html>启动与停止
Bash
# Linux / macOS
bin/startup.sh
bin/shutdown.sh
# Windows
bin\startup.bat
bin\shutdown.bat启动成功会打印 Server startup in ... ms。看不到这行就是启动失败,去 logs/catalina.out 找 SEVERE。
常见启动问题
| 现象 | 原因 |
|---|---|
| 端口被占用 | 8080 上还有别的进程 |
| 访问 404 | 文件不在 webapps/<app>/ 下,或文件名大小写不符 |
| 空白页 | JSP 里有编译错误,容器只写日志不报给浏览器 |
| 中文乱码 | 未设 pageEncoding / contentType |
| 改了没生效 | work/ 缓存了旧的翻译结果 |
JSP 的编译错误不会显示在浏览器上,页面只是一片空白。排查第一步永远是看
logs/catalina.out 或 logs/localhost.<date>.log,里面会精确指出行号。