在C:\java-tomcat\weapss下创建目录helloapp(我们所要创建的第一个web应用),helloapp目录结构如下:
创建的helloapp目录下包含index.html、login.jsp和hello.jsp,以及Servlet类DispatcherServlet.java。创建的步骤依次如下:
1.在helloapp目录下创建index.html,这个文件仅仅用来显示一串带字符“Welcome to HelloApp",以及login in链接。源码如下:
<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>
2.在helloapp目录下创建login.jsp,它显示登陆界面,要求输入用户名和口令。这个页面链接到一个名为DispatcherServlet的Servlet。
<html>
<head>
<title>login in</title>
</head>
<body>
<form name="loginForm" method="POST" action="dispatcher">
<table>
<tr>
<td>User Name: </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password: </td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>
3.在hello目录下创建hello.jsp,这个JSP被DispatcherServlet调用,显示hello页面。
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<b>Welcome:<%=request.getAttribute("USER")%></b>
</body>
</html>
package mypack;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class DispatcherServlet extends HttpServlet{
private String target = "/hello.jsp";
public void init(ServletConfig config)throws ServletException{
super.init(config);
}
public void goGet(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 password = request.getParameter("password");
request.setAttribute("USER",username);
request.setAttribute("PASSWORD",password);
ServletContext context = getServletContext();
System.out.println("Redirecting to " + target);
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
dispatcher.forward(request,response);
}
public void destroy(){
}
}
5.编译DispatcherServlet.java。
(1)将C:\common\lib\servlet-api.jar的路径加入到环境变量classpath中,因为编译时需要使用servlet-api.jar;
(2)将C:\common\lib\servlet-api.jar文件拷贝到jdk\jre\lib\ext下;
(3)编译DispatcherServlet.java —— javac DispatcherServlet.java;
(4)将生成的DispatcherServlet.class文件拷贝到WEB-INF\classes\mypack目录下,如果没有mypack目录,
则需建立mypack 目录。(增加此目录是为了当存在多个Servlet文件时,容易管理)。
6.在helloapp\WEB-INF目录下创建web.xml文件,指定Servlet的名字以及mapping等,在login.jsp中通过action="dispatcher"链接到该Servlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>mypack.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/dispatcher</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
7.在C:\java-tomcat\conf\server.xml中注册该应用。在最后一个/Host之前添加如下信息:
<Context path="/helloapp" docBase="helloapp" debug="0" reloadable="true"/>
最后,启动应用服务器,在浏览器中输入http://localhost:8080/helloapp/index.html即可实现访问。(需要哦加上项目名称)