Pegar HttpServletRequest

80 views
Skip to first unread message

Manoel Calixto

unread,
Apr 21, 2015, 11:04:13 AM4/21/15
to caelum-...@googlegroups.com
Bom dia pessoal!

Estou querendo pegar o objeto do HttpServletRequest, mas toda vez está vindo nulo. Segue o meu codigo abaixo, está faltando alguma coisa?

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

@RequestScoped
public class RequestUtil {
   
    private HttpServletRequest httpRequest;
   
    public RequestUtil() {
        // TODO Auto-generated constructor stub
    }
   
    @Inject
    public RequestUtil(HttpServletRequest httpRequest) {
        this.httpRequest = httpRequest;
    }
   

   
    public String getSubdominio(){
        String subdominio = httpRequest.getServerName().split("\\.")[0];
       
        return subdominio;
    }

}

--

Atenciosamente,

Manoel Calixto

Programador

Grupo 3corações
www.3coracoes.com.br

Manoel Calixto

unread,
Apr 21, 2015, 11:04:13 AM4/21/15
to caelum-...@googlegroups.com
Desculpe a insistência, alguém tem alguma ideia de como posso pegar os dados da requisição?

William Seiti Mizuta

unread,
Apr 21, 2015, 12:07:17 PM4/21/15
to caelum-...@googlegroups.com
Onde você está usando esta classe RequestUtil? Num controller?


William Seiti Mizuta 
Engenheiro de Software Elo7

elo7.com.br


--
Você recebeu essa mensagem porque está inscrito no grupo "caelum-vraptor" dos Grupos do Google.
Para cancelar inscrição nesse grupo e parar de receber e-mails dele, envie um e-mail para caelum-vrapto...@googlegroups.com.
Para postar nesse grupo, envie um e-mail para caelum-...@googlegroups.com.
Acesse esse grupo em http://groups.google.com/group/caelum-vraptor.
Para mais opções, acesse https://groups.google.com/d/optout.

Valério

unread,
Apr 21, 2015, 12:22:20 PM4/21/15
to caelum-...@googlegroups.com
Manoel, voce está injetando o RequestUtil em algum lugar? Ou está usando o construtor manualmente? Se você não requisitar essa classe ao CDI via @Inject o CDI não vai gerenciar esse bean e muito menos injetar o HttpServletRequest.

Att,

Valério

Manoel Calixto

unread,
Apr 21, 2015, 7:53:43 PM4/21/15
to caelum-...@googlegroups.com
Oi,

Estou injetando via CDI em uma classe normal, mas está vindo nulo.

Rodrigo Turini

unread,
Apr 21, 2015, 8:09:09 PM4/21/15
to caelum-...@googlegroups.com
O restante todo funciona? Digo, você tem um controller que recebe dependências
injetadas e está funcionando direitinho? Tenta injetar a classe MutableRequest do
VRaptor no lugar do HttpServletRequest pra ver se vai funcionar? Tem algum log?  

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br

Manoel Calixto

unread,
Apr 21, 2015, 8:16:37 PM4/21/15
to caelum-...@googlegroups.com
Sim funciona, inclusive tentei pegar o request diretamente em um @Controller, mas não injetava, então coloquei um @RequestScoped e começou a injetar o request, o comportamento é esse mesmo?

Vou tentar aqui com o MutableReques.

Obrigado!

Rodrigo Turini

unread,
Apr 21, 2015, 8:20:10 PM4/21/15
to caelum-...@googlegroups.com
perdão, não entendi uma coisa, você colocou o @RequestScoped onde? no controller? 

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br


Manoel Calixto

unread,
Apr 21, 2015, 8:21:43 PM4/21/15
to caelum-...@googlegroups.com
Isso no controller.

Rodrigo Turini

unread,
Apr 21, 2015, 8:43:31 PM4/21/15
to caelum-...@googlegroups.com
Por padrão todo @Controller já é um @RequestScoped, então adicionar o escopo 
explicitamente não deveria fazer diferença. Veja se com o MultableRequest voc6e
vai ter o mesmo comportamento. O CDI dá alguma exception? Confere no log. Se 
não funcionar, manda um trecho do código onde você está injetando essa classe? 

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br


Manoel Calixto

unread,
Apr 21, 2015, 9:18:16 PM4/21/15
to caelum-...@googlegroups.com
Rodrigo,

Rollback no que eu disse do controller, aqui deu certo eu tirando o @RequestScoped, não sei porque não deu certo em momento.
Fiz o teste com MultableReques, e continua nulo, não vem nenhum erro no log da CDI, segue o codigo da injeção:

@Inject
    RequestUtil request;

    @Override
    public String resolveCurrentTenantIdentifier() {
        return request.getSubdominio();
    }

Rodrigo Turini

unread,
Apr 21, 2015, 9:27:30 PM4/21/15
to caelum-...@googlegroups.com
Então você consegue injetar o HttpServletRequest no controller, mas no RequestUtil não?
Se for isso, você está com log4j configurado? Rola adicionar CDI como debug e mandar o
log do startup dessa sua app? Ele provavelmente vai alarmar algo de errado com esse cara.

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br


Manoel Calixto

unread,
Apr 21, 2015, 9:55:08 PM4/21/15
to caelum-...@googlegroups.com
Como coloco a CDI em debug? Pesquisei aqui e não achei nada sobre isso, pois aqui não entra de jeito nenhum dentro do método do RequestUtil.

Rodrigo Turini

unread,
Apr 21, 2015, 9:58:00 PM4/21/15
to caelum-...@googlegroups.com
dentro do seu log4j.xml você adiciona:

<category name="org.jboss.weld">
<priority value="DEBUG" />
<appender-ref ref="stdout" />
</category>  

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br


Manoel Calixto

unread,
Apr 21, 2015, 10:33:39 PM4/21/15
to caelum-...@googlegroups.com
Segue:

23:30:55,315 INFO  [org.jboss.modules] (main) JBoss Modules version 1.3.3.Final
23:30:56,024 INFO  [org.jboss.msc] (main) JBoss MSC version 1.2.2.Final
23:30:56,344 INFO  [org.jboss.as] (MSC service thread 1-1) JBAS015899: WildFly 8.2.0.Final "Tweek" starting
23:31:01,410 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) JBAS015003: Found Elas_Belas_Front.war in deployment directory. To trigger deployment create a file called Elas_Belas_Front.war.dodeploy
23:31:01,456 INFO  [org.jboss.as.server] (Controller Boot Thread) JBAS015888: Creating http management service using socket-binding (management-http)
23:31:01,509 INFO  [org.xnio] (MSC service thread 1-2) XNIO version 3.3.0.Final
23:31:01,527 INFO  [org.xnio.nio] (MSC service thread 1-2) XNIO NIO Implementation Version 3.3.0.Final
23:31:01,688 INFO  [org.wildfly.extension.io] (ServerService Thread Pool -- 31) WFLYIO001: Worker 'default' has auto-configured to 4 core threads with 32 task threads based on your 2 available processors
23:31:01,718 INFO  [org.jboss.as.clustering.infinispan] (ServerService Thread Pool -- 32) JBAS010280: Activating Infinispan subsystem.
23:31:01,833 INFO  [org.jboss.as.webservices] (ServerService Thread Pool -- 48) JBAS015537: Activating WebServices Extension
23:31:01,733 INFO  [org.jboss.as.jsf] (ServerService Thread Pool -- 38) JBAS012615: Activated the following JSF Implementations: [main]
23:31:02,080 INFO  [org.jboss.as.naming] (ServerService Thread Pool -- 40) JBAS011800: Activating Naming Subsystem
23:31:01,747 WARN  [org.jboss.as.txn] (ServerService Thread Pool -- 46) JBAS010153: Node identifier property is set to the default value. Please make sure it is unique.
23:31:02,109 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 47) JBAS017502: Undertow 1.1.0.Final starting
23:31:01,884 INFO  [org.jboss.as.security] (ServerService Thread Pool -- 45) JBAS013171: Activating Security Subsystem
23:31:02,412 INFO  [org.jboss.as.connector.subsystems.datasources] (ServerService Thread Pool -- 27) JBAS010403: Deploying JDBC-compliant driver class org.h2.Driver (version 1.3)
23:31:02,508 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-4) JBAS017502: Undertow 1.1.0.Final starting
23:31:02,659 INFO  [org.jboss.as.security] (MSC service thread 1-4) JBAS013170: Current PicketBox version=4.0.21.Final
23:31:02,805 INFO  [org.jboss.as.naming] (MSC service thread 1-4) JBAS011802: Starting Naming Service
23:31:02,879 INFO  [org.wildfly.extension.undertow] (ServerService Thread Pool -- 47) JBAS017527: Creating file handler for path C:\Users\Calixto\wildfly-8.2.0.Final/welcome-content
23:31:03,220 INFO  [org.jboss.as.mail.extension] (MSC service thread 1-4) JBAS015400: Bound mail session [java:jboss/mail/Default]
23:31:04,318 INFO  [org.jboss.remoting] (MSC service thread 1-2) JBoss Remoting version 4.0.6.Final
23:31:04,494 INFO  [org.jboss.as.connector.logging] (MSC service thread 1-4) JBAS010408: Starting JCA Subsystem (IronJacamar 1.1.9.Final)
23:31:04,597 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-4) JBAS010417: Started Driver service with driver-name = h2
23:31:04,614 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017525: Started server default-server.
23:31:04,627 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "Elas_Belas_Front.war" (runtime-name: "Elas_Belas_Front.war")
23:31:04,640 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-2) JBAS015012: Started FileSystemDeploymentService for directory C:\Users\Calixto\wildfly-8.2.0.Final\standalone\deployments
23:31:04,647 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-2) JBAS017531: Host default-host starting
23:31:08,199 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016002: Processing weld deployment Elas_Belas_Front.war
23:31:08,363 INFO  [org.hibernate.validator.internal.util.Version] (MSC service thread 1-2) HV000001: Hibernate Validator 5.1.3.Final
23:31:08,517 INFO  [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named DAO in deployment unit deployment "Elas_Belas_Front.war" are as follows:

    java:global/Elas_Belas_Front/DAO!br.elasbelas.dao.DAOBase
    java:app/Elas_Belas_Front/DAO!br.elasbelas.dao.DAOBase
    java:module/DAO!br.elasbelas.dao.DAOBase
    java:global/Elas_Belas_Front/DAO
    java:app/Elas_Belas_Front/DAO
    java:module/DAO

23:31:09,833 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) JBAS017519: Undertow HTTP listener default listening on localhost/127.0.0.1:8080
23:31:11,366 INFO  [org.jboss.ws.common.management] (MSC service thread 1-1) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.3.2.Final
23:31:11,692 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) JBAS010404: Deploying non-JDBC-compliant driver class org.postgresql.Driver (version 9.4)
23:31:11,695 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016005: Starting Services for CDI deployment: Elas_Belas_Front.war
23:31:11,711 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
23:31:11,763 INFO  [org.jboss.weld.Version] (MSC service thread 1-1) WELD-000900: 2.2.6 (Final)
23:31:11,854 INFO  [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-3) JBAS010417: Started Driver service with driver-name = Elas_Belas_Front.war_org.postgresql.Driver_9_4
23:31:11,889 INFO  [org.jboss.weld.deployer] (MSC service thread 1-3) JBAS016008: Starting weld service for deployment Elas_Belas_Front.war
23:31:19,999 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) JBAS017534: Registered web context: /
23:31:20,169 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 28) JBAS018559: Deployed "Elas_Belas_Front.war" (runtime-name : "Elas_Belas_Front.war")
23:31:20,350 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9991/management
23:31:20,350 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9991
23:31:20,351 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.2.0.Final "Tweek" started in 25728ms - Started 676 of 729 services (92 services are lazy, passive or on-demand)

Rodrigo Turini

unread,
Apr 21, 2015, 11:19:28 PM4/21/15
to caelum-...@googlegroups.com
o weld está como DEBUG? ele não está mostrando o log de definição dos beans

Rodrigo Turini
Caelum | Ensino e Inovação
www.caelum.com.br


William Seiti Mizuta

unread,
Apr 22, 2015, 1:18:08 AM4/22/15
to caelum-...@googlegroups.com
Esta classe onde você está injetando o RequestUtil, como ela é utilizada? Você também a injeta ou instancia na mão?


William Seiti Mizuta 
Engenheiro de Software Elo7

elo7.com.br


Reply all
Reply to author
Forward
0 new messages