首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

Servlet开发乞援

2011-12-25 
Servlet开发求助在下写了个Servlet,用HTML提交,但是运行后出现错误,请各位指教,万分感激:type:Statusrepor

Servlet开发求助
在下写了个Servlet,用HTML提交,但是运行后出现错误,请各位指教,万分感激:
type:   Status   report
message:   /ServletModule/ServletModule/customerregistration
description:   The   requested   resource   (/ServletModule/ServletModule/customerregistration)   is   not   available.

我的XML描述是:
<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<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_4.xsd "   version= "2.4 ">
    <display-name> ServletModule </display-name>
    <servlet>
        <servlet-name> customerregistration </servlet-name>
        <servlet-class> example_lg_1.CustomerRegistration </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name> customerregistration </servlet-name>
        <url-pattern> /customerregistration </url-pattern>
    </servlet-mapping>
</web-app>

HTML文件:
<html>
<head>
<meta   content= "text/html;charset=gb2312 "   />
<title> 用户注册表 </title>
</head>
<body>
<form   action= "ServletModule/customerregistration "method= "post ">
<table   width= "100% "   border= "0 "   bgcolor= "center ">
    <tr   align= "center "   valign= "top ">
        <td   colspan= "2 ">
          <h1>
            <em>
              <strong> 用户注册表 </strong>
            </em>
          </h1>
        </td>
    </tr>
    <tr>
      <td>
        <span>
          <strong> 用户   ID: </strong>
        </span>
      </td>
      <td>
        <input   name= "txtuserid "   type= "text "   id= "txtuserid "   size= "45 "   maxlength= "35 ">
      </td>
    </tr>
    <tr>
      <td>
        <span>
          <strong> 密码: </strong>
        </span>
      </td>
      <td>
        <input   name= "txtpassword "   type= "password "   id= "txtpassword "   size= "12 "
        maxlength= "10 ">
      </td>
    </tr>
    <tr>
      <td>
        <span>


          <strong> 姓名: </strong>
        </span>
      </td>
      <td>
        <input   name= "txtname "   type= "text "   id= "txtname "   maxlength= "15 "/>
      </td>
    </tr>
    <tr>
      <td>
        <span>
          <strong> 性别: </strong>
        </span>
      </td>
      <td>
        <input   name= "rbgender "   type= "radio "   value= "男 "   checked/>
        男
        <input   name= "rbgender "   type= "radio "   value= "女 "   checked/>
        女
      </td>
    </tr>
    <tr>
      <td>
        <span>
          <strong> 年龄: </strong>
        </span>
      </td>
      <td>
        <input   name= "txtage "   type= "text "   id= "txtage "   size= "3 "   maxlength= "2 "/>
        岁
      </td>
    </tr>
    <tr>
      <td> &nbsp; </td>
      <td>
        <input   name= "Submit "   type= "submit "   value= "提交 "/>
        <input   type= "reset "   name= "Reset "   value= "重置 "/>
      </td>
    </tr>
</table>
</form>
</body>
</html>

类文件:
package   example_lg_1;

import   javax.servlet.*;
import   javax.servlet.http.*;
import   java.io.*;
import   java.util.*;

public   class   CustomerRegistration
        extends   HttpServlet   {
    private   static   final   String   CONTENT_TYPE   =   "text/html;   charset=GBK ";

    //Initialize   global   variables
    public   void   init()   throws   ServletException   {
    }

    //Process   the   HTTP   Get   request
    public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)   throws
            ServletException,   IOException   {
        request.setCharacterEncoding( "GBK ");
        response.setContentType(CONTENT_TYPE);
        PrintWriter   out   =   response.getWriter();
        out.println( " <html> ");
        out.println( " <head> <title> CustomerRegistration </title> </head> ");


        out.println( " <body   bgcolor=\ "#ffffff\ "> ");
        String   UserId   =   request.getParameter( "txtuserid ");
        if   (UserId   ==   null)   {
            UserId   =   " ";
        }
        String   Password   =   request.getParameter( "txtpassword ");
        if   (Password   ==   null)   {
            Password   =   " ";
        }
        String   FirstName   =   request.getParameter( "txtName ");
        if   (FirstName   ==   null)   {
            FirstName   =   " ";
        }
        String   Gender   =   request.getParameter( "rbgender ");
        if   (Gender   ==   null)   {
            Gender   =   " ";
        }
        String   Age   =   request.getParameter( "txtage ");
        if   (Age   ==   null)   {
            Age   =   " ";
        }
        out.println( " <table   border=0   align=center   width=50% ");
        out.println( " <tr> <td   colspan=2> <center> <font   color=red   size=6> 用户注册详细信息 </font> </center> </td> </tr> ");
        out.println( " <tr> <td> 用户ID: </td> <td> <Font   Color=red   Face=宋体> "+UserId+ " </Font> </td> </tr> ");
        out.println( " <tr> <td> 密码: </td> <td> <Font   Color=red   Face=宋体> "+Password+ " </Font> </td> </tr> ");
        out.println( " <tr> <td> 姓名: </td> <td> <Font   Color=red   Face=宋体> "+FirstName+ " </Font> </td> </tr> ");
        out.println( " <tr> <td> 性别: </td> <td> <Font   Color=red   Face=宋体> 女 </Font> </td> </tr> ");
        out.println( " <tr> <td> 年龄: </td> <td> <Font   Color=red   Face=宋体> "+Age+ " </Font> </td> </tr> ");
        out.println( " </table> ");
        out.println( " </body> ");
        out.println( " </html> ");
        out.close();
    }

    //Process   the   HTTP   Post   request
    public   void   doPost(HttpServletRequest   request,   HttpServletResponse   response)   throws
            ServletException,   IOException   {
        doGet(request,   response);
    }



    //Clean   up   resources
    public   void   destroy()   {
    }
}


[解决办法]
<form action= "ServletModule/customerregistration "method= "post "> 改成
<form action= "customerregistration "method= "post "> 试试
[解决办法]
恩 form action设置为servlet的名字
[解决办法]
<form action= "ServletModule/customerregistration "method= "post ">
改成 <form action= "/ServletModule/customerregistration "method= "post ">

String FirstName = request.getParameter( "txtName ");
改成 String FirstName = request.getParameter( "txtname ");

类文件CustomerRegistration里面加上 request.setCharacterEncoding( "gbk ");

out.println( " <tr> <td> 性别: </td> <td> <Font Color=red Face=宋体> 女 </Font> </td> </tr> ");
改成
out.println( " <tr> <td> 性别: </td> <td> <Font Color=red Face=宋体> "+Gender+ " </Font> </td> </tr> ");
[解决办法]
servlet的位置和web.xml没映射上

热点排行