日期:2014-05-17  浏览次数:20687 次

HTTP Status 404 - Servlet TestJSP is not available
HTTP Status 404 - Servlet TestJSP is not available 

-------------------------------------------- 

type Status report 

message Servlet TestJSP is not available 

description The requested resource (Servlet TestJSP is not available) is not available. 

以上是出现的问题。 

案例是当访问者输入姓名后,单击“提交”按钮,将返回一句话。 
创建过程: 
1.在tomcat安装目录下的webapps目录下,新建ch02文件夹; 
2.打开记事本,在ch02文件夹下编写一个Servlet类,用来获取请求中某个参数的值。其代码如下: 
package ch02; 
import java.io.*; 
import java.io.PrintWriter; 
import javax.servlet.*; 
import javax.servlet.http.*; 
public class Test extends HttpServlet 

  public void doGet (HttpServletRequest request,HttpServletResponse response) throws 

ServletException,IOException 
  { 
  String name=request.getParameter("name"); 
  String SuccessInfo="Congratulate you,your JSP environment of configure have Successed!"+name; 
   


  response.setContentType("text/html"); 
  PrintWriter out =response.getWriter(); 


  out.println(" <html>"); 
  out.println(" <head>"); 
  out.println(" <title>"); 
  out.println("SuccessPage"); 
  out.println(" </title>"); 
  out.println(" </head>"); 
  out.println(" <body>"); 
  out.println("SuccessInfo"); 
  out.println(" </body>"); 
  out.println(" </html>"); 
  out.close(); 
  } 


  public void doPost(HttpServletRequest request,HttpServletResponse response) throws 

ServletException,IOException 
  { 
  doGet(request,response); 
  } 


将上述文件另存为Test.java,打开命令提示符窗口,切换到该目录下并编译该文件。 
4.在ch02文件夹下新建一个文件夹WEB-INF,在里面新建一个web.xml,代码如下: 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<web-app 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_5.xsd" 
  version="2.5"> 
  <servlet> 
  <servlet-name>TestJSP </servlet-name> 
  <servlet-class>ch02.Test </servlet-class> 
  </servlet> 


  <servlet-mapping> 
  <servlet-name>TestJSP </servlet-name> 
  <url-pattern>/first </url-pattern> 
  </servlet-mapping> 

</web-app> 
5.编写一个HTML页面,success.html,代码如下 
<html> 
<body> 
<form action="first" method="post"> 
请输入您的姓名: <input type ="text" name="name"> <p>