for me it seems impossible but expecting clarification on it. i am sending a request as follow :
<form action="/name" method="get">
<input type="text" />
<input type="submit" />
</form>
Now action class at server side manipulates & send the response to client, can i handle this response by ajax somehow ??
Yes, but you have to submit it via ajax (XmlHttpRequest) in order to be able to get the response that way.
Using jQuery makes this simple:
$.post("/name", {param:param}, function(data) {
});
In that example you should pass manually each form field as param. In case of bigger forms this is not that good. So you can use serialize():
$.post($("#yourForm").attr("action"),
$("#yourForm").serialize(),
responseHandlerFunction);
Related
am i implementing the post redirect get approach in java web. i have this index.jsp which i can add information to database.
<form action="servlet" method="post>
<input type="text" placeholder="itemname"/>
<input type="text" placeholder="itemprice"/>
<input type="submit" value="add item"/>
</form>
and in servlet i process the username and password
//returns a boolean if success or not
if(processItem(itemname,itemprice)){
response.sendRedirect("secondservlet?ADD=success");
}
and in the secondservlet
if(request.getParameter("ADD").equals("SUCCESS"))
request.getRequestDispatcher("success.jsp").forward(request,response);
am i doing it right?
am i doing it right?
You are sending a POST, then redirecting, which results in a GET. If that is what you were trying to do, then yes, you are doing it right.
Note that your second servlet should probably check if getParameter(..) returns null. You might get to the second servlet from some other call that doesn't include any request parameters.
I have a Following JSP:
<form action="UploadServlet" method="post" enctype="multipart/form-data">
Select file to upload: <input type="file" name="file" id =upfile"size="50" /> <input type="button" value="Save" onclick="javascript:uploadPartnerDetails();" class="buttons">
</form>
And DWR Script which calls remoted Java Method:
function uploadPartnerDetails() {
SMUDWR.uploadPartnerDetails(function(data) {
dwr.util.setValue("UserTypeDiv", data, {
escapeHtml: false
});
});
}
The Remoted Method uploadPartnerDetails()is:
try {
WebContext wctx = WebContextFactory.get();
HttpServletRequest request = wctx.getHttpServletRequest();
// checks if the request actually contains upload file
if (!ServletFileUpload.isMultipartContent(request)) {
// if not, we stop here
}
I get This Error :
the request doesn't contain a multipart/form-data or multipart/form-data stream, content type header is text/plain
The Problem is the request above does not contain multipart/form-data.
How to I get that from this request?
In general DWR calls are AJAX calls and in AJAX we cannot upload files just like form data.
There are different ways to upload files using AJAX calls
Find different ways provided in answers to this question jQuery Ajax File Upload
Go through these and you will get better understanding of file handling with AJAX
I am trying to find a way to invoke a piece of java code within the JSP using HTML form
<form method="get" action="invokeMe()">
<input type="submit" value="click to submit" />
</form>
<%
private void invokeMe(){
out.println("He invoked me. I am happy!");
}
%>
the above code is within the JSP. I want this run the scriptlet upon submit
I know the code looks very bad, but I just want to grasp the concept... and how to go about it.
thanks
You can use Ajax to submit form to servlet and evaluate java code, but stay on the same window.
<form method="get" action="invokeMe()" id="submit">
<input type="submit" value="click to submit" />
</form>
<script>
$(document).ready(function() {
$("#submit").submit(function(event) {
$.ajax({
type : "POST",
url : "your servlet here(for example: DeleteUser)",
data : "id=" + id,
success : function() {
alert("message");
}
});
$('#submit').submit(); // if you want to submit form
});
});
</script>
Sorry,not possible.
Jsp lies on server side and html plays on client side unless without making a request you cannot do this :)
you cannot write a java method in scriptlet. Because at compilation time code in scriptlet becomes part of service method. Hence method within a method is wrong.
How ever you can write java methods within init tag and can call from scriptlet like below code.
<form method="get" action="">
<input type="submit" value="click to submit" />
</form>
<%
invokeMe();
%>
<%!
private void invokeMe(){
out.println("He invoked me. I am happy!");
}
%>
Not possible.
When the form is submitted, it sends a request to the server. You have 2 options:
Have the server perform the desired action when the it receives the request sent by the form
or
Use Javascript to perform the desired action on the client:
<form name="frm1" action="submit" onsubmit="invokeMe()"
...
</form>
<script>
function invokeMe()
{
alert("He invoked me. I am happy!")
}
</script>
You can't do this since JSP rendering happens on server-side and client would never receive the Java code (ie. the invokeMe() function) in the returned HTML. It wouldn't know what to do with Java code at runtime, anyway!
What's more, <form> tag doesn't invoke functions, it sends an HTTP form to the URL specified in action attribute.
I have problem with receive data from jsp to servlet.
I know that I must serializes this data using JSON.
In my jsp in JavaScript I make something like this:
var myJSONText = JSON.stringify(items);
document.getElementById('test').value = myJSONText;
where, items is an array of JavaScript objects, and test:
<input type="hidden" name="test" id="test">
Now I want to receive this array on the servlet, I'm trying that (in method doPost()):
request.getParameter("test");
but it contains an empty value. Has anyone idea how to do it?
Can you show your code, when you do post request to server? Seems it should be something like
$.post('http:myurl.com', data)
or
<form ...>
<input type="submit">
</form>
I am using Struts/JSP for a webapp. I have a page A where I am collecting user defined parameters (as request parameters can't make them session params) and then I go to page B to ask yes/no kind of a question to the user. In case of yes I need to come back to page A and continue regular processing. But obviously the request object for page A is gone.
Is there a way to set page A's request object as parameter in page B so that when I come back to page A i have the same request object I had when i was there (on page A) the first time.
I need something like below:
page A --(req1)------> page B (set req.setAttr('prevReq', req1)) ------> page A (req = req.getAttr('prevReq'))
Any help is appreciated.
No, you can't do what you have in mind. Do you understand how the HTTP request-response cycle works?
User sends HTTP request to the server using a browser.
Server processes the request (your servlet or JSP is called).
Your servlet or JSP produces a response which normally consists of an HTML page.
The server sends the response back to the browser.
There is no way that you can save the request for page A, and then in page B respond to that request to make the browser go back to page A. That's just not how the request-response cycle works.
What you can do, is store data in the session object. You can call request.getSession() to get a HttpSession object, in which you can store data for the duration of the session of that user. In page A, you can get the data out of the session object again.
In a multi page process you will need to store all the intermittently gathered data into the session. See HttpServletRequest.getSession() and HttpSession.setAttribute(String, Object).
Use hidden input elements (input type="hidden") wherein you retain the request parameters of the form submit. Don't duplicate/store it as request attribute. They get lost when the response is finished.
Since I don't do struts, here's a basic example how the JSP should look like (leaving input labels and obvious security issues like XSS outside consideration, Struts should be smart enough to handle it itself).
Page A:
<form>
<input type="text" name="input1" value="${param.input1}">
<input type="text" name="input2" value="${param.input2}">
<input type="text" name="input3" value="${param.input3}">
<input type="hidden" name="yesorno" value="${param.yesorno}">
<input type="submit" value="go to page B">
<input type="submit" value="submit">
</form>
Page B
<form>
<input type="checkbox" name="yesorno" value="yes" ${!empty param.yesorno ? 'checked' : ''}>
<input type="hidden" name="input1" value="${param.input1}">
<input type="hidden" name="input2" value="${param.input2}">
<input type="hidden" name="input3" value="${param.input3}">
<input type="submit">
</form>