如何从此请求中获取属性?

时间:2015-06-12 11:46:07

标签: java jsp

Servlet代码:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    String action = request.getParameter("action");

    Connection conn = null;

    try {
        conn = ds.getConnection();
    } catch (SQLException e) {
        throw new ServletException();
    }

    UserOperations uo = new UserOperations(conn);

    if (action.equals("mainbutton")) {
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String address = request.getParameter("address");
        String country = request.getParameter("country");
        String state = request.getParameter("state");
        String postalCode = request.getParameter("postalcode");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        User user = new User(name, surname, address, country, state,
                postalCode, phone, email);
        try {
            if (user.validate()) {
                uo.create(name, surname, address, country, state,
                        postalCode, phone, email);
            } else {
                request.setAttribute("message", user.getMessage());
                System.out.println(request.getAttribute("message"));
                request.getRequestDispatcher("index.jsp").forward(request,
                        response);
            }

        } catch (SQLException e) {
            System.out.println("No connection to the database.");
            e.printStackTrace();
        }
    }

    try {
        conn.close();
    } catch (SQLException e) {
        throw new ServletException();
    }
}

JSP代码:

<script type="text/javascript">
    $(document).ready(function(){
        $("#mainbutton").click(function(){
            var name=$('input[name=name]').val();
            var surname=$('input[name=surname]').val();
            var address=$('input[name=address]').val();
            var country=$('#country :selected').val();
            var state=$('#state :selected').text();
            var postalcode=$('input[name=postalcode]').val();
            var phone=$('#mobile-number').val();
            var email=$('input[name=email]').val();
            $.post("userctrl",
                    {
                      action: "mainbutton",
                      name: name,
                      surname: surname,
                      address: address,
                      country: country,
                      state: state,
                      postalcode: postalcode,
                      phone: phone,
                      email: email
                    });
        });
    });
</script>
<li>
    <p class="valid-error">
        <%= request.getAttribute("message") %>
    </p>
</li>
<li>
    <input id="mainbutton" class="mainbutton" type="button" value="Rush"/>
</li>

点击紧急按钮后,我用atribute&#34; message&#34;填写请求。重定向到同一页面后,我看到只有null。如何获得我的请求属性? sysout将打印到控制台。还尝试重定向到index.jsp而非/index.jsp但没有成功。

2 个答案:

答案 0 :(得分:2)

为什么这不起作用

您正在通过AJAX提交表单,因为我们知道AJAX的意思是整页不会刷新。只刷新页面的特定部分。

现在,您的页面创建了一个Ajax请求,该请求转到servlet和servlet再次将该请求转发到JSP页面。但请注意,您的页面已在早期请求中显示。它不会被Servlet提供的请求重新打开/刷新。

这就是为什么它不能打印价值。

完成任务的最简单方法

尝试通过表单中的action属性提交没有Ajax的表单。

答案 1 :(得分:2)

试试这个:

在你的jsp中

        $.post("userctrl",
                {
                  action: "mainbutton",
                  name: name,
                  surname: surname,
                  address: address,
                  country: country,
                  state: state,
                  postalcode: postalcode,
                  phone: phone,
                  email: email
                },
           success: function(response) {
                $(".valid-error").html(response)
           });

从Servlet传递你的messenge

        if (user.validate()) {
            uo.create(name, surname, address, country, state,
                    postalCode, phone, email);
        } else {
          PrintWriter out = response.getWriter();
          out.print(user.getMessage());
        }