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

标签 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>

单击紧急按钮时,我用属性“消息”填充请求。重定向到同一页面后,我只看到 null。如何获取我的请求属性? sysout 被打印到控制台。还尝试重定向到 index.jsp 而不是 /index.jsp 但没有成功。

最佳答案

为什么这不起作用

您正在通过 AJAX 提交表单,正如我们所知,AJAX 意味着整个页面不会被刷新。仅刷新页面的特定部分。

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

这就是它不打印值的原因。

完成它的最简单方法

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

关于java - 如何从此请求中获取属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30802281/

相关文章:

java - 如何为抽象类创建对象数组?

java - 为什么没有 jsessionid 的资源会被拒绝访问?

java - 在 Java 中获取 <td> 的值 - request.getParameter

java - 如何从浏览器 URL 访问位于 java 服务器中的 PDF/图像/视频文件?

java - 检查 TreeSet 中的键是否以 String 开头,并获取该键

Java 设置、类路径和环境变量

java - 如何将 PEM 编码的椭圆曲线公钥加载到 Bouncy CaSTLe 中?

java - Spring 4 WebSocket 应用程序

java - Tomcat7 WEB-INF/lib 目录不起作用

java - 如何在magnolia中的jsp中迭代ContentMap