Servlet JSP getparameter getattribute返回null

时间:2021-11-21 11:50:35

I'm having trouble with passing servlet variables to jsp.

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

Of course, I also have the web.xml set for the servlet already

当然,我也已经为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>

The result is all of the name, owner, species and sex values are null. Can someone help me with this? Thanks

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

P.S: I also tried to use request.getSession().setAttribute in the servlet, didn't work either

P.S:我也试过在servlet中使用request.getSession()。setAttribute,也没用

P.P.S: So if I make the following changes:

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);
}

}

and this is my new jsp:

这是我的新jsp:

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

</body>

both Name and Owner returns empty string, while Species and Sex still returns NULL

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

basically what I'm trying to do is to access MySQL database to retrieve variables from a table, and display it using JSP

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

3 个解决方案

#1


0  

Try using


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

I guess it should work. And use request.getAttribute() only, it will return Object type, you will have to cast it

我想它应该有效。并且仅使用request.getAttribute(),它将返回Object类型,您必须将其强制转换

#2


0  

Make sure that you have valid values for name, owner, species and sex when setting request attribute inside doPost method.

在doPost方法中设置request属性时,请确保您具有名称,所有者,种类和性别的有效值。

Use EL syntax like

使用EL语法

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

Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?

不要在JSP中编写scriptlet,因为scriptlet不应该在JSP中使用超过十年。学习JSP EL,JSTL,并使用servlet作为Java代码。如何避免JSP-Files中的Java代码?

#3


0  

Just give a scope to your attributes, and you can change your code like this :

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

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);
}

And when you handling the attribute make it sessionScope like :

当你处理属性时,使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>

I hope this helps you.

我希望这可以帮助你。

#1


0  

Try using


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

I guess it should work. And use request.getAttribute() only, it will return Object type, you will have to cast it

我想它应该有效。并且仅使用request.getAttribute(),它将返回Object类型,您必须将其强制转换

#2


0  

Make sure that you have valid values for name, owner, species and sex when setting request attribute inside doPost method.

在doPost方法中设置request属性时,请确保您具有名称,所有者,种类和性别的有效值。

Use EL syntax like

使用EL语法

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

Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code. How to avoid Java Code in JSP-Files?

不要在JSP中编写scriptlet,因为scriptlet不应该在JSP中使用超过十年。学习JSP EL,JSTL,并使用servlet作为Java代码。如何避免JSP-Files中的Java代码?

#3


0  

Just give a scope to your attributes, and you can change your code like this :

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

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);
}

And when you handling the attribute make it sessionScope like :

当你处理属性时,使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>

I hope this helps you.

我希望这可以帮助你。