Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question on instance variables in servlets

 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 724
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instance variables are not thread safe.
Why you are creating these instance variables?
 
Wayan Suwastika
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic