• Post Reply Bookmark Topic Watch Topic
  • New Topic

set request in init() method

 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello every one , i've a question about setting request "req.setAttribute("Key",Action); " in void init() method , and this last one loads first on startup of tomcat .

public class InitController extends HttpServlet {// first servlet that tomcat loads

public void init() throws ServletException {

//here i want to set the request __ but the problem it returns NullPointerException .




}

}
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no request at initialization time, so there's also no object on which to invoke setAttribute. What are you trying to accomplish?
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is there a solution ??
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on what the problem is that you're trying to solve.
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okey, i'm trying to load data in my welcome file "index.html" and it's necessary to set a request to retrive the data from !

example :

req.setAttribute("listAuthenticiy", ServiceFactory.getInstance().getAuthenticityInstane().listAll()); // this in my servlet

#set( $listAuthenticity = $request.getAttribute("listAuthenticity"))

 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

okey, i'm trying to load data in my welcome file "index.html"


Is it a static resource? Or is it served by a servlet?
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm loading from a database
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does this have to do with the servlet init() method?
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my welcome page index.jsp i try to load some data from database -with mvc pattern-> to connect to the database , so the problem is how to load those information in the start up of tomcat , i've a servlet that loads first
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. You could retrieve some pieces of information at servlet (or better still at context) init time and store them in the ServletContext. Later while serving requests, you can access that information as needed.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not following. If you want to load data at servlet startup time (like in the init() method), what do you need a request or request attribute for? Just get it from the DB, store it some appropriate in-memory data structure, and retrieve it at request time.

Even better would probably be a servlet context listener that does this, and stores it as a context attribute, but that's a design choice for you to make.

Edit: ... too late :-)
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no it doesn't work !
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What doesn't work? What did you try? And how did it not work?
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i didn't understand what you are saying would you please give an example ? thanks a lot
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, so you 're saying "it doesn't work" to something you don't understand?

There are 3 steps:

1) get whatever data you need from the DB in your servlet's init method
2) store it in some data appropriate data structure - a Map, or watever fits the data
3) retrieve that part of the data that you need at request time from the data structure in the doGet or doPost method

I sense that you're not very experienced when it comes to servlets, so I won't elaborate on the context listener approach; that can come later.

If the individual parts still don't make sense -after you have tried implementing them!- then come back here with a detailed description of what you tried, and what the result was.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!