aspose file tools*
The moose likes Servlets and the fly likes getRequestURL problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getRequestURL problem" Watch "getRequestURL problem" New topic
Author

getRequestURL problem

Richard Olson
Greenhorn

Joined: Mar 15, 2004
Posts: 15
Hello,

Somewhat newbie to servlets looking for some help.

If I do the following URL url = new URL("http://www.cnn.com"); I can pass the URL to my program and do whatever.

However, if I do the the following code snippet:
StringBuffer myURL = request.getRequestURL();
writer.println("myURL = " + myURL);
String theURL = myURL.toString();
writer.println("<p>");
writer.println("theURL = " + theURL);
try
{
// Set up the streams
URL url = new URL(theURL); // Create the URL
in = url.openStream();
Tomcat hangs.

My question, why when I pass a string to the server everything works, but when I call getRequestURL, change the output to a string I hang Tomcat?

Thanks,
Rich
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
Hi,

The problem is not in getRequestURL(), but in the logic. If you call getRequestURL() in a servlet and try to open a stream on this URL, it tries to open the URL first. And because the URL ultimately ends up in this very servlet, it places a new request to the same servlet, that starts in a new thread.

This process happens 'again and again' and so essentially you are creating multiple requests that never end. One request creates another that creates another and so on. Tomcat accepts these requests till all threads in the ThreadPool are used and then it hangs.

To understand this, put an instance variable, say, "int i" in the servlet and print it out within doGet() and increment it. Call the servlet. You can see that the variable is incremented and after about 150 (the default thread pool size), the server hangs.

SOLUTION: It seems like wat u want is an InputStream to the request. Why don't you directly call getInputStream() on the request? That is the simplest straight forward way.


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Richard Olson
Greenhorn

Joined: Mar 15, 2004
Posts: 15
Thanks, Sravan, I'll implement the suggestions.
Richard Olson
Greenhorn

Joined: Mar 15, 2004
Posts: 15
Hi,

I tried Sravan's suggestion. I've made progress by not hanging Tomcat.

However, no output. Basically, I'm just using the following URL to test the servlet - http:server:host/servlets-examples/servlet/GetURLServlet, but never see any output in my file (see code). I checked for contentLength, but nothing.

Any suggestions?

Thanks,
Rich

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;


public class GetURLServlet extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
ResourceBundle rb =
ResourceBundle.getBundle("LocalStrings",request.getLocale());
response.setContentType("text/html");

PrintWriter writer = response.getWriter();
try
{
// get a char input stream for the URL

BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream()));
// create stream for output file
PrintWriter out = new PrintWriter(
new BufferedWriter(
new FileWriter(
new File("c:/temp/httpoutput.txt"))));

// read the URL write to a file
String buffer;
writer.println("<h2>Before while</h2>");
while((buffer = in.readLine()) != null)
{
out.write(buffer);
}

in.close();
out.close();
}
catch(MalformedURLException e)
{
System.out.println("Failed to create URL:\n" + e);
}
catch(IOException e)
{
System.out.println("File error:\n" + e);
} // end catch
}// end try
}// end method doGet
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
Hi Rich,

Please note a few things.

1. When you try to get the request's InputStream and print out the contents you read from it, you do not get the URL. You get the content of the request packet i.e. the body.

2. In all the following cases, the default HTTP method is GET:
i. Form default - if you do not specify method attribute as POST
ii. Link
iii. Fetching a URL using the Address Bar
In all these cases, the body of the request is essentially empty. Only if the HTTP method is POST, the input parameters are passed in the body.

In your example, you use the URL, alright, but you are triggering a GET request and that is the reason nothing gets printed in the file. The BODY content of your request is EMPTY.

SOLUTION:
1. Change the Servlet's method from doGet() to doPost() and write a form that will submit a POST request to this servlet. Then you will find the contents in the file.

2. If you want the URL to be printed, you do not even require an InputStream. Just call getRequestURL() on the request that returns a StringBuffer and print this to the file.
Richard Olson
Greenhorn

Joined: Mar 15, 2004
Posts: 15
Thanks for the response, I've been busy on other issues
 
 
subject: getRequestURL problem