File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Need help! Request Headers! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need help! Request Headers!" Watch "Need help! Request Headers!" New topic

Need help! Request Headers!

Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Hi friends,
I want to add this part to my program where is the place that this part should go?
Many thanks,
this part:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
PrintWriter out = response.getWriter();
String agent = request.getHeader("user-agent");
String referer = request.getHeader("referer");
String Host = request.getHeader("Host");
out.println("user-agent" + " = " + agent);
out.println("referer" + " = " + referer);
out.println("Host" + " = " + Host);

Hartmut Ludwig
Ranch Hand

Joined: Aug 31, 2002
Posts: 51
Hi, Elahe,
seems like this is your first contact with servlet development.
I have no idea from which class the Servlet-Code you sent is derived (do you?). Seems like there is one import statement missing, cause AFAIK BaseServlet does not belong to any JDK. You have obviously copied the code from some book or webpage without knowing what exactly happens there.
I would recommend that you first familiarize yourself a little bit with servlet-development in general.
A good point to start is thisweb pageof the book "Core Servlets and JSPs" by Marty Hall.
If you take the time to read there a little bit, you will find out, that most HTTP-Servlets implement a doGet() and a doPost() method.
Those methods will be called, if the servlet receives a GET- or POST request.
I have no idea what the servlet code you posted is supposed to do, but if it would be a subclass of javax.servlet.http.HttpServlet you could place your code snipplet right behind the first /************/
Your code would then be executed anytime the servlet is called using the GET-Method. It would gather some information about the users browser and where the user came from in an HTML-Page and send this back to the user. Not very usefull, since the user knows all this stuff anyway.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

He's using Velocity as a presentation framework, which does have a BaseServlet to extend off of.

Your code snip is from a 'regular' servlet that performs its own output... but your velocity servlet merely does logic, and depends on the template for output.

So instead of placing those lines from your snippet into the velocity servlet, I'd use the velocity template to output those values. Something like: user-agent = $request.getHeader("user-agent")
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The other alternative is to retrieve them (almost anywhere would do, as long as it's not in a logic branch that is only travelled in some conditions).. and place them in the context.

ctx.put("user-agent", request.getHeader("user-agent") );

And then in velocity template:

user-agent = $user-agent
Elahe Shafie
Ranch Hand

Joined: Dec 12, 2001
Posts: 291
Thank you so much I got it Now it works...
I agree. Here's the link:
subject: Need help! Request Headers!
It's not a secret anymore!