wood burning stoves 2.0*
The moose likes Servlets and the fly likes question on instance variables in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "question on instance variables in servlets" Watch "question on instance variables in servlets" New topic
Author

question on instance variables in servlets

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
hi friends my servlet code

public class LeaveServlet extends HttpServlet {
private String form_id;
private static final Logger log = Logger.getLogger(LeaveServlet.class);

private HttpServletRequest request = null;
private HttpServletResponse response = null;
List errMessageList = null; // new ArrayList();

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException,ServletException {

request = req;
response = resp;
form_id = request.getParameter(Constants.FORM_ID);

// and rest of code
}

public void dispatch (String page,String s1) throws IOException, ServletException {
String form_id = request.getParameter(Constants.FORM_ID);
request.setAttribute(s1,form_id);
RequestDispatcher rd=request.getRequestDispatcher("jsp/" + page);
rd.forward(request,response);
}


//other methods

}

friends i have created HttpServletRequest and HttpServletResponse object's
reference variables but creates objects in do post and other methods
does it affect my code and is it possible that i face threading problems
i think yes and if yes what step should i follow to avoid data corruption
David Ulicny
Ranch Hand

Joined: Aug 04, 2004
Posts: 724
Instance variables are not thread safe.
Why you are creating these instance variables?


SCJP<br />SCWCD <br />ICSD(286)<br />MCP 70-216
Wayan Suwastika
Greenhorn

Joined: May 25, 2005
Posts: 11
Yes, Instance variable on any Servlet class are considered to be not Thread save.

why don't you just pass that two variables through u'r method argument say :
public void dispatch (HttpServletRequest request, HttpServletResponse response, String page,String s1) throws IOException, ServletException {
//......
}

rgds
wyasa


SCJA, SCJP 1.4, SCWCD inprogress
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
Data that changes 'per user', ie, data for individual users(login name, default language etc) should not be stored in instance variables since, like mentioned above, this data is not thread safe.
This kind of data should always be declared/initialiased in the service()(or doGet()/DoPost() etc) methods.

Whereas generic data, or data common to all users, (Eg: list of holidays for the organisation) should be declared as instance variables since this is not required to be different for every user.
[ September 08, 2005: Message edited by: Neeraj Dheer ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question on instance variables in servlets