Servlet JSP getparameter getattribute返回null

时间:2014-01-08 07:33:05

标签: java jsp servlets getattribute getparameter

我将servlet变量传递给jsp时遇到了麻烦。

当然,我也已经为servlet设置了web.xml

<servlet>
    <servlet-name>databaseServlet</servlet-name>
    <servlet-class>Servlet.databaseServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>databaseServlet</servlet-name>
    <url-pattern>/dbServlet</url-pattern>
</servlet-mapping>

结果是所有名称,所有者,物种和性别值均为空。有人可以帮我弄这个吗?感谢

P.S:我也尝试在servlet中使用request.getSession()。setAttribute,但都没有工作

P.P.S:所以如果我做出以下改变:

databaseServlet.java

package Servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;

@SuppressWarnings("serial")

public class databaseServlet extends HttpServlet {
private Connection conn;
private Statement statement;

String name;
String owner;
String species;
String sex;
String birth;
String death;

public void init(ServletConfig config) throws ServletException {
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection(
                "jdbc:mysql://localhost/STUDENTS",
                "root",
                "");
        statement = conn.createStatement();

        String sql = "SELECT name, owner, species, sex, birth, death FROM pet";
        ResultSet rs = statement.executeQuery(sql);

        //STEP 5: Extract data from result set
        while(rs.next()){
            //Retrieve by column name
            name  = rs.getString("name");
            owner = rs.getString("owner");
            species = rs.getString("species");
            sex = rs.getString("sex");
            birth = rs.getString("birth");
            death = rs.getString("death");
        }
        rs.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.setAttribute("NAME", "Hello");
    System.out.println(name);
    request.setAttribute("OWNER",owner);
    request.setAttribute("SPECIES",species);
    request.setAttribute("SEX", sex);
    RequestDispatcher dispatcher=getServletConfig().getServletContext().getRequestDispatcher("/dbServlet.jsp");
    dispatcher.forward(request,  response);
}

}

这是我的新jsp:

<body>
Name="${databaseServlet.NAME}" 
Owner="${databaseServlet.OWNER}" 
Species="<%= request.getAttribute("SPECIES") %>"
Sex="<%= request.getSession().getAttribute("SEX") %>"

</body>

Name和Owner都返回空字符串,而Species和Sex仍返回NULL

基本上我要做的是访问MySQL数据库以从表中检索变量,并使用JSP显示它

3 个答案:

答案 0 :(得分:0)

name方法中设置请求属性时,请确保您拥有ownerspeciessexdoPost的有效值。

使用EL语法,如

${NAME}
${OWNER}
${SPECIES}
${SEX}

不要在JSP中编写scriptlet ,因为scriptlet不应该在JSP中使用超过十年。学习JSP ELJSTL,并使用servlet获取Java代码。 How to avoid Java Code in JSP-Files?

答案 1 :(得分:0)

尝试使用


RequestDispatcher dispatcher=getServletConfig().getServletContext().getRequestDispatcher("/dbServlet.jsp");

我想它应该有效。 并使用request.getAttribute() 只有它会返回对象类型,你必须将其强制转换

答案 2 :(得分:0)

只需为您的属性提供范围,您就可以像这样更改代码:

protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    request.getSession().setAttribute("NAME", "Hello");
    System.out.println(name);
    request.getSession().setAttribute("OWNER",owner);
    request.getSession().setAttribute("SPECIES",species);
    request.getSession().setAttribute("SEX", sex);
    RequestDispatcher dispatcher=getServletConfig().getServletContext().getRequestDispatcher("/dbServlet.jsp");
    dispatcher.forward(request,  response);
}

当你处理属性时,让它像sessionScope一样:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<body>
<c:set var="myName" value="NAME" />
<c:set var="myOwner" value="OWNER" />
Name="${sessionScope[myName]}" 
Owner="${sessionScope[myOwner]}" 
       //etc...
</body>

我希望这会对你有所帮助。