JSF页面调用Servlet无响应问题求助(附相关代码)
Let's break down the issues preventing your Servlet from triggering and fix them step by step:
1. JSF Form & Submission Issues (index.xhtml)
- JSF commandButton doesn’t directly invoke Servlets:
h:commandButtonis built for JSF managed beans, not direct Servlet calls. To trigger a Servlet, use a plain HTML form or explicitly set the JSF form’sactionto your Servlet URL. - Missing
nameattributes: Your input fields lacknameattributes, so their values won’t be sent as request parameters to the Servlet. - Unsubmitted calendar value: The
<p:calendar>is bound to a bean but has noname, so the date won’t be included in the form submission.
Fixed index.xhtml:
<h:body> <f:view> <!-- Explicitly target the Servlet URL --> <h:form action="/parqueo.do" method="post"> <h:outputText value="Reserva de Parqueadero " /> <br/> <h:panelGrid columns="2"> <p:outputLabel value="Nombre:" for="nombre" /> <p:inputText id="nombre" name="nombre" title="Nombre" required="true" requiredMessage="Nombre es obligatorio."/> <p:outputLabel value="Tipo:" for="tipo" /> <p:inputText id="tipo" name="tipo" title="Tipo" required="true" requiredMessage="Tipo es obligatorio."/> <p:outputLabel value="Placa:" for="placa" /> <p:inputText id="placa" name="placa" title="Placa" required="true" requiredMessage="Placa es obligatoria."/> <p:outputLabel value="Hora Ingreso:" for="horain" /> <p:inputText id="horain" name="horain" title="Hora Ingreso" required="true" requiredMessage="Hora de ingreso es obligatoria."/> <p:outputLabel value="Hora Salida:" for="horaout" /> <p:inputText id="horaout" name="horaout" title="Hora Salida" required="true" requiredMessage="Hora de salida es obligatoria."/> <p:outputLabel value="Fecha:" for="fecha" /> <p:calendar id="fecha" name="fecha" value="#{calendar.date1}" showOn="button" navigator="true"></p:calendar> </h:panelGrid> <br/> <h:commandButton value="Enviar" type="submit" /> </h:form> </f:view> </h:body>
2. Servlet Code Errors (Parqueo.java)
- Incorrect parameter handling: Using
FacesContextin a Servlet is wrong—Servlets should useHttpServletRequestdirectly. You also have a typo:requestParams.get("tipo")for the Placa parameter (should be"placa"). - Response writer breaks forward: Calling
response.getWriter().print()before forwarding commits the response, causing an exception and blocking the redirect to the result page. - Forwarding to wrong page: You’re pointing to
reserva.xhtmlinstead ofresult.xhtml. - No storage for input values: You need to save user inputs to the request scope so
result.xhtmlcan access them.
Fixed Parqueo Servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Fetch parameters directly from the request object String nombre = request.getParameter("nombre"); String tipo = request.getParameter("tipo"); String placa = request.getParameter("placa"); // Fixed typo here String fecha = request.getParameter("fecha"); String horaIn = request.getParameter("horain"); String horaOut = request.getParameter("horaout"); if (horaIn != null && !horaIn.isEmpty() && horaOut != null && !horaOut.isEmpty()) { jaxws.Parking_Service service; jaxws.Parking port; try { service = new jaxws.Parking_Service(); port = service.getParkingPort(); String parqueoResult = port.reserva(horaIn, horaOut); // Store inputs and result in request scope for the result page request.setAttribute("nombre", nombre); request.setAttribute("tipo", tipo); request.setAttribute("placa", placa); request.setAttribute("fecha", fecha); request.setAttribute("result", parqueoResult); // Forward to the correct result page request.getRequestDispatcher("/result.xhtml").forward(request, response); } catch (Exception e) { // Add proper error handling e.printStackTrace(); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error al llamar al WebService"); } } else { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Las horas de ingreso y salida son obligatorias"); } }
3. Result Page EL Syntax Issues (result.xhtml)
- Invalid EL expressions:
#{session.getAttribute(name)}is incorrect—you need to pass parameter names as strings, or use scope variables directly. - Misplaced date converter: The
<f:convertDateTime>should be nested inside the date’s<h:outputText>. - Mixed EL syntax: Stick to JSF’s
#{}for consistency instead of mixing with${}.
Fixed result.xhtml:
<h:body> <f:view> <h:outputText value="Nombre: " /> <h:outputText value="#{requestScope.nombre}"/> <br/> <h:outputText value="Tipo: " /> <h:outputText value="#{requestScope.tipo}"/> <br/> <h:outputText value="Placa: " /> <h:outputText value="#{requestScope.placa}"/> <br/> <h:outputText value="Fecha: " /> <h:outputText value="#{requestScope.fecha}"> <f:convertDateTime pattern="MM/dd/yyyy" /> </h:outputText> <br/> <h:outputText value="Resultado: " /> <h:outputText value="#{requestScope.result}"/> </f:view> </h:body>
4. Verify web.xml Configuration
Your web.xml looks correct—just double-check that controlador.Parqueo is the fully qualified class name of your Servlet (matches the package structure).
After applying these fixes, clicking "Enviar" will trigger the Servlet, call your WebService, and forward to result.xhtml with all user inputs and the WebService result displayed properly.
内容的提问来源于stack exchange,提问作者ives rodriguez
相关产品推荐
相关产品推荐

