jsp获取不到request的attribute

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp获取不到request的attribute相关的知识,希望对你有一定的参考价值。

我在servlet里面的语句:ArrayList <introducer> result = PerHandle.queryind(level,name);
request.setAttribute("introducer", result);然后传递给queryintroducer。jsp然后获取: ArrayList<introducer> tem =(ArrayList<introducer>) request.getAttribute("introducer");
if(tem==null) System.out.print(6); 它一直输出6 为什么获取不到 大家帮忙看看啊谢谢

参考技术A 或许你跳转方式有问题 ,好像有种跳转数据会丢失样 你试试其他的跳转方式 参考技术B 在页面也需要一个容器保存 introducer 的值! 参考技术C 名字写错了 。。

Servlet通过JavaBean传值到JSP页面

主要通过Attribute进行传递,主要代码如下:

赋值,并定义跳转的页面:

        request.setAttribute("user", user);
        request.getRequestDispatcher("view.jsp").forward(request, response);

取值,在JSP页面或者另外一个Servlet中:

UserInfo user=(UserInfo)request.getAttribute("user");

示例:

1、login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="Java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title>登录页面</title>
</head>
<body>
    <form action="PassServlet" method="post">
        <table>
            <tr>
                <td>用户名:</td>
                <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <td>&nbsp;码:</td>
                <td><input type="password" name="pwd" /></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><input type="radio" name="sex" value="男" /><input
                    type="radio" name="name" value="女" /></td>
            </tr>
            <tr>
                <td>年龄:</td>
                <td><input type="text" name="age" /></td>
            </tr>
            <tr>
                <td>Email:</td>
                <td><input type="text" name="email" /></td>
            </tr>
            <tr>
                <td><input type="submit" value="注册" /> <input type="reset"
                    value="重置" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

2、UserInfo.java

package com.jsoft.testservletbasics;

public class UserInfo {
    private String userName;
    private String userPwd;
    private String userSex;
    private int userAge;
    private String userEmail;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserPwd() {
        return userPwd;
    }

    public void setUserPwd(String userPwd) {
        this.userPwd = userPwd;
    }

    public String getUserSex() {
        return userSex;
    }

    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }

    public int getUserAge() {
        return userAge;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserEmail() {
        return userEmail;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

}

3、PassServlet.java

package com.jsoft.testservletbasics;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class PassServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
        String age = request.getParameter("age");
        String email = request.getParameter("email");
        UserInfo user = new UserInfo();
        user.setUserName(name);
        user.setUserPwd(pwd);
        user.setUserSex(sex);
        user.setUserAge(Integer.parseInt(age));
        user.setUserEmail(email);
        request.setAttribute("user", user);
        request.getRequestDispatcher("view.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

5、view.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.jsoft.testservletbasics.UserInfo" %>
<!DOCTYPE HTML >
<html>
<head>
<title>显示页面</title>
</head>
<body>
    <%
      UserInfo user=(UserInfo)request.getAttribute("user");
     %>
    <table border="1" align="center">
        <tr>
            <td>用户名:</td>
            <td><%=user.getUserName()%></td>
        </tr>
        <tr>
            <td>&nbsp;码:</td>
            <td><%=user.getUserPwd()%></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td><%=user.getUserSex()%></td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><%=user.getUserAge()%></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><%=user.getUserEmail()%></td>
        </tr>
    </table>
</body>
</html>

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test19

以上是关于jsp获取不到request的attribute的主要内容,如果未能解决你的问题,请参考以下文章

servlet获取不到表单数据?

jsp中怎么提取redirect中参数的值

如何获取httpservletrequest

前一个将jsp页面能用session获取值,再打开一个新的jsp页面用session怎么获取不到??

java保存到session里,新的JSP页面怎么获取不到值

JSP 总结