我用myeclipse生成的工程文件
loginServlet放在excel\WebRoot\WEB-INF\classes的login包里,web.xml也在classes下,要调用loginServlet的index.jsp放在excel\WebRoot下,web.xml里的相关部分我是这样写的<servlet>
<servlet-name>loginServlet</servlet-name>
<servlet-class>login.loginServlet</servlet-class>
</servlet><servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
打开index.jsp时的url是http://localhost:8080/excel/WebRoot/index.jsp
提交表单时,action="loginServlet?method=userLogin"
跳转以后的提示总是:
HTTP Status 404 - /excel/WebRoot/loginServlet--------------------------------------------------------------------------------type Status reportmessage /excel/WebRoot/loginServletdescription The requested resource (/excel/WebRoot/loginServlet) is not available.
--------------------------------------------------------------------------------Apache Tomcat/6.0.14
请问要如何注册servlet和如何调用servlet才能成功,这个问题搞了很久了

解决方案 »

  1.   

    更正一下
    跳转以后的提示是这样的:
    HTTP Status 404 - /excel/loginServlet--------------------------------------------------------------------------------type Status reportmessage /excel/loginServletdescription The requested resource (/excel/loginServlet) is not available.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.14没有中间的WebRoot,是网页忘记刷新所致,不好意思。
    我总觉得我没有哪里错了
      

  2.   

    <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet.do</url-pattern>
    </servlet-mapping>
      

  3.   

    "message /excel/loginServlet "
    楼住路径错误.
      

  4.   

    你的servlet访问路径跟你的servlet实际路径无关,只和你在web.xml中的配置有关,你的servlet的访问路径应该是http://<address>:<port>/<contextType>/loginServlet ;
    address是服务地址,port是端口,contextType是程序上下文根
      

  5.   

    问问ROINAPOLLO,.do是必要的吗?message是什么?
    我的index.jsp的代码是:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>Excel在线练习考试系统</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script language="JavaScript">
    function userLogin()
    {
         var uName = document.getElementById("userName").value;
         var uPsw = document.getElementById("userPsw").value;
         if(uName == null || uName.length<1)
         {
         alert('请输入用户名!');
             return;
         }
         if(uPsw == null || uPsw.length<1)
         {
         alert('请输入用户密码!');
             return;
         }     document.form1.action="loginServlet?method=userLogin";
         document.form1.submit();
    }
    </script>
      </head>
      
      <body>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <form name="form1">
      <table width="587" height="337" border="0" align="center">
        <tr>
          <td width="289" height="333"><table width="273" height="331" border="0" align="left">
            <tr>
              <td width="267" height="155"><img src="WebRoot/WEB-INF/imgs/logo.jpg" alt="Excel Online" width="283" height="227"></td>
            </tr>
            <tr>
              <td height="23" align="right" valign="middle">
                <label></label>
                <label>
                <input type="button" name="button2" id="button2" value="  注册  " onClick="location='reg.jsp'">
                </label>         
               </td>
            </tr>
            <tr>
              <td height="71">&nbsp;</td>
            </tr>      </table></td>
          <td width="32" align="center" valign="middle"><img src="WebRoot/WEB-INF/imgs/middleline.jpg" width="28" height="283"></td>
          <td width="252"><table width="224" border="0" align="left">
            <tr>
              <td height="62" colspan="2" bordercolor="0">&nbsp;</td>
            </tr>
            <tr>
              <td height="6" colspan="2" bordercolor="0"><table width="227" border="0" align="left">
                <tr>
                  <td width="92" height="27" align="left" valign="middle"><img src="WebRoot/WEB-INF/imgs/yonghuming.jpg" alt="�û���" width="61" height="21"></td>
                  <td width="119" align="right">
                    <label>
                      <input type="text" name="userName" id="userName">
                      </label>             
                  </td>
                </tr>
              </table>
              </td>
            </tr>
            <tr>
              <td height="6" colspan="2" bordercolor="0">&nbsp;</td>
            </tr>
            <tr>
              <td height="14" colspan="2" bordercolor="0"> <table width="239" border="0" align="left">
                <tr>
                  <td width="61" height="26" align="left" valign="middle"><img src="WebRoot/WEB-INF/imgs/mima.jpg" alt="����" width="61" height="21"></td>
                  <td width="168" align="center">
                    <label>
                      <input type="text" name="userPsw" id="userPsw">
                      </label>            
                  </td>
                </tr>
              </table>
              </td>
            </tr>
            <tr>
              <td height="14" colspan="2" bordercolor="0">&nbsp;</td>
            </tr>
            <tr>
              <td width="144" height="45" bordercolor="0">&nbsp;</td>
              <td width="70" align="left" valign="top" bordercolor="0">
                <label></label>
                <input type="button"  onclick="userLogin();" value="  确定  "></td>
            </tr>
          </table>
          </td>
        </tr>
      </table>
      </form>
      <br>
      </body>
    </html>
      

  6.   

    document.form1.action="loginServlet?method=userLogin"; 
    ==============>document.form1.action="/loginServlet?method=userLogin"; 
      

  7.   

    url变为http://localhost:8080/loginServlet?userName=1&userPsw=1提示是:
    HTTP Status 404 - /loginServlet--------------------------------------------------------------------------------type Status reportmessage /loginServletdescription The requested resource (/loginServlet) is not available.
    --------------------------------------------------------------------------------Apache Tomcat/6.0.14
      

  8.   

    用绝对路径
    action="<%=basePath%>loginServlet?method=userLogin";  
    另外
    未编译的java文件放在classes下面是没用的
    classes下面放的是编译好的class文件
    应该建一个login的包
    将loginServlet放在login包下面
    编译之后classes文件自动到classes下面了
      

  9.   

    回答wangxf_8341:
    我把上面杂七杂八的内容
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
    <% 
    String path = request.getContextPath(); 
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
    %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
       <head> 
         <base href=" <%=basePath%>"> 
         
         <title>Excel在线练习考试系统 </title> 
    <meta http-equiv="pragma" content="no-cache"> 
    <meta http-equiv="cache-control" content="no-cache"> 
    <meta http-equiv="expires" content="0">     
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
    <meta http-equiv="description" content="This is my page"> 
    <!-- 
    <link rel="stylesheet" type="text/css" href="styles.css"> 
    --> 
    都去掉了,改了各种servlet-mapping和href都不行第二,我的classes下面全部放的都是class文件,因为这是myeclipse自动编译放置的
      

  10.   

    我再补充一下我的问题
    我以前有做过JSP的网页,也成功用过servlet,用的是比较原始的工具JCreator,一个一个文件写的那些什么注册之类的方法和原理我都懂这回用MyEclipse做,新建一个Web Project以后,网页和根目录之间就多了一个WebRoot挺不习惯的希望有用MyEclipse的仁兄之间告诉我答案吧,那些原理和方法在网上太多太多了,不然我也不会跑这里来问大家虚心求教中
      

  11.   

    action="loginServlet?method=userLogin"这里路径有问题
      

  12.   

    我也知道是我的调用和servlet-mapping之间肯定出了问题,就是不知怎么改或者有哪位可以给我一个调用成功的示例,让我模仿模仿我琢磨Tomcat自带的Servlet示例HelloWorld半天,越看越头晕
      

  13.   

    action="/loginServlet?method=userLogin" 
      

  14.   

    web.xml也在classes下。。楼主这样写的话又些不和道理!!
      

  15.   

    <servlet>
    <servlet-name>Photo</servlet-name>
    <servlet-class>
    com.startech.query.server.GetPhotoServlet
    </servlet-class>
    </servlet> <servlet-mapping>
    <servlet-name>Photo</servlet-name>
    <url-pattern>/photo</url-pattern>
    </servlet-mapping>
      

  16.   

    web.xml没有在classes下,在WEB-INF下
    前面写错了,不好意思
      

  17.   

    JSP里的action配置

    web.xml的url-pattern>/loginServlet</url-pattern>
    对不上
      

  18.   

    <servlet-class>login.loginServlet</servlet-class>这个也有问题
      

  19.   

    在form中设置属性action="webRoot/loginservlet?method=userLogin"
      

  20.   

    action="/loginServlet?method=userLogin"
      

  21.   

    错误是误差的累积。
    我在工程里面添加servlet的时候,开发平台告诉我了servlet-mapping,url等等信息,我曾经改过,改了以后就出现了404的错误。
    按给出的信息编程下去是不会出现404错误的。
    做人何必那么累,没叫我改,我还是用开发平台提供的现成的就好了。问题解决,辛苦各位了!