简体   繁体   English

使用jquery将请求转发到servlet,而j​​Query又转发到jsp?

[英]Forward request to servlet using jquery which in turn forwards to jsp?

I have a js file and based on the below condition I need to display a message or forward the request to servlet which in turn forwards to jsp. 我有一个js文件,根据以下条件,我需要显示一条消息或将请求转发到servlet,然后再将其转发到jsp。

var isInIframe = (parent !== window),
    if (isInIframe) {
        //Forward request to servlet
    }else {
        //Display Access denied message
    }

I can call a servlet using jquery ajax. 我可以使用jquery ajax调用servlet。 But how can I forward request to servlet? 但是如何将请求转发到servlet? once the request is forwarded to servlet then servlet will load some property files and forwards request to some.jsp as below. 一旦将请求转发到servlet,则servlet将加载一些属性文件,并将请求转发到some.jsp,如下所示。

//Keeps some values using request.setAttribute() then forward the request
request.getRequestDispatcher("/WEB-INF/some.jsp").forward(request, response);

How can i do that? 我怎样才能做到这一点? Thanks! 谢谢!

You can use Session for sending some objects to servlet. 您可以使用Session将一些对象发送到servlet。 Because session remains same for the user throughout the application. 因为会话在整个应用程序中对用户而言都是相同的。

JQuery/Javascript does not "forward" requests to web applications. JQuery / Javascript不会将请求“转发”到Web应用程序。 It makes new requests via AJAX or redirects. 它通过AJAX或重定向发出新请求。 JQuery itself does not handle HTTP requests. JQuery本身不处理HTTP请求。

You need to make an AJAX request for this. 您需要为此提出AJAX请求。 I assume that JSP is the presentation layer of your web application. 我假设JSP是Web应用程序的表示层。 So just make an ajax request to your web app using the JQuery $.ajax() method.. 因此,只需使用JQuery $ .ajax()方法向您的Web应用发出ajax请求。

You can handle the response using the AJAX callback function 您可以使用AJAX回调函数处理响应

Servlet and jsp are server-side components. Servletjsp是服务器端组件。 The Server receives HTTP requests and dispatches a Servlet to handle them by producing a corresponding HTTP response for each of them, some times with the help of a jsp . 服务器有时会在jsp的帮助下接收HTTP请求并通过为每个请求生成相应的HTTP响应来调度Servlet来处理它们。

Javascript is a client-side component. Javascript是客户端组件。 It runs in the browser. 它在浏览器中运行。 When the javascript runs, the HTTP response containing the HTML you see in the browser has already been sent, so the Servlet is completely out of the picture. 运行javascript ,已经发送了包含您在浏览器中看到的HTML的HTTP响应,因此Servlet完全无法显示。 You cannot forward to it like you are imagining with 您无法像您想象的那样forward

request.getRequestDispatcher("/WEB-INF/some.jsp").forward(request, response);

What you can do is use ajax to construct and send a new HTTP request that will be handled by a Servlet . 您可以使用ajax构造并发送一个新的HTTP请求,该请求将由Servlet处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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