File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Access my request from my class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Access my request from my class" Watch "Access my request from my class" New topic
Author

Access my request from my class

Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 419
Hi,

I am building an app, which has a .jsp, and a class. I have no servlets. Then I am interested in access from my class to the request of the page. Can I access my request without using servlets? What way?

I mean where is the request with the parameters from the URL?

Many Thanks,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Requests only exist from the moment a client asks for a resource until the moment your web application sends back the response to the request. Your class should only access the request during this period, and the usual way of achieving this is to call your class from a JSP (or servlet, but you don't have any). You should definitely not keep a reference to the request somewhere else, as the object may be reused for a different request at some later time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 419
Hi Rob,

I going to tell you about the case in particular. When I run my app my .jsp calls my class, and my class sends a link for authorization to my .jsp. Then the user click in the link and grant access to his Google account.

Then in the URL, imagine www.rob.com a couple of variables are appended, well I need retrieve that couple of parameters from my class. Do you have any idea about what way do it, please?

Many Thanks,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Well, no, that isn't how you do it. Of course when you carelessly say that the JSP "calls your class" you divert your attention from the actual solution. What actually happens that there's an object of your class, and you call a method of that object. (Or perhaps you call a static method of the class.) So now that you have the JSP calling a method, it should be obvious that you can pass parameters to that method. In particular you can pass the request, or perhaps those request parameters, to that method. That's how you do it.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 419
Hi,

thanks for reply.

Yes I am trying to apply your advice. But I have another doubt I need retrieve the value of my parameters from my URL.

Also in order to use the class I need import:


<%@ page import="/../../../../../src/package/Class" %>, but it is not working the error is that I must delete invalid tokens.

My folder structure in this way:


I have the structure of packages, web-->folder1-->folder2-->folder3-->myFile.jsp and also

the structure src-->package1-->File.java

This my script in my jsp, obviously it doesn't find the class

<%



String url = (request.getRequestURL()).toString();

FetchingAuthToken f=new FetchingAuthToken();



%>


Any idea please?

Many Thanks,
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Angus Ferguson wrote:Hi
<%@ page import="/../../../../../src/package/Class" %>, but it is not working the error is that I must delete invalid tokens.

You don't give directory path upto the class to import it.. Rather you have to give it's fully qualified name (like: java.util.Date)
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Angus Ferguson wrote:
Yes I am trying to apply your advice. But I have another doubt I need retrieve the value of my parameters from my URL.

Also in order to use the class I need import:

<%@ page import="/../../../../../src/package/Class" %>, but it is not working the error is that I must delete invalid tokens.

My folder structure in this way:
I have the structure of packages, web-->folder1-->folder2-->folder3-->myFile.jsp and also
the structure src-->package1-->File.java

This my script in my jsp, obviously it doesn't find the class
<%
String url = (request.getRequestURL()).toString();
FetchingAuthToken f=new FetchingAuthToken();
%>
Any idea please?

Many Thanks,


Please try to be more specific about the errors you are getting.. And the class you are accessing..
It would be better if you place a chunk of code that is related with this part of your error.. (Both JSP and Java Class)

And yes see UseCodeTags
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Angus Ferguson wrote:Hi,


A better way to send some parameters with the request to the java class is by setting the attribute in the request object itself..
As soon as you get the request in the java class, you can get that attribute from the request.
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 419
Hi,

thanks for reply.

I have done all you have told my and it works better. But when I run my app nothing happens. I mean, in my class with name FetchingAuthToken(), I have created a method with name testRequest(HttpServletRequest request), it looks like this:



Also in my *.jsp I ahve wrote like this:



Well when I run my app I expect that a trace "System.out.println(request.getRequestURL()+"htpServletRequesthtpServletRequest");" which is in the java method will be printed in the console, but it doesn't happens. Any ideas, please?


Many Thanks,

Regards,

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

In many Servlet Containers, the System.out stream gets redirected to a file. See if you can find an output log file somewhere in your application or server paths.


Steve
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 419
Hi Steve,

I am not using Sevlet, it is a simple class

Do you other idea?

Many thanks,
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Angus Ferguson wrote:Hi Steve,

I am not using Sevlet, it is a simple class

Do you other idea?

Many thanks,


Are you using JSP like you previously said? If so, then you are using Servlets - because JSPs get compiled into Servlets. Web Servers which run Servlets are often referred to as 'Servlet Containers' because they contain servlets (and to distinguish them from the more thorough JavaEE implementations which also include EJBs and other similar technologies).

So
1) you are using Servlets, even if you write JSPs
2) even though your code is in your own class, your class is running in a JVM created for the Servlet Container
3) the JVM, controlled by the Servlet Container, most likely has its System.out redirected to a text file somewhere
Isaac Ferguson
Ranch Hand

Joined: Jun 22, 2012
Posts: 419
Hi Steve,

many thanks for the explanation, now it is clearer for me. I am investigating but I don't find the output file, could be another reason? Else where that file could be located?

Many Thanks,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access my request from my class