I have jsp page that imports Testing.java
<jsp:useBean id="test" scope="session" class="Testing" />
<jsp:useBean id="sb" scope="session" class="SessionBean" />
<jsp:useBean id="eb" scope="session" class="ErrorBean" />
I need to call public method that is in Testing class after user confirms changes.
this is what I have so far:
<tr>
<td align="left">
<a href="<%=test.persistPrintingInfo(eb,sb) %>" >
<img src="../images/update.gif" OnClick="if( !confirm('Update printing information?')) return false"></a>
</td>
</tr>
Does anyone know how to do this?
Can I maybe call javascript method and call persistPrintingInfo() through javascript?
the page has been sent by the server to your browser. while javascript can modify the content of your page , in order to call a bean's method you must make a call to the server(a request to the servlet) beacause the bean lives on the server side. and this call can be made by creating an url mapped to the servlet, or a form whose action is the servlet
`<FORM ACTION="${pageContext.request.contextPath}/sampleServlet">`
if the form's method is GET, then on the doGet() method of the servlet you call your bean's method.
this form does not need to contain any kind of field. it is created just to make a request to the servlet. while you would normally click the submit button to proceed to the action, this time we will submit the form through javascript. with some javascript tricks, i think this form can also be hidden, because you don't actually need it to be displayed in your page
so you simply create this form in your jsp, and submit it through javascript , like this:
on your link, you will have onClick=myJavascriptMethod(); in your jsp, you create a javascript block
<script type="text/javascript">
function myJavascriptMethod)=()
{
document.forms["myform"].submit();
}
</script>
You can use this way, although there is better approaches using servlets.
<%com.example.Testing.yourMethod()%>
a second approach which i found while googling is this one:
How do I pass current item to Java method by clicking a hyperlink or button in JSP page?
in your case, the code will be
<img.. >
the newPage.jsp will contain just
<%yourPackage.YourClass.yourMethod()%>
Related
I have a parent jsp file that includes two child jsp's. I have a variable defined in the parent file like so:
<c:set var="test" value="N" />
which I then pass into two child jsp files:
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp">
<jsp:param name="controlFlag" value="${test}"/>
</jsp:include>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp">
<jsp:param name="controlFlag" value="${test}"/>
</jsp:include>
</div>
In my page1.jsp file I then store the value in a hidden div:
<div id="cashAuditFlag" style="display: none;">${param.cashAuditFlag}</div>
I then have a button in page1.jsp that, when clicked, I want it to change the value of the parent variable ${test} to "Y". This in turn would then change the value of ${test} in page2.jsp which would cause a change in page2.jsp.
I basically want to have a child jsp communicate an update to another child jsp, both of which belong to the same parent.
A - Is this the best way of doing this process?
B - How can I have a child jsp update the parent jsp variable?
Thank you!
To understand this, you need to understand scopes. Think of a scope as a bucket that variables go into when they are defined. Some code only has access to some of those buckets.
Request scoped variables are available in any part of the code that knows about the http request. There is only one request scope per HTTP request to your webapp. In your example, the parent file, page1.jsp, and page2.jsp all have access to that request scope. For example, if you did this in your parent JSP page:
<c:set var="test" value="N" scope="request" />
...it would put the "test" variable into the request scope bucket with a value of N.
Then, if you want to view or modify this value in either page1.jsp or page2.jsp, you don't even need to have a jsp:param element in your jsp:include, so you can just do this:
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp"/>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp"/>
</div>
So, if you want to display this in either child page, you can simply use Expression Language and tell it to look in the requestScope for the variable named "test" by using the requestScope object:
<p>The Test Variable is: ${requestScope.test}</p>
Similarly, if you wanted to modify this variable in either childPage, you can simply do another c:set statement:
<c:set var="test" value="Y" scope="request" />
Now, if you print out the value of ${requestScope.test} in any page, it will be Y.
In your example, when you used the c:set statement without scope="request", you created a variable in that jsp page's "page scope", meaning you could only access that variable in the jsp code you wrote in your parent jsp page.
Now, as to whether this is the best way to do this...
You say you have a button in page1.jsp that, when clicked, should change the test variable to Y and cause some display change in page2.jsp. Here's the flow of what would need to happen:
User vists your JSP page at some url, like "mywebapp/testPage.jsp"
The page renders. The initial c:set statement runs which sets the "test" var to a value of "N".
The user clicks the button, which causes the browser to send a new request but adds a request parameter, resulting in a request of something like "mywebapp/testPage.jsp?buttonClicked=1"
All your JSP pages render again (keep in mind JSP does not do things "dynamically" i.e. without a browser refresh - when you click a button, the browser sends a new HTTP request to the webserver and your JSP is rendered again).
At the top of your parent JSP, you need logic that checks whether the buttonClicked request parameter is present. If so, it sets the value of test to "Y" instead of "N".
So, to explain: in order to have the page render differently based on whether the button was just clicked, you would need to have your button pass a request parameter when it is clicked, and you would need to have your JSP look at the new request to find that request parameter (to see the value that was submitted when the button was clicked). If you don't have your code check that, then, every time your page loads, your parent JSP page will just keep re-setting the test variable to N because your initial c:set value="N" statement will always run when the JSP renders.
So, if you want something like the case you described, you'd have to do something like this in your parent JSP page:
<c:set var="test" value="N" scope="request"/>
<!-- Here's the check for whether the request parameter is present -->
<c:if test="${not empty param.buttonClicked}">
<c:set var="test" value="Y" scope="request"/>
</c:if>
<div id="div_data_1" style="display:none;">
<jsp:include page="page1.jsp"/>
</div>
<div id="div_data_2" style="display:none;">
<jsp:include page="page2.jsp"/>
</div>
...then in page1.jsp, where you have the button, you would do something like this:
<input type="submit" name="buttonClicked" value="1" />
That way, when the button is clicked, it will refresh the page and add a new request parameter called buttonClicked with a value of "1". In the parent JSP, it will see that this request parameter is present (with "not empty param.buttonClicked") and it will set the value of the test variable to "Y", overwriting the previous assignment of "N".
Then in your page2.jsp, you can access the value of test using ${requestScope.test} at any point on your page.
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 a simple JSP as follows:
<form name="myform" id="myform"
action="${pageContext.request.contextPath}/validateLoginID.do" method="POST">
<input type="text" name = "loginID" id="loginID" value="${loginID}"/>
</form>
<script>
window.alert("Submitting form!");
document.myform.submit();
</script>
The above JSP code works. But when I try to initialize the action from a variable instead, using something like:
action="${myLink}" method="POST">
It goes into an infinite loop, printing "Submitting form!" each time. Why is this happening?
Well, probably because the action it submits the form to returns the above page, which submits the form, which displays the page, which submits the form, etc.
"myLink" was actually a dummy name for the variable "name" that I was actually using. I tried to mimic ${pageContext.request.contextPath} and have a name like that, but unfortunately, the dot in the variable name was causing the problem. With the dot removed, it works.
I think the struts validation framework implied underneath the code above. A far as it returns the input the same page involve the javascript to submit the page with errors again and it's repeating. Remove document.myform.submit(); or make function
<script type="text/javascript">
function doSubmit(){
window.alert("Submitting form!");
document.myform.submit();
}
</script>
that will stop submitting on load.
In my jsp page, there is link as follows.
<s:url var="editReqDetails" action="editReqDetails">
<s:param name="siteID" value="siteId"/>
</s:url>
when I click on that link, browser URL is
http://localhost:7101/legal/editReqDetails?siteID=99
like above.(The parameter shows in the URL.)
I want to know how to hide above highlighted part(the parameter) from the url.
If you can use javascript you could do this
<s:a href="#" onclick="window.location.href='%{editReqDetails}'">Edit Details</s:a>
This way you "hide" the url from the user. Though I'm not sure what the big problem is. If the user is malicious he can easily look in the source and get the values.
No, you can't use this. You pass parameter with http GET method that is default used in s:url tag and you want to get http POST method behavior. See the usage of struts url and choose one http GET or POST method.
You can do this:
<form id="edit-form" action="editReqDetails" method="POST">
<input type="hidden" name="siteID" value="siteId" />
</form>
Then:
<script type="text/javascript">
$(document).ready(function() {
$("#your-link").click(function(e) {
$("#edit-form").submit();
});
});
</script>
I currently have a JSP page with a Form for the user to enter their name, but what I want is to get the user forwarded to a different JSP page after form submission and to carry on their name to be used.
I don't want to use JSTL EL just simple JSP uses.
I was thinking of using a bean storing the detail in a session but how would it work.
Thanks.
You'd have JSP enter the info into a form and POST it to a servlet. The servlet would validate the form input, instantiate the bean, add it to session, and redirect the response to the second JSP to display.
You need a servlet in-between. JSPs using JSTL are for display; using the servlet this way is called MVC 2. Another way to think of it is the front controller pattern, where a single servlet handles all mapped requests and simply routes them to controllers/handlers.
duffymo you have the best idea but here is a quick solution by just passing things through the JSP.
Here is the JSP with the form
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Simple jsp page</title></head>
<body><form name="test" action="./stackTest2.jsp" method="POST">
Text Field<input type="text" name="textField">
<input type="submit">
</form> </body>
</html>
and then the second page looks like this:
<html>
<head><title>Simple jsp page</title></head>
<body><%=request.getParameter("textField")%></body>
</html>
And then put information in a hidden field, you can get information by using the request.getParameter method. This just prints out what was in the form, but using the same idea for inputting it in to the hidden field in a form. I recommend this as all my experience with sessions have ended in a failure. I STRONGLY DO NOT Recommend this method, MVC is a much better way of developing things.
Dean