my dog learned polymorphism*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Need help! Request Headers!" Watch "Need help! Request Headers!" New topic
Author

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,
Elahe
this part:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
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);
}



Thanks,
Elahe
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.
sl
Hartmut
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

elahe,
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...
 
wood burning stoves
 
subject: Need help! Request Headers!
 
Similar Threads
Can not solve my problem....
How can I show part of my text in my template?
Question on cache control...
check for type of input......
How can I solve this problem?