File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Browser b = new Browser() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Browser b = new Browser()" Watch "Browser b = new Browser()" New topic
Author

Browser b = new Browser()

Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Hi all!
I have built a class that extends HttpServlet. I have implemented getBrowser() and setBrowser and so on. In this methods I try to locate the browser using, Os and versions. It's just that when I try to use it in my JSP it returns null. The init() method isn't called? How is that?

I have set a string In init and tries to return this string in getBrowser()

I'm pussled, tied and a bad javaprogrammer
[ November 06, 2003: Message edited by: Mike Curwen ]

SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5
Dear me .. where to start...
1. you are using instance variables like this.request in a servlet - thats a no-no because it is not safe handling multiple requests.
2. if you want a class to represent the browser type, why are you extending HttpServlet? A servlet is meant to live in a servlet engine environment.
3. Why not just a separate class with a constructor that takes the user-agent String, or a setUserAgent method.
Bill
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Thanks William!
I'm not very good at this, as you can see. What you mean is that I
should only access a servlet via "servlet/servletName". ( Tomcat ). Ok I get the drift.
When I post or GET data doesn't a servlet make an instance of itself? I mean. Why can I not use this?
You say make a class that takes user-agent as an argument. How do I get the user-agent not using javascript and not using servlets?
// Mathias
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The init() method isn't called? How is that?
Because you didn't call b.init()? Having *just* said that...
When I post or GET data doesn't a servlet make an instance of itself? I mean. Why can I not use this?

The servlet container is responsible for instantiating, initializing and destroying a servlet instance. The servlet container, therefore, is what calls the init() method *for you*. You should *not* be treating a servlet as just a "plain old java class". They are highly specialized classes that need a container to run properly.

If you are studying for the SCWCD, I *highly* recommend downloading a copy of the Servlet specification. It has a lot of really good information about servlets, their lifecycle, servlet containers, and the hows and whats of using servlets.

http://www.jcp.org/aboutJava/communityprocess/final/jsr053/
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
Thanks!
I will do that.
// Mathias
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Browser b = new Browser()