那个是声明用的吧?
写语句应该用<%……%>

解决方案 »

  1.   

    对啊,我就是要声明几个方法,在<% %>中是不能声明方法的!另外纠正一下上面的错误:
    <%!
    String str = (String)session.getAttribute("str");
    %>
    类型转换不是主要问题,关键就是如何使用session?谢谢回答!
      

  2.   

    这个就不知道了,我一直都用<%……%>从来没用过<%!……%>,因为少一个叹号一样好用~~
      

  3.   

    <%! %>中声明的是全局变量和方法,那时候,session, request, response等变量还没有实例化,所以肯定用不了。
      

  4.   

    <%!public static HttpServletRequest request = null;
    public static HttpServletResponse response = null;
    public static JspWriter  out = null;public static void test() throws Exception {
        out.print("//");
    }%>
    <%this.request = request;
    this.response = response;
    this.out = out;this.test();
    %>
      

  5.   

    其实不用吧?我没试过,但你可以试试这样运行是否正常
    就是让你定义的方法接受所需要的参数<%!
    public void test(HttpServletRequest request,
            HttpServletResponse response,
            JspWriter out) throws Exception {    request.xxxx;
        out.xxxxxx;
    }%>
    <%
    test(reqeust,response.out);
    %>
      

  6.   

    传递变量到方法内部,我之前也是这么做的。
    后来仔细看了一下jsp文件编译成的servlet,发现似乎也只能这么做了。
    不过application和config可以直接用getServletContext()和getServletConfig()得到,这又是什么原因呢?
      

  7.   

    使用参数把request,session等东西传进<%!.....%>的方法里,这应该最合适的方法了