• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Browser b = new Browser()

 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!
I will do that.
// Mathias
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic