2009-05-21 3 views
22

Sto utilizzando un javax.servlet.http.HttpServletRequest per implementare un'applicazione Web.Come impostare un parametro in un HttpServletRequest?

Non ho alcun problema a ottenere il parametro di una richiesta utilizzando il metodo getParameter. Tuttavia non so come impostare un parametro nella mia richiesta.

risposta

25

Non è possibile, non utilizzare l'API standard. HttpServletRequest rappresentano una richiesta ricevuta dal server e quindi l'aggiunta di nuovi parametri non è un'opzione valida (per quanto riguarda l'API).

In linea di principio è possibile implementare una sottoclasse di HttpServletRequestWrapper che avvolge la richiesta originale e intercetta i metodi getParameter() e inoltra la richiesta spostata all'avvio.

Se vuoi seguire questa strada, è necessario utilizzare un Filter per sostituire il vostro HttpServletRequest con un HttpServletRequestWrapper:

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    if (servletRequest instanceof HttpServletRequest) { 
     HttpServletRequest request = (HttpServletRequest) servletRequest; 
     // Check wether the current request needs to be able to support the body to be read multiple times 
     if (MULTI_READ_HTTP_METHODS.contains(request.getMethod())) { 
      // Override current HttpServletRequest with custom implementation 
      filterChain.doFilter(new HttpServletRequestWrapper(request), servletResponse); 
      return; 
     } 
    } 
    filterChain.doFilter(servletRequest, servletResponse); 
} 
+2

Presumo che hai questa implementazione da [qui] (http://stackoverflow.com/questions/1046721/accessing-the- raw-corpo-di-un-put-o post-richiesta). Il MULTI_READ_HTTP_METHODS era un po 'confuso, e anche se ora capisco quale scopo serve, ti chiedo di rimuoverlo se completamente. – Neil

15

Dalla tua domanda, penso che quello che si sta cercando di fare è quello di memorizzare qualcosa (un oggetto, una stringa ...) per inoltrarla quindi a un altro servlet, utilizzando RequestDispatcher(). Per fare questo non è necessario impostare un paramater ma un attributo utilizzando

void setAttribute(String name, Object o); 

e poi

Object getAttribute(String name); 
+5

Gli attributi e i parametri non sono intercambiabili e rappresentano concetti molto diversi. – skaffman

+2

@skaffman ma forse è in grado di risolvere ciò di cui ha bisogno utilizzando gli attributi - non dovrebbe essere necessario aggiungere un nuovo parametro a una richiesta nel mezzo dell'elaborazione della richiesta –

+2

la risposta deve essere modificata per riflettere che l'impostazione di un attributo potrebbe essere una soluzione al problema, invece di implicare l'impostazione, l'attributo imposta un parametro nella richiesta. – Chii

17

Se si vuole veramente fare questo, creare un HttpServletRequestWrapper.

public class AddableHttpRequest extends HttpServletRequestWrapper { 

    private HashMap params = new HashMap(); 

    public AddableingHttpRequest(HttpServletRequest request) { 
      super(request); 
    } 

    public String getParameter(String name) { 
      // if we added one, return that one 
      if (params.get(name) != null) { 
       return params.get(name); 
      } 
      // otherwise return what's in the original request 
      HttpServletRequest req = (HttpServletRequest) super.getRequest(); 
      return validate(name, req.getParameter(name)); 
    } 

    public void addParameter(String name, String value) { 
      params.put(name, value); 
    } 

} 
+3

cosa convalida il metodo? – jomaora

+0

@Jeff, puoi dire per favore che cosa significa questo metodo di validazione ?? –

+4

basta sostituirlo con - return req.getParameter (name); – Akvel

-4

Ci dispiace, ma perché non usare la seguente costruzione:

request.getParameterMap().put(parameterName, new String[] {parameterValue}); 
+4

Perché si ottiene questa eccezione: java.lang.IllegalStateException: Nessuna modifica è consentita su una ParameterMap bloccata – joaopribs