servlet不会返回上一页

时间:2014-06-09 22:31:39

标签: java jquery jsp servlets

这是我的网络servlet。它的第一项工作是从数据库中检索国家/地区列表并在选择表单元素中打印出来。为此,一切正常。当我单击表单中的提交按钮时,我通过action="/submit"调用servlet。假设我在提交表单时没有插入任何用户名。 validateSignUpForm返回`true。而不是返回注册页面,我在提交页面。我不明白。当validationErrorFlag为true时,我应该回到注册页面而不是再次进入提交页面。错误在哪里?

    @WebServlet(name = "SignUpServlet", urlPatterns =
    {
        "/signup"
})
public class SignUpServlet extends HttpServlet
{
    @EJB
    private UtilBeanInterface utilBean;

    /**
     * Handles the HTTP <code>GET</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 doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        request.setAttribute("CountriesList", utilBean.getContriesList());
        request.getRequestDispatcher("/WEB-INF/view/signup.jsp").forward(request, response);
    }

    /**
     * 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
    {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String confirm_password = request.getParameter("confirm_password");
        String email = request.getParameter("email");

        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String address = request.getParameter("address");
        String city = request.getParameter("city");
        String zipcode = request.getParameter("zip_code");
        String country = request.getParameter("country");
        String homenumber_code = request.getParameter("homenumber_code");
        String homenumber = request.getParameter("homenumber");
        String mobilenumber_code = request.getParameter("mobilenumber_code");
        String mobilenumber = request.getParameter("mobilenumber");

        boolean validationErrorFlag = false;

        validationErrorFlag = utilBean.validateSignUpForm(username, 
                                                          password, 
                                                          confirm_password, 
                                                          email, 
                                                          name, 
                                                          surname, 
                                                          address, 
                                                          city, 
                                                          zipcode, 
                                                          country, 
                                                          homenumber_code, 
                                                          homenumber, 
                                                          mobilenumber_code, 
                                                          mobilenumber, 
                                                          request);

        if(validationErrorFlag == true)
        {
            doGet(request, response);
        }

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }

}

JSP PAGE

<article class="container_12">
    <section id="signupform" class="grid_5">
        <h4>Registrazione membri</h4>
        <p>(*) Campi richiesti</p>
        <h6>I tuoi dati d'accesso su VolaConNoi.it</h6>
        <form action="submit" method="POST">
            <div>
                <label>Username* <span class="advice">(max 16 caratteri)</span></label>
                <input type="text" name="username" autofocus>
                <c:if test="${!empty usernameInvalid}">
                    <p class="errorSignUp">Errore: inserisci un username valido</p>
                </c:if>
                <c:if test="${!empty usernameAlreadyExist}">
                    <p class="errorSignUp">Errore: l'username è già in uso</p>
                </c:if>
            </div>
            <div>
                <label>Password*</label>
                <input type="password" name="password">
            </div>
            <div>
                <label>Conferma Password*</label>
                <input type="password" name="confirm_password">
            </div>
            <div>
                <label>E-mail*</label>
                <input type="email" name="email">
            </div>

            <br/>

            <h6>Dati Personali</h6>
                <div>
                    <label>Nome*</label>
                    <input type="text" name="name">
                </div>
                <div>
                    <label>Cognome/i*</label>
                    <input type="text" name="surname">
                </div>
                <div>
                    <label>Indirizzo*</label>
                    <input type="text" name="address">
                </div>
                <div>
                    <label>Città*</label>
                    <input type="text" name="city">
                </div>
                <div>
                    <label>CAP*</label>
                    <input type="text" name="zip_code">
                </div>
                <div>
                    <label>Paese*</label>
                    <select name="country">
                        <c:forEach items="${CountriesList}" var="country">
                            <option value="${country.iso}">${country.nicename}</option>
                        </c:forEach>
                    </select>
                </div>
                <div>
                    <label>Fisso</label>
                    <select name="homenumber_code">
                        <c:forEach items="${CountriesList}" var="country">
                            <option value="${country.phonecode}">${country.nicename} (+${country.phonecode})</option>
                        </c:forEach>
                        <input type="text" name="homenumber">
                    </select>
                </div>
                <div>
                    <label>Mobile*</label>
                    <select name="mobilenumber_code">
                        <c:forEach items="${CountriesList}" var="country">
                            <option value="${country.phonecode}">${country.nicename} (+${country.phonecode})</option>
                        </c:forEach>
                    </select>
                    <input type="text" name="mobilenumber">
                </div>

                <div>
                    <input type="submit" value="Registrati"/>
                </div>
        </form>
        <div id="signupfooter"></div>
    </section>
</article>

1 个答案:

答案 0 :(得分:1)

比较

@WebServlet(name = "MainControllerServlet",
        loadOnStartup = 1,
        urlPatterns = {"/signup",
                       "/submit"})

使用:

String url = "/WEB-INF/view" + userPath + ".jsp";

即使userPath = "/signup";你永远不会进入注册servlet,因为你提到了#34; .jsp&#34;在那里。

另外,如果你曾经到过/signup,它看起来就像一个无限循环一遍又一遍地转发给自己。

顺便说一下,为每个不同的操作实际制作单独的servlet可能要简单得多,而不是加倍它们并进行这种字符串操作。