JavaWeb_JavaWeb开发的目录结构
第一个 WEB 应用程序:开发,并部署到 Tomcat 服务器下运行
-
1.在 Eclipse 新建一个 Java Project
-
2.在 Java 项目下创建 WEB 开发的目录结构
- WebContent
-WEB-INF
- classes (编译后的 class 文件必须放在该目录下。)
- lib
- web.xml (从 apache-tomcat-6.0.16\webapps\docs\WEB-INF 中复制过来,可以不做修改)
- html 页面
- JSP 页面
- 图片... -
3.在 src 下新建一个 Person 类
package com.xs.test;
public class Person {
public String getPersonInfo(){
return "person info...";
}
}
-
4.手工的 Person 类对应的 class 文件(含包)复制到 classes 目录下可以通过修改默认的输出目录达到自动把编译好的 class 放到 classes 目录下:
-
5.在 WebContent 目录下新建一个 JSP 文件:
<%@page import="com.xs.test.Person"%>
<%@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>
<!-- Java 代码写在如下的里边。 -->
<%
Person person = new Person();
System.out.print(person.getPersonInfo());
%>
</body>
</html>
-
6.把 WebContent 目录复制到 Tomcat 的 webapps 目录下,并改名为:helloworld
-
7.在浏览器的地址栏中输入: http://localhost:8989/helloworld/hello.jsp 即可看到命令行的打印信息。
-
8.配置任意目录下的Web应用程序: conf 目录下依次创建 catalina\localhost 目录,然后在 localhost 目录下为 test 这个Web应用程序建立 test.xml 文件,编辑这个文件输入以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="E:\\Java\\Source\\atguigu\\java-1\\firstWebApp\\WebContent" reloadable="true"/>