如何从JSP获取java类的对象 [英] How to get an object of java class from JSP

查看:245
本文介绍了如何从JSP获取java类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结果jsp页面,它打印java类的字符串变量。
有人可以给我任何想法。

I have a result jsp page, which print the string variable of java class. Can someone give me any idea.

我使用简单的httpServlet作为动作类。

I am using simple httpServlet as action class.

Class A 
{  
String name;  
public void setName()
{  
   this.name = callMethod(); // somehow it assigns the value. Nothing to do with problem  
}  
}

Result.jsp

这里我需要打印String的当前值

Here i need to print the current value of String

推荐答案

我假设servlet最后将请求转发给JSP。

I assume that the servlet, at the end, forwards the request to the JSP.

您应该在请求中将要在JSP中使用的对象存储为属性。

You should store the objects you want to use in the JSP in the request, as an attribute.

在动作类/ servlet中:

In the action class / servlet :

request.setAttribute("name", name);

在JSP中(使用JSTL):

In the JSP (using the JSTL) :

<c:out value="${name}"/> or <c:out value="${requestScope.name}"/>

这篇关于如何从JSP获取java类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆