servlet

can we use constructor in servlet?
[41 byte] By [namita.pradhan08a] at [2007-11-26 16:12:10]
# 1
Actually ever heard of Google? http://www.google.com/search?hl=en&q=can%20we%20use%20constructor%20in%20servlet%3F
Tribioa at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 2
A servlet is just like an applet in the respect that it has an init() method that acts as a constructor, an initialization code you need to run should e place in the init(), since it get called when the servlet is first loaded
bangaru.reddya at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 3

> A servlet is just like an applet in the respect that

> it has an init() method that acts as a constructor,

> an initialization code you need to run should e place

> in the init(), since it get called when the servlet

> is first loaded

Not quite.... no no

Actually an HTTP servet is a just a normal class that extends the HttpServlet class. The extended class then has two methods used to process Http requests. Basically this is how a servet looks like.

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class NewServlet extends HttpServlet

{

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

processRequest(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

{

processRequest(request, response);

}

public String getServletInfo()

{

return "Short description";

}

}

MeTitus

Me_Titusa at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 4

> A servlet is just like an applet in the respect that

> it has an init() method that acts as a constructor,

> an initialization code you need to run should e place

> in the init(), since it get called when the servlet

> is first loaded

When a person doesn't know what it is saying its better to say nothing at all. Where did you take you degree anyway, if they teach you this kind of things then I surrelly want to take my masters there ;)

Don't deceive people.

MeTitus

Me_Titusa at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 5

> Actually ever heard of Google?

>

> http://www.google.com/search?hl=en&q=can%20we%20use%20

> constructor%20in%20servlet%3F

I can see you know how to use google, unfortunatelly not correctly.

If you were to use one class extending HttpServlet It would not make much sense...

Try this one

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/

MeTitus

Me_Titusa at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 6

> > A servlet is just like an applet in the respect

> that

> > it has an init() method that acts as a

> constructor,

> > an initialization code you need to run should e

> place

> > in the init(), since it get called when the

> servlet

> > is first loaded

>

> When a person doesn't know what it is saying its

> better to say nothing at all. Where did you take you

> degree anyway, if they teach you this kind of things

> then I surrelly want to take my masters there ;)

>

> Don't deceive people.

>

The OP didn't ask what a servlet was, but if the Ctor in servlet could be used.

Apparently, the OP was looking for some ways to write some initialization code. That initialization code is typically written in the init() method of a servlet which is called once by the servlet container. Applets have an init() method too that supplies a similar functionality, which is called once by the applet container.

Therefore the answer that you've been attacking (in a pretty disproportionate way, I might add) is valid.

karma-9a at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 7

> > Actually ever heard of Google?

> >

> >

> http://www.google.com/search?hl=en&q=can%20we%20use%20

>

> > constructor%20in%20servlet%3F

> I can see you know how to use google, unfortunatelly

> not correctly.

>

Another undeserved criticism. The Google search string supplies some pretty useful links. Why would that be an incorrect use of Google?

karma-9a at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 8
why you want to run constructor ? all arguments is passed by http request or http response
eaajea at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 9
I assumed he was speaking about an HttpServlet not a GenericServlet...Sorry,MeTitus
Me_Titusa at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...
# 10

> why you want to run constructor ?

>

> all arguments is passed by http request or http

> response

Maybe because it is sometimes a [url=http://www-128.ibm.com/developerworks/websphere/library/bestpractices/using_httpservlet_method.html]good practice[/url] to do some one-time initialization stuff.

It might also be that the OP is just curious, and does not need to initialize anything at all...

karma-9a at 2007-7-8 22:34:45 > top of Java-index,Java Essentials,New To Java...