简单的留言板,包括三个页面:
提交留言界面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""无标题""无信息等"?
提交留言界面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""无标题""无信息等"?
解决方案 »
- 在web实现购物车功能,,最好给一下简单的例子,最好是MVC结构的。JSP+Servlet+JavaBean。
- Struts2中,采用注解的方式配置json,总是错误!
- java webservice 使用XFire,还是使用Axis?
- tomcat 布暑 JAVA 项目
- !!!难道就没人用JNDI连过数据库吗?求助求助求助求助求助求助求助求助求助求助啊
- ibatis动态查询问题
- JSP BODY onload 问题
- sqlserver的JDBC驱动,上哪找?
- 我在书上看到的连接数据库都是用JdbcOdbcDriver , 那么我问:如果我将数据库上传到JSP空间后,那不是不能用?因为还要在SERVE上建ODBC,
- 请哪位给我一些关于JSP+ORACLE的资料(电子文档)适用于初学者使用,谢谢!
- 为什么会出现 org.apache.jasper.JasperException: No data found
- 在java中是如何处理@^
{name="guest"+(int)(Math.random()*10000);
} if(title==null)
{title="无标题";
} if(messages==null)
{messages=" 无信息";
} 该成 name.equal("")试试。 如果request得到的属性为空,或是个空字符,输出的结果为“null”这个字符串。貌似是这样,我没有验证,你可以修改后验证下。
{name="guest"+(int)(Math.random()*10000);
} 改成if(name==null||name.equals(""))
{name="guest"+(int)(Math.random()*10000);
} 标题和信息也是同理
if(name==null || "".equals(name)){
// ...... 设置默认值
}