Ho scritto Front Controller Pattern e ho eseguito il test. In qualche modo request.getPathInfo() restituisce null quando deve restituire le informazioni sul percorso.Come mai request.getPathInfo() nel metodo di servizio restituisce null?
1. HTML che chiama servlet
<a href="tmp.do">Test link to invoke cool servlet</a>
2. Mappare la servlet in DD.
Tutto ciò che ha .DO estensione (ex tmp.do) invocherà il servlet "Redirector"
<!-- SERVLET (centralized entry point) -->
<servlet>
<servlet-name>RedirectHandler</servlet-name>
<servlet-class>com.masatosan.redirector.Redirector</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RedirectHandler</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
3. Il servlet che prende richiesta * .DO
public class Redirector extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//test - THIS RETURNS NULL!!!!
System.out.println(request.getPathInfo());
Action action = ActionFactory.getAction(request); //return action object based on request URL path
String view = action.execute(request, response); //action returns String (filename)
if(view.equals(request.getPathInfo().substring(1))) {
request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);
}
else {
response.sendRedirect(view);
}
}
catch(Exception e) {
throw new ServletException("Failed in service layer (ActionFactory)", e);
}
}
}//end class
Il il problema è che request.getPathInfo() restituisce null. Basato sul libro Head First,
il servlet del ciclo di vita si sposta da
"does not exist"
stato a"initialized"
Stato (che significa pronto a servizio richiesta del cliente) a partire con il suo costruttore. Init() viene sempre completato prima della prima chiamata a service().
Questo mi dice che da qualche parte tra il costruttore e il metodo init(), il servlet non è completamente sviluppato.
Quindi, questo significa che, quando viene chiamato il metodo time service(), il servlet deve essere un servlet sviluppato completamente e il metodo di richiesta dovrebbe essere in grado di chiamare getPathInfo() e si aspetta che il valore valido restituisca invece di null.
UDPATE
Molto interessante. (http://forums.sun.com/thread.jspa?threadID=657991)
(HttpServletRequest - getPathInfo())
Se l'URL è come qui di seguito:
http://www.myserver.com/mycontext/myservlet/hello/test?paramName=value.
Se web.xml potrei solo il modello servlet come/myContext/* getPathInfo() restituirà myServlet/ciao/test e getQueryString() restituirà paramName = valore
(HttpServletRequest - getServletPath())
Se l'URL è come di seguito:
http://hostname.com:80/mywebapp/servlet/MyServlet/a/b;c=123?d=789
String = servletPath req.getServletPath();
Esso restituisce "/ servlet/MyServlet"
Questa pagina è anche molto buona: http://www.exampledepot.com/egs/javax.servlet/GetReqUrl.html
Avrei dovuto guardare il documento, non sapevo che getServletPath() esiste! –
Gottya così in DD, se si specifica/foo/*. Do url-pattern>, getPathInfo() otterrebbe solo il percorso DOPO il ".do"
Ciò significa, /foo/test.do/blah?name = myname allora restituisce "/ blah? name = myname" –
'/ foo/*. do' non è un pattern url valido, quindi non sarà in grado di restituire nulla :) Anche allora, in teoria, darebbe solo '/ blah'. Non include la stringa di query. Per questo hai il metodo 'getQueryString()' (o solo i soliti metodi 'getParameter()'). – BalusC