[英]Servlet JSP getparameter getattribute returning null
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 谢谢
PS: I also tried to use request.getSession().setAttribute in the servlet, didn't work either PS:我也尝试在servlet中使用request.getSession()。setAttribute,也没有用
PPS: So if I make the following changes: PPS:因此,如果我进行以下更改:
databaseServlet.java 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显示它
Make sure that you have valid values for name
, owner
, species
and sex
when setting request attribute inside doPost
method. 在doPost
方法中设置请求属性时,请确保您具有有效的name
, owner
, species
和sex
值。
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. 不要在JSP中编写scriptlet ,因为scriptlet在JSP中不应使用超过十年。 Learn the JSP EL , the JSTL , and use servlet for the Java code. 学习JSP EL和JSTL ,并将servlet用于Java代码。 How to avoid Java Code in JSP-Files? 如何避免JSP文件中的Java代码?
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类型,您必须将其强制转换
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. 我希望这可以帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.