简单的留言板,包括三个页面:
提交留言界面ex5_13.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<HTML>
<BODY>
  <FORM action="messagePane.jsp" method="post" name="form">
     <P>输入您的名字:
     <INPUT type="text" name="peopleName"><BR>
     <P>输入您的留言标题:
     <INPUT type="text" name="Title"><BR>
     <P>输入您的留言:<BR>
     <TEXTAREA name="messages" ROWs="10" COLS=36 WRAP="physical">
     </TEXTAREA><BR>
     <INPUT type="submit" value="提交留言" name="submit">
  </FORM>
  <FORM action="showMessage.jsp" method="post" name="forml">
      <INPUT type="submit"  value="查看留言"    name="look">
  </FORM>
</BODY>
</HTML>提交信息messagePane.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<HTML>
<BODY>
  <%!
    Vector v=new Vector();
    ServletContext application;
    synchronized void sendMessage(String s)
    {   v.add(s);
       application=getServletContext();
       application.setAttribute("Mess",v);
    }
  %><%
     String name=(String)request.getParameter("peopleName");
     String title=(String)request.getParameter("Title");
     String messages=(String)request.getParameter("messages");
     if(name==null)
       {name="guest"+(int)(Math.random()*10000);
       }     if(title==null)
       {title="无标题";
       }     if(messages==null)
       {messages=" 无信息";
       }     String time=new Date().toString();
     String s="#"+name+"#"+title+"#"+time+"#"+messages+"#";
     sendMessage(s);
     out.print("您的信息已经提交!"); //字符串信息保存在application对象中
  %>  <A HREF="ex5_13.jsp"> 返回  </A>
<BODY>
</HTML>
显示信息showMessage.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<HTML>
<BODY>
  <%
    Vector v=(Vector)application.getAttribute("Mess");
    out.print("<table border=2>");
    out.print("<tr>");
    out.print("<th bgcolor=cyan>"+"留言者姓名"+"</th>");
    out.print("<th bgcolor=cyan>"+"留言标题"+"</th>");
    out.print("<th bgcolor=cyan>"+"留言时间"+"</th>");
    out.print("<th bgcolor=cyan>"+"留言内容"+"</th>");
    out.print("</tr>");
    for(int i=0;i<v.size();i++)
       { String message=(String)v.elementAt(i); 
         StringTokenizer fenxi=new StringTokenizer(message,"#");
         int number=fenxi.countTokens();
         out.print("<tr>"); //记录行开始.每一次留言,用一行数据表示
         for(int k=0;k<number;k++)  
           { String str=fenxi.nextToken();
             byte a[]=str.getBytes("ISO-8859-1");
             str=new String(a);
             if(k<number-1)
                {      out.print("<td bgcolor=yellow >"+str+"</td>");//输出每一//列单元数据
                }
             else
                {
                  out.print("<td><TextArea  rows=3 cols=30 >"+str+"</TextArea></td>");
                }
            }           out.print("</tr>");//记录行结束
       
       }
     out.print("</table>");  %><BODY>
</HTML>
上述代码大家可以试验一下,问题是用户名/标题/留言为空时,为什么显示为null,而不是"guest""无标题""无信息等"?

解决方案 »

  1.   

    if(name==null) 
           {name="guest"+(int)(Math.random()*10000); 
           }      if(title==null) 
           {title="无标题"; 
           }      if(messages==null) 
           {messages=" 无信息"; 
           } 该成 name.equal("")试试。 如果request得到的属性为空,或是个空字符,输出的结果为“null”这个字符串。貌似是这样,我没有验证,你可以修改后验证下。
      

  2.   

    一楼说的得对 应该把if(name==null)
           {name="guest"+(int)(Math.random()*10000);
           }  改成if(name==null||name.equals(""))
           {name="guest"+(int)(Math.random()*10000);
           }    标题和信息也是同理
      

  3.   


    if(name==null || "".equals(name)){
        // ...... 设置默认值
    }