<%!
Vector v = new Vector();
int i = 1;
ServletContext application;
synchronized void sendMessage(String s) {
i ++;
v.add("No." + i + "," + s);
application.setAttribute("message", v);
}
%>
    <%
String name = request.getParameter("name");
String title = request.getParameter("title");
String messages = request.getParameter("message");
if(name == null) { 
name = "guest" + (int)(Math.random()*10000);
}
if(title == null) {
title = "无标题";
}
if(messages == null) {
messages = "无信息";
}
String s = "Name:" + name + "#" + "Title" + title + "#" + "Content:" + "<br>" + messages;
sendMessage(s);
out.println("您的信息已提交!");
%>请高手们帮忙解决一下

解决方案 »

  1.   

    首先,凡是涉及到错误的问题,请先把错误堆栈信息贴出来,免得大家还得猜测在哪里
    然后:你这定义是想干啥?
      ServletContext application;
    打算让它能变出东西来啊?
    其实application可以直接使用的,如果你别画蛇添足的去声明它的话
    如果打算写,就写在函数里面吧:
      ServletContext application = getServletContext();
      

  2.   

    确实啊,application是可以直接用的内置对象,你重新定义了,但没初始化,当然是空指针。
      

  3.   

    不好意思啊,我不太会玩这个,我也是一个新手,请见谅
    <%!
     Vector v = new Vector();
     int i = 1;
     ServletContext application;
     synchronized void sendMessage(String s) {
     i ++;
     v.add("No." + i + "," + s);
     application.setAttribute("message", v);//空指针报错
     }
     %>
       <%
     String name = request.getParameter("name");
     String title = request.getParameter("title");
     String messages = request.getParameter("message");
     if(name == null) {  
    name = "guest" + (int)(Math.random()*10000);
     }
     if(title == null) {
     title = "无标题";
     }
    if(messages == null) {
     messages = "无信息";
     }
    String s = "Name:" + name + "#" + "Title" + title + "#" + "Content:" + "<br>" + messages;
     sendMessage(s);
     out.println("您的信息已提交!");
     %>
      

  4.   

    楼主是没看我1楼的回复么?<%!
    Vector v = new Vector();
    int i = 1;
    // ServletContext application; // 这句话完全没必要,application是JSP内置对象
    synchronized void sendMessage(String s) {
       i ++;
       v.add("No." + i + "," + s);
       application.setAttribute("message", v);//空指针报错(你自己定义了一个没有初始化的application变量,当然空指针)
    }
    %>
      

  5.   

    如果打算写,就写在函数里面吧:
    ServletContext application = g……
    上面1楼应该是正解 你修改一下
      

  6.   

    你没有弄明白<%!%> <%%>的区别  一个是声明为全局变量 一个是声明为局部变量
    而jsp的9个内置对象的作用域范围是在service方法中声明的 所有方法外部没有办法访问到内置的对象