开发者

Request to the servlet without any submit action from a jsp

I have a jsp ExpressLinking.jsp. As soon as we get to this jsp i need to send a request to the ControllerServlet with the query parameter of the request

<%=request.getContextPath()%>/ManageMyMappings?cmd=loadStatus&linkid=<%=linkId%>

After the Controller runs, I need to display a message based on the status received with two buttons "OK" and "Cancel". 开发者_Python百科The status is a variable.

How do I achieve sending the request to servlet without any submit action. Is there any possiblity to send the request with an auto page refresh. Please help


As home says, you need to look at AJAX. There's a fairly simple example here:

http://mabdelghani.wordpress.com/2008/12/07/ajax-with-jquery/

But use this form instead:

<script type="text/javascript">  

 $(document).ready(function() {  

         $.post("UserData.aspx?Func=RetrieveUserInfo",  

         { username: $("#tbUsername").val() }, function(output) {  

             $("#outputDiv").html(output);  

             $("#outputDiv").css("display", "block");  

         });  

 });  

The above code:

  1. Once the webpage has finished loading, call the URL: UserData.aspx?Funct=....
  2. Send as a parameter the value of an input field with an id of 'tbUsername', with the name "username".
  3. Upon completion of request, the result is stored in the "output" variable.
  4. Copy the returned "output" and insert it into a DIV element with an id of 'outputDIV'.
  5. Change the "display" style of 'outputDIV' to 'block' - AKA make the DIV visible.

This depends on JQuery, but I don't think that's a bad thing. You can do it without JQuery, but I think this is simplest.


You could use <jsp:include> for this.

<jsp:include page="ManageMyMappings?cmd=loadStatus&linkid=${linkId}" />

However, it's usually done the other way round. You should directly request the servlet by its URL and the servlet should in turn forward to the JSP.

See also:

  • Servlet wiki page
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜