z 建立Java Web开发环境
9 安装JDK5.0并设置环境变量:
Java_home C:\Program Files\Java\jdk1.6.0_02
Class_path %Java_home%\lib
Path %Java_home%\bin
9 安装Tomcat:直接解压即可,设解压位置为d:\tomcat 6。
解压后进入bin目录, 双击startup.bat启动Tomcat服务器,
打开浏览器在地址栏输入 http://localhost:8080,如果页面
能正常显示Apache Tomcat则服务器安装配置成功。
9 安装带Lomboz 插件的Eclipse。直接解压即可。
9 安装文本编辑器UltraEdit, 用于手工建立第一个Web应用。
z 手工建立第一个Web应用的步骤
1. 在tomcat的安装目录(d:\tomcat 6)下的webapps目录下建
立helloapp目录。 即位置为d:\tomcat 6\webapps\helloapp。
2. 在helloapp目录下建立WEB‐INF 目录,并在WEB‐INF下建
立classes目录。
3. 在文本编辑器中创建如下组件:
9 HTML组件:index.html
9 JSP组件:login.jsp, hello.jsp
9 Servlet组件:LoginServlet
以上组件之间的链接关系为: 张 燕 实验 2参考文档
index.html Æ login.jsp Æ LoginServlet Æ hello.jsp
各组件的源代码见后面。
4. 配置web.xml文件,如下:
5. 文件在磁盘上的组织结构为:
<?xml version="1.0" encoding="UTF‐8"?>
<web‐app version="2.4"
xmlns=" http://java.sun.com/xml/ns/j2ee"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema‐instance"
xsi:schemaLocation=" http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web‐app_2_4.xsd">
<servlet>
<servlet‐name>loginServlet</servlet‐name>
<servlet‐class>servlet.LoginServlet</servlet‐class>
</servlet>
<servlet‐mapping>
<servlet‐name>loginServlet</servlet‐name>
<url‐pattern>/loginServlet</url‐pattern>
</servlet‐mapping>
<welcome‐file‐list>
<welcome‐file>index.html</welcome‐file>
</welcome‐file‐list>
</web‐app>
<CATALINA‐HOME>\webapps目录
helloapp 目录
WEB‐INF目录
LoginServlet.class
web.xml
index.html
login.jsp
hello.jsp
servlet 目录
classes目录 张 燕 实验 2参考文档
6. 在 server.xml(d:\tomcat 6\conf\server.xml)中的最后一个
</host>之前添加:
<Context path="/helloapp" docBase="helloapp" debug="0" reloadable="true" />
7. 打开浏览器,在地址栏输入 http://localhost:8080/helloapp
访问你部署好的第一个Java Web应用。
z 在Eclipse中建立Web应用
1. 新建Web工程
2. 在Web Content 下分别新建index.html, login.jsp, hello.jsp.
3. 在Java Resource/src下新建class,位于包servlet下。
4. 编辑Web Content 下WEB‐INF下的 web.xml。 在</web‐app>
之前添加上面加深的部分。
5. 在 Eclipse‐> Windows ‐> Prefrences ‐> Server ‐> Installed
Runtimes 下,选择Add,在弹出对话框中选择Apache,选
择tomcat 6.0,为tomcat installation directory选择磁盘上
安装的tomcat 6.0的安装目录。Ok即可。
6. 在Eclipse中打开 Servers 视图:Windows‐> show view 中选
择 Servers。在 Eclipse 底部会有一个 Servers 的视图,该视
图的空白处右键,选择New‐>Server,在弹出的对话框中选
择你刚刚添加的tomcat v6.0 server,点击Next,将编写的
helloapp 工程添加到右侧窗口(部署),点击 finish。添加成
功后在该服务器上右键,选择start,启动 tomcat 服务器。
7. 打开浏览器输入 http://localhost:8080/helloapp 访问部署
成功的Web应用。 张 燕 实验 2参考文档
z 各组件源代码:
9 index.html:
9 login.jsp
<html>
<head>
<title>helloapp</title>
</head>
<body>
<p><font size=5> Welcome to HelloApp</font></p>
<p><a href="login.jsp"> Login In </a></p>
</body>
<html>
<head>
<title>login in</title>
</head>
<body>
<form name="loginForm" method="post" action="loginServlet">
<table>
<tr>
<td> User Name: </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td> Password: </td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="提交"></td>
</tr>
</table>
</form>
</body>
</html> 张 燕 实验 2参考文档
9 hello.jsp
9 LoginServlet.java (提供LoginServlet.class 文件)
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<b>Welcome: <%=request.getAttribute("USER") %></b>
</body>
</html>
package servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class LoginServlet extends HttpServlet{
private String target = "/hello.jsp";
public void init(servletConfig config)throws ServeltException{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
request.setAttribute("USER",username);
request.setAttribute("PASSWORD",pwd);
RequestDispatcher disp = request.getRequestDispatcher(target);
disp.forward(request, response);
}
}