有两个文件,index.html负责输入:
<input type="textbox" name="uname">
<input type="password" name="upass">
servlet query.java负责逻辑跳转:public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        String username = null, password=null;
        String dataname=null, datapass=null;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("  <BODY>");
//
        username = request.getParameter("uname");
        username = username.replaceAll(" ", "");
        password = request.getParameter("upass");
        password = password.replaceAll(" ", "");
        if(username == null)
        response.sendRedirect("/web30/error.html");
        else
        response.sendRedirect("/web30/success.html");
//
out.println("  </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
如果在index.html中输入的用户名为空的话怎么才能判断出来? if(username == null)好象不行,因为现在总是跳转到第二个页面success.html。

解决方案 »

  1.   

    username = username.replaceAll(" ", ""); 影响空字符串了没有?去掉空格字符,应该不影响吧。
      

  2.   

    if(username!=null&&username.length()>0){
     response.sendRedirect("/web30/success.html"); 
    }else{
            response.sendRedirect("/web30/error.html");
    }       
      

  3.   

    username = request.getParameter("uname"); if( null != username || username != "")or 
    if( org.apache.commons.lang.StringUtils.isBlank(username))
      

  4.   

    if(username == null|| "".equals(username))
      

  5.   

    if(username!=null&&username.trim().length()>0){
    response.sendRedirect("/web30/success.html");
    }else{
            response.sendRedirect("/web30/error.html");

    判断应该到位了
      

  6.   

    if(username == null||"".equals(username)){
            response.sendRedirect("/web30/error.html"); 
            }else {
            response.sendRedirect("/web30/success.html"); 
    }