第一  JSP代码
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form id="form1" name="form1" method="post" action="/loginCheck">
  用户名:
    <label>
  <input name="loginName" type="text" id="loginName" />
  </label>
  <p>口令:
    <label>
    <input name="pw" type="password" id="pw" />
    </label>
    <label>
    <input type="submit" name="Submit" value="提交" />
    </label>
  </p>
</form>
</body>
</html>第二  Servlet程序
package my;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LoginServlet extends HttpServlet 
{    protected void doPost(HttpServletRequest request,HttpServletResponse response)
                                throws ServletException,java.io.IOException
   {
      ServletContext application=getServletContext() ;
      ServletConfig config=getServletConfig() ;
      response.setContentType("text/html;charset=gb2312");
      PrintWriter out=response.getWriter(); 
      HttpSession session =request.getSession();
      String name=request.getParameter("loginName");
      String pw  =request.getParameter("pw");
      if(name!=null &&  name.length()!=0 && pw!=null && pw.length()!=0)
      {
       if(name.equals("tom")&& pw.equals("123"))
       out.print("登录成功");
       else
       out.print("用户名或口令不对");
       }
  }
}
编译后的类文件我已放好 我是在Tomcat 5.5\webapps\ROOT\WEB-INF\classes下第三  Tomcat 5.5\webapps\ROOT\WEB-INF下的Servlet部署信息加:<servlet>
<servlet-name>login</servlet-name>
    <servlet-class>my.LoginServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/loginCheck</url-pattern>
  </servlet-mapping>然后重启Tomcat状况是  :不管我用户密码怎么输,提交后页面是空白!!!
救救我吧 怎么改啊???我快疯了!

解决方案 »

  1.   

    <form id="form1" name="form1" method="post" action="/loginCheck"> 这里,把action="/loginCheck"改成action="loginCheck"就ok了
      

  2.   

    你这个状况只是个“表现”,你应该具体看几个地方
    1。你是否成功发布了servlet。如果你知道如何debug,在servlet中添加断点来debug,如果不会,至少在servlet里进行log输出,再不济也多写几行System.out.println(“...”)来判断状况
    2。如果成功发布了,再看返回是否有错误
    3。如果成功发布了,但是servlet也没有被call,那就有可能是你发布的URL需要查一下,看看大小写什么的一些容易忽略的地方。
      

  3.   

    debug 模式运行,if(name!=null &&  name.length()!=0 && pw!=null && pw.length()!=0) 在这一行打断点,看 name 和pw 有没有取到值。
      

  4.   

    检查一下你的WEB应用上下文。
    将表单的action属性加上应用上下文,例如:<% String contextPath = request.getContextPath(); %> 
    <form action="<%=contextPath%>/sub/submit.jsp" method="post"> 
    </form>
    可以参考以前的讨论
    http://topic.csdn.net/u/20091122/21/0809f147-23ec-4c7c-a563-1c0997ed5807.html
      

  5.   

    你的配置文件web.xml中,servletname应该为LoginServlet ,即类名,这个不能随意改的!
      

  6.   

    我的判断失误!
    请检查你的网页浏览器使用的网页编码是否是GB2312?
    如果是GB2312,请修改你的Servlet代码if(name!=null &&  name.length()!=0 && pw!=null && pw.length()!=0) { 
        if(name.equals("tom")&& pw.equals("123")) 
            out.print("登录成功"); 
        else 
            out.print("用户名或口令不对"); 
        } 
    }else {
        out.print("无法预知的错误");
    }看一下表单提交之后是否显示“无法预知的错误”
      

  7.   

    十有八九是servlet编译的编码问题。
    我使用eclipse utf-8重新编译servlet,然后按你所述重新部署一遍,没有什么问题。
    一切输出结果都正常。代码都是复制你网页上的直接保存。就servlet重新编译了一下。
      

  8.   

    debug 调试一下,页面没有内容看看控制台有没有东西输出啊
      

  9.   

    String pw  =request.getParameter("pw"); 
    System.out.println(pw);在servlet里面打印一下 看看控制台台下能不能输出传进来的密码值
      

  10.   

    1.先在servlet里用System.out.println("");输出一下,看程序有没有进去
    2.在servlet最后加上下面两行 
    out.flush();
    out.close();
      

  11.   

    程序在
    LoginServlet 执行没有?
      

  12.   

    把你的doPost 改成下面的代码就行注意红色部分。。
    protected void doPost(HttpServletRequest request,HttpServletResponse response) 
                                    throws ServletException,java.io.IOException 
      { 
          ServletContext application=getServletContext() ; 
          ServletConfig config=getServletConfig() ; 
          response.setContentType("text/html;charset=gb2312"); 
          PrintWriter out=response.getWriter(); 
          HttpSession session =request.getSession(); 
          String name=request.getParameter("loginName"); 
          String pw  =request.getParameter("pw"); 
          if(name!=null &&  name.length()!=0 && pw!=null && pw.length()!=0) 
          { 
          if(name.equals("tom")&& pw.equals("123")) 
          out.print("登录成功"); 
          else 
          out.print("用户名或口令不对"); 
          }       RequestDispatcher rd =request.getRequestDispatcher("/index.jsp");//index.jsp是你想要跳转到哪个页面去的路径。
    rd.forward(request, response);
      } 
      

  13.   

    if(name.equals("tom")&& pw.equals("123")) 
          out.print("登录成功"); 
          else 
          out.print("用户名或口令不对"); 
    改为
    if(name.equals("tom")&& pw.equals("123")) 
          out.print("<javascript>alert('登录成功')</javascript>"); 
          else 
          out.print("<javascript>alert('用户名或口令不对')</javascript>");