JSP 开发环境搭建

Tomcat 目录、JSP 放哪里、如何部署与访问、常见启动问题。

Beginner
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 与 .class

Under 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.xml
JSP
<%-- 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

A successful start prints Server startup in ... ms. If that line never appears, startup failed — look for SEVERE in logs/catalina.out.

常见启动问题

SymptomCause
Port already in use8080 上还有别的进程
404 on the JSP文件不在 webapps/<app>/ 下,或文件名大小写不符
Blank pageJSP 里有编译错误,容器只写日志不报给浏览器
Chinese garbled未设 pageEncoding / contentType
Changes not appliedwork/ 缓存了旧的翻译结果
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.

Friends & Partners