简体   繁体   English

从Servlet或Java类调用jsp页面的一部分

[英]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 . 如果满足条件,则使用scriptletsJSTL仅显示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结合使用,请尝试以下操作

Ajax for Java Web Applications Java Web应用程序的Ajax

$('#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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM