简体   繁体   中英

Why we can not access request from jsp declarations?

I am new to the JSP related technologies. I faced a strange situation: works find with scriplet

<%
    String username = request.getParameter("username");
    if ( username != null && username.length() > 0 ) {

    }
%>

But with declaration request object can not be found?

在此处输入图片说明

I read that when be define method or variable in declarations they are part of the servlet, from the servlet we can access request object. So why is this strange behavior?

JSP is converted into a servlet before compilation.

What you write inside a jsp scriptlet go inside the service() method of the servlet and everything inside jsp declaration go outside service method but inside servlet class.

if condition can only be used inside methods and request is a parameter of service method So it cannot be accessed outside service method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM