• Post Reply Bookmark Topic Watch Topic
  • New Topic

why request methods cannot be resolved inside functions?  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
I did small function in jsp that resolved current directory
but its seams the function can't resolve request method and the compiler yelling error
why is that what do I miss here?

public String getScript(){
String jspPath = request.getServletPath();
String absoluteJspPath = application.getRealPath(jspPath);
int ilstInx =absoluteJspPath.lastIndexOf("\\");
String jspCurrDir = absoluteJspPath.substring(0,ilstInx);
ilstInx =jspCurrDir.lastIndexOf("\\");
jspCurrDir = jspCurrDir.substring(ilstInx);
return jspCurrDir;
}
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the UBB code tags when posting code.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to add more details to your posts for us to be able to help you. Please read this.

Saying "I get an error" without showing us the error is not helpful.

Also, where and how did you declare your function for the JSP?

These are details that help others help you.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok sorry here is what i have plain and simple :
<html>
<body>
<%!
public String getScript(){
String jspPath = request.getServletPath();
String absoluteJspPath = application.getRealPath(jspPath);
int ilstInx =absoluteJspPath.lastIndexOf("\\");
String jspCurrDir = absoluteJspPath.substring(0,ilstInx);
ilstInx =jspCurrDir.lastIndexOf("\\");
jspCurrDir = jspCurrDir.substring(ilstInx);
return jspCurrDir;
}
%>
</body>
<% out.println(getScript()) ;%>
</html>

what im geting is :
request cannt be resolved
and
application cannt be resolved

at lines :
String jspPath = request.getServletPath();
String absoluteJspPath = application.getRealPath(jspPath);

now the thing is when i make it as simple jsp without function like that :
<%
String jspPath = request.getServletPath();
String absoluteJspPath = application.getRealPath(jspPath);
int ilstInx =absoluteJspPath.lastIndexOf("\\");
String jspCurrDir = absoluteJspPath.substring(0,ilstInx);
ilstInx =jspCurrDir.lastIndexOf("\\");
jspCurrDir = jspCurrDir.substring(ilstInx);

out.println(jspCurrDir);
%>
its working fine

thanks for the help
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, please use the UBB code tags to preserve the formatting of your posted code.

Hint: you can edit your previous posts to add them.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand the difference between using <%! %> and using <% %>?
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following should work (but I haven't tested it):


The implicit variables in a JSP are not global variables.
They are only available to the "_jspService()" method.
<% code; %> gets added to the _jspService() method as is.
<%= expr %> gets added to the _jspService() method as an out.println(expr)
<%! declaration %> gets added to the class, outside of any method (just like any other java method in a class)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind:
getRealPath only works if your application has been deployed from an exploded file system. If deployed from a war archive it will return null.

Relying on getRealPath leads to non-portable code.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and thanks for the fast reply
i wander if i can build function that returns me the current directory
the jsp page is in .
the only down side i trying to find solution is that the function call can't get parameters (getScript), the reason is that im doing convert from some build in script lang to jsp and i like to keep functions call as they are.
is there other way to do it?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some key differences between JSP and web scripting languages.
Chief among them is the fact that a servlet gets compiled into a multi-threaded servlet. One instance of your JSP handles all of your requests.
For this reason a method defined in a JSP won't have any access to the request information unless the 'request' object is passed to it.

In other languages such as Perl and PHP, the entire script gets interpreted, fresh, with each request.

Also, the idea of a 'directory structure' is not as rigid in a Servlet/JSP app. Java webapps can be run from an exploded file system but they can also be run from within a packed war archive or from a remote machine. In these cases there is no direcotry structure. In this case, getRealPath will return null.

There are also url-mappings and forwards to consider.

What are you trying to do with the directory information?
Does it need to be specific to each JSP?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello and thanks for the fast reply
i will try to explain what im trying to get here ,
the other lang function "getScript" was basically return me the current directory from which the page wasrunning
so for example if i have something like this :
if all my pages was in the directory
/home/myplace/test/myPage.ol
if i write in the page :
<# getScript() #>/myPage.ol
the result returns me in html after server parsing
test/myPage.ol

now what im trying to do here is to keep the getScript() function and just to change to <%= %>
the jsps will be under \Tomcat 5.5\webapps\ROOT\ in this case in the test dir , but the dir name can be also test1 or foo2 ....

not war files or servlets or not any thing else simple jsps under ROOT .
i hope i made my self clear now

thanks allot
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:

i hope i made my self clear now

Your point was clear from the start.

My point is that is that, unlike in whatever scripting language you were using before, JSP containers create one instance of your JSP (generated into a servet) in memeory. That instance handles all requests for that JSP.
If you want to write your own methods in a JSP which are request aware, you will need to pass the request object to that method.

In your other post, you wondered why you couldn't call out.println("asdf") from within your method. Just like with the request object, you will need to pass the "out" object to the method if you want to call it from the method.

JSP is conceptually very different from most of the other web technologies.
You're probably not going to be able to port everything from the last version without learning about the underlying concepts and considering how JSP works in comparison with whatever language you were previously using.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!