0

I am working in an academic Semantic Web project, so I have designed and I am trying to implement a web site in order users can perform "semantic searches". I wrote two servlets using Jena package, and I need these two servlets chained.

package controlador;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.compObjA.GeraSequenciaBean;
import com.compObjA.model.ConceitosSelecionados;
import com.compObjA.model.ListaConceitos;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntTools.Path;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.util.iterator.Filter;

/**
 * Servlet implementation class GeraSequenciaServlet
 */
public class GeraSequenciaServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GeraSequenciaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        OntModel m_model = ModelFactory
                .createOntologyModel(OntModelSpec.OWL_DL_MEM);
        final Filter<Statement> ANY = Filter.any();

        // RDFNode end;
        String c1, c2; // define as variáveis c1 e c1, conceitos inicial e final
        String NS = "http://example.com/test#"; // define a URI

        // dois parametros para definir a lista dos conceitos
        c1 = request.getParameter(ConceitosSelecionados.getConceitoInicial());
        c2 = request.getParameter(ConceitosSelecionados.getConceitoFinal());

        // cria os nós inicial e final do caminho
        Resource start1 = m_model.createResource(NS + c1);
        RDFNode end = m_model.createResource(NS + c2);

        // instancia o gerador de sequencias & gera o caminho
        GeraSequenciaBean geraSequencia = new GeraSequenciaBean();
        Path caminho = geraSequencia
                .generateSequence(m_model, start1, end, ANY);

        // instancia a lista de conceitos e cria uma lista de conceitos a partir
        // do caminho
        ListaConceitos listaConceitos = new ListaConceitos();
        List<String> sugestao = listaConceitos.getListaConceitos(caminho);
        request.setAttribute("sugestao", sugestao);
        // chama o dispatcher e cria o jsp para mostrar a sequencia de conceitos
        RequestDispatcher view = request
                .getRequestDispatcher("SequenciaDosConceitos.jsp");
        // passa para frente.
        view.forward(request, response);
    }
}

and

package controlador;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.compObjA.model.ConceitosSelecionados;
import com.compObjA.model.SelecionaConceitosForm;

/**
 * Servlet implementation class ControlaSelecaoConceitosUsuario
 */
public class ControlaSelecaoConceitosUsuario extends HttpServlet {
    private static final long serialVersionUID = 1L;

    // Constantes ----------------------

    private static final String VIEW = "selecionaConceitos.jsp";
    private static final String ATTRIBUTE_FORM = "selecionaConceitosForm";
    private static final String ATTRIBUTE_SELECTED = "conceitosSelecionados";

    // Variaveis -----------------------------------------

    private ConceitosSelecionados conceitosSelecionados;

    // Actions --------------------

    public ControlaSelecaoConceitosUsuario() {
        super();
        // TODO Auto-generated constructor stub

    }

    @Override
    public void init() throws ServletException {
        // Obtém o UserDAO do DAOFactory por meio do Config.

    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        request.getRequestDispatcher(VIEW).forward(request, response);

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // Prepara o formBean

        SelecionaConceitosForm selecionaConceitosForm = new SelecionaConceitosForm(
                conceitosSelecionados);
        request.setAttribute(ATTRIBUTE_FORM, selecionaConceitosForm);

        // Processa a solicitação e obtém os resultados
        ConceitosSelecionados conceitosSelecionados = selecionaConceitosForm
                .selecaoUsuario(request);
        request.setAttribute(ATTRIBUTE_SELECTED, conceitosSelecionados);

        String destino = "/GeraSequenciaServlet";
        request.getRequestDispatcher(destino).forward(request, response);

    }

}

I got an exception. Could someone give me a hint?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
hjmnzs
  • 131
  • 2
  • 14
  • 1
    In the future questions, please cut down relevant code into the smallest possible working snippets which reproduces your problem (thus: remove any irrelevant code/noise). Please also share the whole exception and stacktrace as it usually contains the answer. Exceptions should not be ignored as if they are decoration. – BalusC Aug 25 '11 at 17:13

1 Answers1

0

You should use RequestDispatcher#include() whenever you want to invoke another servlet from insie a servlet and then return(!). You should not use RequestDispatcher#forward() for that.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you BalusC. I have studied your tutorial, and as you can see I have use some of your ideas on it. I am not an expert in servlet programming, but I must to. I could not past exception output because there would be more than 2 links. – hjmnzs Aug 25 '11 at 17:19
  • Just post it in your question instead of posting external links? – BalusC Aug 25 '11 at 17:20
  • I am wondering about one thing. The GeraSequenciaServlet uses Jena Framework library. It is not compiled, in web-inf there is not GeraSequenciaServlet.class. So I wondered if I had to put Jena Library at WEB-INF library. However I haven't got no success... – hjmnzs Aug 25 '11 at 19:25
  • Ok, as I put Jena library at server path, class was compiled. Thank you BalusC once more. – hjmnzs Aug 25 '11 at 20:39