简体   繁体   中英

Is it possible to execute the request.getRequestDispatcher from the post call of a javascript function?

I am calling a java controller by "post" and I send a form, I process it and send a response back with some attributes in the request and I wish that with javascript they can be processed. it's possible?

My controller:

  /**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}
/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException 
{
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter())
    {
        Respuesta r;
        request.setAttribute("MensajeRespuesta", "");
        request.setAttribute("StatusRespuesta", "");
        ServletInputStream aaaa = request.getInputStream();

    if (request.getParameter("btnGuardarDiio") != null && !"".equals(request.getParameter("btnGuardarDiio")))
        {
            //Code...
            request.setAttribute("MensajeRespuesta", r.getMensaje());
            request.setAttribute("StatusRespuesta", r.isStatus());
            request.setAttribute("StatusGuardado", true);
     request.getRequestDispatcher("/pages/maestros/crudAnimal.jsp").forward(request, response); 

        }
  .
  .
  .

My Script in JSP:

 <script>
        $('#myFormSubmit').click(function(e){
                $.post('crudAnimal?btnGuardarDiio=eliminar', $('#formGuardarDIIO').serialize(), 
                  function(responseText,respuesta){
                    if(respuesta = "success"){
                     // What do I have to do so that the "response" of the controller redirects to the page and can receive the attributes?
                   }

            });

            });
 </script>

Thanks!

on javascript client-side you will not be able at all to access any request/response attribute set on server-side. That belongs to the server and is not part of any http response payload.

But, your post seems to indicate you are writing that javascript code on a jsp page. If that jsp output is being regenerated per request, then you can play with that. Access to the request attributes through java code on the jsp (that is processed server-side) so that generated js code, being sent back to client, contains what you want.

here you have some basic documentation on how jsps work. i recommend reading that if you want to understand how this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM