首先,我需要澄清——这是一个学术项目。我还觉得我应该说,我知道这是做我需要做的事情的错误方式,但是,这是我必须做的方式,所以请尽可能坚持回答问题而不是其他选择。
我必须在 JSTL 中或通过 servlet 显示 SQL 查询的结果,具体取决于浏览器是否是手机。我已经管理了 JSTL 部分,并确定它是否是电话,并且我可以很好地转发到 servlet。
问题是,我需要能够从 servlet 访问结果变量。从下一个 JSTL 页面访问它很容易,因为我可以将范围设置为应用程序,但是我不确定如何从 servlet 获取访问权限。
我已经尝试过了:
request.getParameter("viewResult");
但这只返回一个字符串,而我相信我需要一个
javax.servlet.jsp.jstl.sql.Result
对象。
有谁知道转发时将对象从 JSTL JSP 页面传递到 servlet 的方法吗?
First of all, I need to make it clear - this is for an academic project. I also feel I should say that I know this is the wrong way of doing what I need to do, however, it's the way I have to do it, so please stick to answering the question if possible rather than alternatives.
I have to display the results of an SQL query either in JSTL or through a servlet, depending on whether the browser is a phone or not. I've managed the JSTL part, and determining whether it's a phone, and I can forward to a servlet fine.
The problem is, I need to be able to access the result variable from the servlet. It's easy enough to access this from the next JSTL page, as I can set the scope to application, however I'm not sure how to go about getting access from the servlet.
I've tried:
request.getParameter("viewResult");
but this only returns a string, whereas I believe I need a
javax.servlet.jsp.jstl.sql.Result
object.
Does anyone know of a way to pass an object from a JSTL JSP page on to a servlet when forwarded?
发布评论
评论(1)
HttpServletRequest#getParameter()
返回 HTTP 请求参数,这些参数隐式始终为Strings
,因为它只是在 HTTP 规范中指定的。您宁愿将其设置为请求属性。您可以使用 var 属性来完成此操作“nofollow”>
sql:query
。这样,它将在 servlet 中可用,如下所示:
不用说,这确实不是最佳实践。这些标签是为快速原型设计而设计的,而不是为现实世界的应用程序而设计的(Sun/Oracle 自己的说法)。您应该在 servlet 的
doGet()
或doPost()
方法中获取数据库结果(用于预处理或后处理)。The
HttpServletRequest#getParameter()
returns HTTP request parameters which are implicitly alwaysStrings
since that's just specified so in the HTTP specification.You rather want to set it as request attribute. You can do that with
var
attribute ofsql:query
.This way it'll be available in the servlet as follows:
Needless to say that this is indeed not the best practice. Those tags are designed for quick prototyping, not for real world applications (Sun/Oracle's own words). You should rather be obtaining the DB result in
doGet()
ordoPost()
method of the servlet (for preprocessing resp postprocessing).