• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access my request from my class

 
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Steve,

I am not using Sevlet, it is a simple class

Do you other idea?

Many thanks,
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Angus Ferguson
Ranch Hand
Posts: 1402
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
reply
    Bookmark Topic Watch Topic
  • New Topic