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:
- Once the webpage has finished loading, call the URL: UserData.aspx?Funct=....
- Send as a parameter the value of an input field with an id of 'tbUsername', with the name "username".
- Upon completion of request, the result is stored in the "output" variable.
- Copy the returned "output" and insert it into a DIV element with an id of 'outputDIV'.
- 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
精彩评论