Divij Mahajan

Greenhorn
+ Follow
since Jul 26, 2004
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Divij Mahajan

In JSP 2.0, how do you use jspContext.getOut(). I am coding it as follows

Code

------------------------------------------------------------------------

public void doTag() throws JspException,IOException{

JspWriter out = jspContext.getOut();
...........
...........
------------------------------------------------------------------------

When trying to compile this file I get an error "jspContext has private access in javax.servlet.jsp.tag ext.SimpleTagSupport".

Am I sleeping (??) or is this really an issue. Any help in getting around this would be highly apprecated.
17 years ago
JSP
Hi William

You are right about handling "synchronization issues" by using local variables rather than global.

However, the point I tried to make was, a servlet implementing the SingleThreadModel interface, by default gaurantees than the instances are thread safe.

Alternatively explained.. a servlet implementing SingleThreadModel does not explicitly need to handle synchronization issues.

Please correct me if I am wrong.
17 years ago
Hey Kriti,

The de-facto implementation of RequestDesipatcher object defines the following two things

1. "A call to forward() may be used if *only* if no content has previously sent to the client.

2. "No further data can be sent to a client after tha call to forward has completed.

Assuming your rd.forward() worked succesfully, the request moves out of the current scope and moves on to the forwarded resource.

Why don't you try generating the error page from the resource that this call was forwarded to. (assumption : your architecture should be able to support this)


Cheers!!
17 years ago
Hey Chris

Two ways to use servlets to upload using MIME types are

- application/x-www-form-urlencoded
- multipart/form-data

The third is to use a FileUpload API. U can get one from http://jakarta.apache.org/commons/fileuploads.

To checkout a few examples of how to write a servlet class using these APIs, please refer the following books

1. OReilly's "Java Servlet Programming"
2. Servlets and JSPs - The JeEE Technology Web tier" by Jayson Falkner and Kevin Jones.

Let me know if you still have queries.


Cheers!!
17 years ago
Hi Malini,

An object is said to be ready for garbage collection whenever it is not holding any references. The JVM may then execute the Garabge Collector "at will" and remove such unreferenced objects from memory.

Another scenario is when a gc may need to free up memory for other objects. At this time it may remove objects in a given order (depends on JVM), say in order of last usage time or if the object is holding weak references.

Finally, a developer may want to call gc at a specific time and thats where one uses calls to methods of the GC or System class explicitly.(ref api)

-- A coding best practice related to GC is to call the finalize method, wherein references held by objects, that are no longer in use can be released. Finalise method is executed just before gc completes execution. But beware that this method may not be called everytime so try using alternatives.

Lemme know if you need more details.
17 years ago
Hey Gaurav


Can you post the part of your web.xml that contains the servlet and servlet-mapping elements. and also the path where you have your servlet stored.

Maybe one of these is the cause.
17 years ago
Hi Krishan

SingleThreadModel has very little in terms of benefits to offer, and thats in built synchronization, which like Smitha said can be achieved thru synchronized calls to the servlets..

However, there's a lot to loose, since servlets implementing this interface leave a large memory footprint and are are not effectively garbage collected. Also, when a servlet requires extra resources like a DB connection, it may open one connection per instance, which is not a good approach.

In essence, this model works nice for smaller applications but not for bigger ones.

-Divij
17 years ago
Hi Folks,

My understanding is that under **normal** circumstances the server creates only one instance of a servlet but uses multiple threads to service clients requests. i.e the init method is called only once and all subsequent client requests are passed to the service method.

On the other hand though, if you want to create an instance pool, you implement the SingleThreadModel interface which ensures that the server creates a new instance for each request. This is generally done to overcome the syncronization issues associated with threads.

Please correct me if I am wrong.

-Divij



I recently figured out something missing in my understanding of servlets.......

Does the servlet container create seperate instances of each servlet i.e...as the requests comein it(ServletContainer) starts creating objects of say a servlet called HelloWorldServlet i.e instances of HelloWorldServlet.

After processing a request it stacks these instances say in a pool. and services the clients when a similar request comes.-----IS IT
[OR]
There is only one servlet say HelloworldServlet and the server creates a new Thread that is listening to port 8080 (say) for every client that is
requesting the servlet.And passes the req and res object to the service method. And at anypoint of time there is only one servlet ..and its all inside Servlet's service method...isit.....
And when we say instance pooling we mean instances of several other servlets....one of each Servlet.

Guys give me links to some excellent resources....i even went thru the specs2.2..couldn't really figure out..
17 years ago