Năo funcionou,
ele continua criando uma nova sessao sempre que eu utilizo o requestDispatcher para
passar
o contexto do servlet para a página JSP.
eu verifiqeui na lista tomcat-users que outras pessoas tem o mesmo problema, e so
conseguiram resolber utilizando o sendRedirect do response, mas ainda nao sei como
utilizá-lo.
sera que nao pode-se resolver isso na configuracao do tomcat?
de qualquer forma eu encontrei no projeto Jakarta um framework para a utilizacao de um
servlet como contrle da aplicacao e as págians JSP apenas como apresentacao...
é seimilar ao que eu estou tentando, a partir do exemplo do livro " Desenvolvendo na
web
com javaserver pages".
alguem sabe algo a respeito de como utilizar sessoes com o RequestDispatcher?
tb acho que seria interesasnte para todos discutirmos esse Framework:
jakarta.apache.org/struts
[]´s
Daniel A.
Sven van ´t Veer wrote:
> >
> >
> > eis o codigo relevante da minha index.jsp:
> >
> > ------------------
> > <%
> >
>
> session = request.getSession(false);
>
> >
> > System.out.println("em Index.jsp - sessao = " + session.getId());
>
> if (session == null){
>
> >
> > if((session.getAttribute("destaques")==null)||
> > (session.getAttribute("menuSecoes")==null))
> > {
>
> session = request.getSession(true);
>
> >
> > System.err.println("destaques e menuSecoes nulos, enviando para o
> > servlet");
> > %>
> > <jsp:forward page="proxy?pagina=index.jsp" />
> > <%
> > }
>
> }
>
> >
> > detran.MenuSecaoBean[] menuSecoes = (pkg.MenuSecaoBean[])
> > sessao.getAttribute("menuSecoes");
> > %>
> > <jsp:useBean id="menuSecao" class="pkg.MenuSecaoBean" >
> > <jsp:useBean id="time" class="pkg.TimeBean" />
> > <jsp:useBean id="banners" class="pkg.BannersBean"/>
> > <html>
> > <head>
> > --------------
> >
> >
> > o meu Servlet:
> > ----------------
> > package pkg;
> >
> > import javax.servlet.*;
> > import javax.servlet.http.*;
> > import java.io.*;
> > import java.util.*;
> >
> > public class proxy extends HttpServlet {
> >
> > //Initialize global variables
> > // private String jspdir ="/";
> > private String error = "/error.jsp";
> > private boolean DEBUG = true;
> >
> >
> > public void service (HttpServletRequest req, HttpServletResponse res)
> > throws ServletException, IOException{
> >
> > String proxPagina;
> > //int codSecao;
> > Command cmd;
> >
> > try {
> > proxPagina = "/" + req.getParameter("pagina");
> > if (proxPagina == "/")
> > throw new CommandException("pagina nao especificada");
> >
> > if (( req.getParameter("secao") ==
> > null)&&(req.getParameter("divisao")==null)&&(proxPagina.equals("/index.jsp"))){
> >
> > cmd = new GetIndexCommand(proxPagina);
> > proxPagina = cmd.execute(req);
> > if (DEBUG){
> > System.out.println("no servlet passou por
> > GetIndexCommand");
> > System.out.println("Servlet id Sessao : " +
> > req.getSession().getId());
> > }
> > }
> > else if ((req.getParameter("divisao")==null)&&(
> > req.getParameter("secao") != null)){
> > cmd = new GetSecaoCommand(proxPagina);
> > proxPagina = cmd.execute(req);
> > if (DEBUG)
> > System.out.println("no servlet passou por
> > GetDivisoesCommand");
> > }
> > else if ((req.getParameter("divisao")!=null)&&(
> > req.getParameter("secao") == null))
> > {
> > cmd = new GetDivisaoCommand(proxPagina);
> > proxPagina = cmd.execute(req);
> > if (DEBUG)
> > System.out.println("no servlet passou por
> > GetDivisoesCommand");
> > }
> > else
> > throw new CommandException("Parametros estao fora do
> > padrao");
> > /*
> > if ( req.getParameter("secao") == null){
> > throw new CommandException("secao nao especificada");
> > }
> > else if (req.getParameter("divisao")==null){
> > cmd = new GetDivisoesCommand(proxPagina);
> > proxPagina = cmd.execute(req);
> > }
> > else{
> > cmd = new GetDivisaoCommand(proxPagina);
> > proxPagina = cmd.execute(req);
> >
> >
> > */
> >
> > }//fim try
> > catch (CommandException ex) {
> > req.setAttribute("javax.servlet.jsp.jspException", ex);
> > proxPagina = error;
> > }//fim catch
> >
> >
> >
> > RequestDispatcher rd;
> > ServletContext sc = getServletContext();
> > rd = sc.getRequestDispatcher( proxPagina );
> >
> > if ( DEBUG ) {
> > System.out.println( "System.out, next = " + proxPagina);
> > System.out.println( "System.out, rd = " + rd );
> > System.err.println( "System.err, rd = " + rd );
> > System.out.println( "System.out, sc = " + sc );
> >
> > }
> >
> >
> >
> > try {
> > rd.forward( req, res );
> > }
> > catch ( IOException e ) {
> > System.out.println( "IOException em Servlet" );
> > }
> >
> >
> >
> >
> > }
> >
> > }
> > --------------
> >
> >
> > o meu getIndexComman
> > -----------------------------
> > package pkg;
> >
> > import javax.servlet.http.HttpServletRequest;
> > import javax.servlet.http.HttpSession;
> >
> > public class GetIndexCommand implements Command {
> >
> > private String proxPagina;
> > private boolean DEBUG = true;
> >
> > public GetIndexCommand(String proxPagina){
> > this.proxPagina=proxPagina;
> > }
> >
> > public String execute(HttpServletRequest req) throws CommandException
> > {
> >
> > HttpSession sessao = req.getSession();
> > try {
> >
> > if
> >
>((sessao.getAttribute("destaques")==null)||(sessao.getAttribute("menuSecoes")==null))
> > {
> >
> >
> >
> > pkgRepositorio repositorio = pkgRepositorio.getInstancia();
> >
> > DestaqueBean[] destaques = repositorio.getDestaques();
> >
> > if (DEBUG)
> > System.out.println("em GetIndexCommand: destaque 0 ->" +
> > destaques[0].getMensagem());
> >
> >
> >
> > if (DEBUG)
> > System.out.println("em GetIndexCommand: ID Sessao ->" +
> > sessao.getId());
> >
> >
> > sessao.setAttribute("destaques", destaques);
> >
> >
> >
> > MenuSecaoBean[] menuSecoes = repositorio.getMenuSecaoAll();
> > sessao.setAttribute("menuSecoes", menuSecoes );
> >
> >
> > BannersBean banners = repositorio.getBanners();
> >
> > if (DEBUG){
> > System.out.println("em GetIndexCommand: banners -> " +
> > banners.getBanner());
> > System.out.println("em GetIndexCommand: ID Sessao ->" +
> > sessao.getId());
> >
> > }
> > //sessao.setAttribute("banners", banners);
> >
> > }//fim if
> >
> >
> > return this.proxPagina;
> > }
> > catch (RepositorioException e) {
> > throw new CommandException("GetDestaquesCommand: "+e.getMessage());
> > }catch(UnknownSecaoException ex){
> > throw new CommandException("GetDestaquesCommand: "+ex.getMessage());
> > }
> >
> > }
> > }
> >
> >
> > ------------
> > está tuod ocorrendo como planejado, exceto que uma nova sesao é criada
> > ao retornar a index.jsp.
> >
> > espero que alguem possa me ajudar.
> >
> >
> > []´s
> >
> >
> > Daniel A.
> >
> >
> > ------------------------------ LISTA SOUJAVA ----------------------------
> > http://www.soujava.org.br - Sociedade de Usuários Java da Sucesu-SP
> > dúvidas mais comuns: http://www.soujava.org.br/faq.htm
> > regras da lista: http://www.soujava.org.br/regras.htm
> > para sair da lista: envie email para [EMAIL PROTECTED]
> > -------------------------------------------------------------------------
> >
> >
>
> ------------------------------ LISTA SOUJAVA ----------------------------
> http://www.soujava.org.br - Sociedade de Usuários Java da Sucesu-SP
> dúvidas mais comuns: http://www.soujava.org.br/faq.htm
> regras da lista: http://www.soujava.org.br/regras.htm
> para sair da lista: envie email para [EMAIL PROTECTED]
> -------------------------------------------------------------------------
------------------------------ LISTA SOUJAVA ----------------------------
http://www.soujava.org.br - Sociedade de Usuários Java da Sucesu-SP
dúvidas mais comuns: http://www.soujava.org.br/faq.htm
regras da lista: http://www.soujava.org.br/regras.htm
para sair da lista: envie email para [EMAIL PROTECTED]
-------------------------------------------------------------------------