如何使用“请求" jsp函数中的对象 [英] how to use "request" object within a function in jsp

查看:43
本文介绍了如何使用“请求" jsp函数中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    <%
    String fname=request.getParameter("fname");
    String username=getVal("lname");
%>
<%!
    private String getVal(String param){
        return request.getParameter("fname");

}
%>
/*

--err



org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP

PWC6197: An error occurred at line: 5 in the jsp file: /register.jsp
PWC6199: Generated servlet error:
string:///register_jsp.java:12: cannot find symbol
symbol  : variable request
location: class org.apache.jsp.register_jsp
/*

推荐答案

JSP通过JSP编译器,该编译器会将JSP页面转换为servlet,从而自动生成Java代码.

JSP goes through a JSP compiler which will convert the JSP page into a servlet, autogenerating the java code.

JSP指令指示JSP编译器在哪里放置内容. <% %>内部的所有内容(称为 JSP scriptlets )都将放入生成的servlet的service()方法中. <%! %>中的所有内容(称为 JSP声明)都将成为生成的servlet实际类的一部分,因此您的getVal()将成为实例方法.

The JSP directives instructs the JSP compiler where to put what. Everything that is inside <% %> (called JSP scriptlets) will be put inside the service() method of the generated servlet. Everything inside <%! %> (called JSP declarations) will become part of the actual class of the generated servlet, so your getVal() will become an instance method.

service()方法中定义了标准的request(以及sessionpageContext等)对象实例,因此实际上它们仅在JSP scriptlet部分中可用.

The standard request (and session and pageContext etc) object instances are defined inside the service() method so they are, in effect, ONLY available inside JSP scriptlet sections.

例如,如果您在Tomcat上运行,那么如果您在Tomcat安装中的工作"目录中查看,则可以查看JSP页面的实际生成的Java代码.可能会很有趣,即使无法更好地了解幕后"情况.

If you are running on Tomcat, for instance, you can look at the actual generated Java code for your JSP pages if you look inside the "work" directory in the Tomcat installation. Might be interesting, if not get a better picture about what is happening "under the hood".

这篇关于如何使用“请求" jsp函数中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆