重新补全。此程序可以成功跳转到welcome界面,但是不能成功跳转至failure界面,总是显示404无资源错误
login.jsp
<%@page language="java" import="java.util.*" pageEncoding="GBK"%>
<form action="test" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"/></td>
</tr>                                                                                                                                                                               
<tr>
<td>密码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
</tr>
<tr>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>loginservlet.java
package org.com;
import java.io.IOException;
//import java.io.PrintWriter;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class loginservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname1=request.getParameter("username");
String password1=request.getParameter("password");
usebean ub=new usebean();
ub.setUname(uname1);
ub.setPword(password1);
request.setCharacterEncoding("GBK");
String forward;
if(ub.getUname().equals("zs")&&ub.getPword().equals("123"))
{
    forward="welcom.jsp";
}
else
{
forward="failure.jsp";
}
RequestDispatcher rd=request.getRequestDispatcher(forward);
    rd.forward(request, response);

}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
            this.doGet(request, response);
}}
usebean.java
package org.com;
public class usebean {
private String uname;
private String pword;
public usebean(){}
public String getUname() {
return uname;
}
   public usebean(String uname ,String pword)
   {
   this.uname=uname;
   this.pword=pword;
   }
   
public void setUname(String uname) {
this.uname = uname;
}
public String getPword() {
return pword;
}
public void setPword(String pword) {
this.pword = pword;
} public boolean pd(String uname1,String pword1)
{
if(("zs".equals(uname1))&&("123".equals(pword1)))
{
 return true;
}
else
{
               return false;
}
}
}
welcom.jsp
<%@page language="java" import="java.util.*" pageEncoding="GBK"%>
欢迎光临
failure.jsp
<%@page language="java" import="java.util.*" pageEncoding="GBK"%>
失败
web.xml
 <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>loginservlet</servlet-name>
    <servlet-class>org.com.loginservlet</servlet-class>
  </servlet>  <servlet-mapping>
    <servlet-name>loginservlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>我就不明白了,为什么总是不能成功跳转是failure.jsp页面的,老是404,但是又可以成功跳转至welcome。jsp

解决方案 »

  1.   

    有个地方写错了,不过那样也行就是,修改如下if(ub.pd(ub.getUname(),ub.getPword())==true)
    {
        forward="welcom.jsp";
    }
    else
    {
    forward="failure.jsp";
    }
      

  2.   

    楼主哪!不会吧!说句实话,说句实话,你这代码根本就没有错好不?要不要我复制过来给你看哪!我用的8.6版的myclipse 。不知你用的是哪个版本,可能是你电脑的环境配置有点问题,这就是常说的bug!下面我把原代码发给你看看哈!还包话一些图片!
    loginservlet.java
    package org.com;
    import java.io.IOException;
    //import java.io.PrintWriter;import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class loginservlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    String uname1=request.getParameter("username");
    String password1=request.getParameter("password");
    usebean ub=new usebean();
    ub.setUname(uname1);
    ub.setPword(password1);
    request.setCharacterEncoding("GBK");
    String forward;
    if(ub.getUname().equals("zs")&&ub.getPword().equals("123"))
    {
    forward="welcom.jsp";
    }
    else
    {
    forward="failure.jsp";
    }
    RequestDispatcher rd=request.getRequestDispatcher(forward);
    rd.forward(request, response);}
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
      this.doGet(request, response);
    }}
    usebean.java
    package org.com;
    public class usebean {
    private String uname;
    private String pword;
    public usebean(){}
    public String getUname() {
    return uname;
    }
    public usebean(String uname ,String pword)
    {
    this.uname=uname;
    this.pword=pword;
    }
      
    public void setUname(String uname) {
    this.uname = uname;
    }
    public String getPword() {
    return pword;
    }
    public void setPword(String pword) {
    this.pword = pword;
    }public boolean pd(String uname1,String pword1)
    {
    if(("zs".equals(uname1))&&("123".equals(pword1)))
    {
    return true;
    }
    else
    {
      return false;
    }
    }
    }failure.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%
    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>My JSP 'failure.jsp' starting page</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">
    --> </head> <body>
    失败 </body>
    </html>login.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%
    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>My JSP 'login.jsp' starting page</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">
    --> </head> <body>
    <form action="test" method="post">
    <table>
    <tr>
    <td>
    用户名:
    </td>
    <td>
    <input type="text" name="username" />
    </td>
    </tr>
    <tr>
    <td>
    密码
    </td>
    <td>
    <input type="password" name="password" />
    </td>
    </tr>
    <tr>
    <td>
    <input type="submit" value="提交" />
    </td>
    </tr>
    <tr>
    <td>
    <input type="reset" value="重置" />
    </td>
    </tr>
    </table>
    </form> </body>
    </html>
    welcom.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
    <%
    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>My JSP 'welcom.jsp' starting page</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">
    -->  </head>
      
      <body>
        欢迎光临
        
      </body>
    </html>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <servlet>
      <description>This is the description of my J2EE component</description>
      <display-name>This is the display name of my J2EE component</display-name>
      <servlet-name>loginservlet</servlet-name>
      <servlet-class>org.com.loginservlet</servlet-class>
      </servlet>  <servlet-mapping>
      <servlet-name>loginservlet</servlet-name>
      <url-pattern>/test</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
      <welcome-file>login.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    这些代码我全是复制你的, 没有改写代码。具体的问题还是请你自己去找吧!先把我这个代码复制下来运行一下!。。我可是花了半钟头,希望楼主。。
      

  3.   

    确实,我后面发现了,浪费我那么多时间,是tomcat的问题,我懂的~~~~~~