Toon posts:

[Servlet/JSP] setAttributeName werkt niet in init()?

Pagina: 1
Acties:

Verwijderd

Topicstarter
Hallo allemaal, ik heb de volgende Servlet:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package nl.hince.xxx;
 
import java.io.IOException;
 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
 
import nl.hince.xxx.DbBean;
 
public class Servlet extends HttpServlet {
    
    public void init(ServletConfig config) throws ServletException {
        System.out.println("initializing controller servlet.");
        
        ServletContext context = config.getServletContext();
        
        DbBean dbBean = new DbBean();
        dbBean.setDbUrl(config.getInitParameter("dbUrl"));
        dbBean.setDbUserName(config.getInitParameter("dbUserName"));
        dbBean.setDbPassword(config.getInitParameter("dbPassword"));
        
        context.setAttribute("base",config.getInitParameter("base"));
        context.setAttribute("imageURL",config.getInitParameter("imageUrl"));
        context.setAttribute("dbBean",dbBean);
        
        try {
            Class.forName(config.getInitParameter("jdbcDriver"));
        }
        catch(ClassNotFoundException e) {
            System.out.println(e.toString());
        }
        super.init(config);
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String base="/";
        String url=base+"index.jsp";
        String action=request.getParameter("action");
        
        if(action!=null) {
            if(action.equals("login")) {
                url=base+"login.jsp";
            }
        }
        RequestDispatcher requestDispatcher = getServletContext().getRequestDispatcher(url);
        requestDispatcher.forward(request,response);
    }
}

Ik bouw dit met de volgende web.xml:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns./j2ee
  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4">
 
  <description>developerWorks beginning tomcat tutorial</description>
  <display-name>XXX</display-name>
 
  <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>nl.hince.xxx.Servlet</servlet-class>
    <init-param>
      <param-name>base</param-name>
      <param-value>http://localhost/xxx</param-value>
    </init-param>
    <init-param>
      <param-name>jdbcDriver</param-name>
      <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
    </init-param>
    <init-param>
      <param-name>imageURL</param-name>
      <param-value>http://localhost/xxx/images</param-value>
    </init-param>
    <init-param>
      <param-name>dbUrl</param-name>
      <param-value>jdbc:odbc:xxx</param-value>
    </init-param>
    <init-param>
      <param-name>dbUserName</param-name>
      <param-value>xxx</param-value>
    </init-param>
    <init-param>
      <param-name>dbPassword</param-name>
      <param-value>xxx</param-value>
    </init-param>
  </servlet>
</web-app>

Het probleem is dat de context.setAttribute("base",config.getInitParameter("base")); en de context.setAttribute("imageURL",config.getInitParameter("imageUrl")); het niet lijken te doen. Als ik het volgende op een jsp pagina doe
code:
1
2
3
4
5
6
7
<%
for (java.util.Enumeration e = application.getAttributeNames();
      e.hasMoreElements();) {
   Object key = e.nextElement();
   out.print(application.getAttribute(key.toString()));
}
%>

dan zijn base and imageUrl null, maar dbBean bestaat wel! Iemand enig idee? Ben er al uren mee aan het stoeien, maar kan het niet vinden! Thx ;)

[ Voor 11% gewijzigd door Verwijderd op 28-03-2006 22:43 ]


Verwijderd

Topicstarter
Sorry, topictitel is natuurlijk fout, moet zijn "[Servlet/JSP] setAttribute werkt niet in init()?"

Verwijderd

Topicstarter
Heb ook al geprobeerd:
code:
1
2
3
4
String rBase = config.getInitParameter("base");
String iURL = config.getInitParameter("imageUrl");
context.setAttribute("base", rBase);
context.setAttribute("imageURL",iURL);

maar dit helpt niks. Hoe zie ik of de code uberhaupt bereikt wordt? Waar gaat de System.out.println() heen? Ik had aangenomen dat hij er wel kwam, omdat dbBean bestaat in de application, maar ik begin meer en meer te twijfelen... Als ik context.setAttribute("pietjePuk","lalalala"); toevoeg aan de init(), ik recompile en tomcat opnieuw start, verschijntie ook niet in de lijst...

[ Voor 20% gewijzigd door Verwijderd op 29-03-2006 08:27 ]


  • matthijsln
  • Registratie: Augustus 2002
  • Laatst online: 23-02 21:07
super.init() moet je als eerste aanroepen in init(), niet als laatste.

Verwijderd

Topicstarter
Hm, heb het gevonden!
code:
1
<load-on-startup>0</load-on-startup>

toegevoged aan mijn <servlet></servlet> in web.xml. Nu werkt het! :) Thx anyway.