[英]Calling part of jsp page from Servlet or Java Class
I need to call small part of JSP
page(like only div load) when a method is invoked? 调用方法时,我需要调用
JSP
页面的一小部分(仅类似于div加载)吗? I know we can load whole JSP
page from Servlet
. 我知道我们可以从
Servlet
加载整个JSP
页面。 Is this possible or any other solution for this? 这可能或其他解决方案吗?
know we can load whole JSP page from Servlet.
知道我们可以从Servlet加载整个JSP页面。 Is this possible
这可能吗
Yes it is possible, you forward your request to jsp from servlet and it will render that jsp, here is an exact same example 是的,有可能,您将请求从servlet转发到jsp,它将呈现该jsp, 这是一个完全相同的示例
1) Set some session or request variable equal to some value, say boolean myVar = true
. 1)设置某个会话或请求变量等于某个值,例如
boolean myVar = true
。
2) Redirect from your jsp
page from your servlet. 2)从servlet的
jsp
页面重定向。
3) In the jsp
page, check for particular condition for the variable myVar
. 3)在
jsp
页面中,检查变量myVar
特定条件。 If condition is fulfilled, using scriptlets
or JSTL
display only the relevant part of you jsp
. 如果满足条件,则使用
scriptlets
或JSTL
仅显示jsp
的相关部分。
You can use AJAX(Asynchronous Javascript and XML) for this purpose 为此,您可以使用AJAX(异步Javascript和XML)
For example, Suggestions, that appear without any refresh for auto-complete in google search, are using Ajax. 例如,在Google搜索中无需任何刷新即可自动完成的建议正在使用Ajax。 JSP code will contain the Ajax code and will send the information to the servlet in form of XmlHttpRequest object and then takes the response from servlet as xmlhttp.responseText .
JSP代码将包含Ajax代码,并将以XmlHttpRequest对象的形式将信息发送到servlet,然后将来自servlet的响应作为xmlhttp.responseText 。 The result can be written at JSP page by using DOM.
可以使用DOM将结果写入JSP页面。
To initiate this process, you need to use the onkeyup in input tag like this: 要启动此过程,需要在输入标签中使用onkeyup ,如下所示:
<input type="text" onkeyup="methodName(this.value)"
Learn more about Ajax 了解有关Ajax的更多信息
To use AJAX with Java, try this 要将AJAX与Java结合使用,请尝试以下操作
$('#myDiv').load('serverPage.jsp #server_div_id');
OR 要么
$.ajax({
url: 'serverPage.jsp',
success: function(data) {
data=$(data).find('div#id');
$('#mydiv').html(data);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.