第一个javaweb应用helloapp


z 建立Java Web开发环境
9 安装JDK5.0并设置环境变量:
Java_home C:\ProgramFiles\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,如果页面
能正常显示ApacheTomcat则服务器安装配置成功。
9 安装带Lomboz 插件的Eclipse。直接解压即可。
9 安装文本编辑器UltraEdit, 用于手工建立第一个Web应用。
z 手工建立第一个Web应用的步骤
1. 在tomcat的安装目录(d:\tomcat 6)下的webapps目录下建
立helloapp目录。 即位置为d:\tomcat6\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. 文件在磁盘上的组织结构为:








<?xmlversion="1.0"encoding="UTF‐8"?>
<web‐appversion="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>之前添加:
<Contextpath="/helloapp"docBase="helloapp"debug="0"reloadable="true"/>
7. 打开浏览器,在地址栏输入 http://localhost:8080/helloapp
访问你部署好的第一个JavaWeb应用。
z 在Eclipse中建立Web应用
1. 新建Web工程
2. 在WebContent 下分别新建index.html,login.jsp,hello.jsp.
3. 在JavaResource/src下新建class,位于包servlet下。
4. 编辑WebContent 下WEB‐INF下的 web.xml。 在</web‐app>
之前添加上面加深的部分。
5. 在 Eclipse‐> Windows‐> Prefrences‐> Server‐> Installed
Runtimes 下,选择Add,在弹出对话框中选择Apache,选
择tomcat 6.0,为tomcat installation directory选择磁盘上
安装的tomcat6.0的安装目录。Ok即可。
6. 在Eclipse中打开 Servers 视图:Windows‐>showview 中选
择 Servers。在 Eclipse 底部会有一个 Servers 的视图,该视
图的空白处右键,选择New‐>Server,在弹出的对话框中选
择你刚刚添加的tomcatv6.0server,点击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><fontsize=5>WelcometoHelloApp</font></p>
<p><ahref="login.jsp">LoginIn</a></p>
</body>
<html>
<head>
<title>loginin</title>
</head>
<body>
<formname="loginForm"method="post"action="loginServlet">
<table>
<tr>
<td>UserName:</td>
<td><inputtype="text"name="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><inputtype="password"name="pwd"></td>
</tr>
<tr>
<td></td>
<td><inputtype="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>
packageservlet;

importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.io.*;
importjava.util.*;

publicclassLoginServletextendsHttpServlet{
privateStringtarget="/hello.jsp";

publicvoidinit(servletConfigconfig)throwsServeltException{
super.init(config);
}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringusername=request.getParameter("username");
Stringpwd=request.getParameter("pwd");

request.setAttribute("USER",username);
request.setAttribute("PASSWORD",pwd);
RequestDispatcher disp = request.getRequestDispatcher(target);
disp.forward(request, response);
}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值