i am new to the servlet and jsp scene and was having trouble with getting my data from my servlet out to my jsp. I followed examples i found online and have done everything i know how to do in order to get data to my jsp.
我是servlet和jsp场景的新手,并且无法将我的servlet数据输出到我的jsp。我按照我在网上找到的例子,做了我知道如何做的一切,以便将数据传送到我的jsp。
Here is the code that i have for my servlet. I tried to pull all the data from the database and then forward that to jsp.
这是我的servlet代码。我试图从数据库中提取所有数据,然后将其转发给jsp。
Another problem i had was that if i try to print any of the info to the console with system.out nothing shows.
我遇到的另一个问题是,如果我尝试使用system.out打印任何信息到控制台没有任何显示。
I commented some stuff out to try and eliminate possible problem areas.
我评论了一些东西,试图消除可能的问题区域。
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
try
{
String query = "select * from PRODUCT_TABLE";
// connect to DB
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("took this out for security");
Statement stmt = conn.createStatement();
// get info from DB
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
// itemName = rs.getString("ITEM_NAME");
itemNum = rs.getString("ITEM_NUM");
itemPrice = rs.getString("ITEM_PRICE");
// compile all the data
/*itemNameComp[i] = itemName;
itemNumComp[i] = itemNum;
itemPriceComp[i] = itemPrice;
i++;*/
}
req.setAttribute("itemName", 567);
req.getRequestDispatcher("/StartShopping.jsp").forward(req, resp);
//String test = req.getParameter("itemName");
//rd.forward(req, resp);
//store info in the session object
// get selection from user and do calculation for total purchase
}
catch (Exception e)
{
e.printStackTrace();
}
in the code below i tried to hard code some value to try and see if i could at least get something to the jsp even if it wasnt from the data that i wanted.
在下面的代码中,我试图硬编码一些值来尝试,看看我是否至少可以获得jsp的东西,即使它不是我想要的数据。
req.setAttribute("itemName", 567);
req.getRequestDispatcher("/StartShopping.jsp").forward(req, resp);
The code that i used to call the hard set data in the JSP is below
我用来调用JSP中的硬集数据的代码如下
<%=(String)request.getAttribute("itemName")%>
The code above prints "null" in the .jsp. i wanted to try and make it print just as a test to see if i could force it to work with hard data.
上面的代码在.jsp中打印“null”。我想尝试打印它作为测试,看看我是否可以强制它使用硬数据。
i tried changing it to req.getAttribute but the jsp wasnt happy with it
我尝试将其更改为req.getAttribute但jsp并不满意
Any insight would be greatly appreciated. Thank you very much!!! i apologize if there is some obvious error but i can't find it.
任何见解将不胜感激。非常感谢你!!!如果有一些明显的错误但我找不到,我道歉。
2 个解决方案
#1
0
Are you trying to just run the jsp file?
你想尝试运行jsp文件吗?
In that case the attribute is never forwarded from the servlet and the jsp page always prints null.
在这种情况下,永远不会从servlet转发属性,并且jsp页面始终打印为null。
try to create a html file which invokes the post method in servlet:
尝试创建一个调用servlet中的post方法的html文件:
<html>
<body>
<form action="/yourServletURL" method="post">
<input type="submit">
</form>
</body>
</html>
#2
0
May be because your are setting an integer and trying to typecast it to String Use the following code and check once
可能是因为您正在设置一个整数并尝试将其强制转换为String使用以下代码并检查一次
req.setAttribute("itemName", "567");
//instead of req.setAttribute("itemName", 567);
#1
0
Are you trying to just run the jsp file?
你想尝试运行jsp文件吗?
In that case the attribute is never forwarded from the servlet and the jsp page always prints null.
在这种情况下,永远不会从servlet转发属性,并且jsp页面始终打印为null。
try to create a html file which invokes the post method in servlet:
尝试创建一个调用servlet中的post方法的html文件:
<html>
<body>
<form action="/yourServletURL" method="post">
<input type="submit">
</form>
</body>
</html>
#2
0
May be because your are setting an integer and trying to typecast it to String Use the following code and check once
可能是因为您正在设置一个整数并尝试将其强制转换为String使用以下代码并检查一次
req.setAttribute("itemName", "567");
//instead of req.setAttribute("itemName", 567);