I want that when my JSP is loaded to call a servlet. I have this link:
Homepage
But this way I have to click the link, I want to perform the calling automatically when the jsp is loaded.
On the other side I need to use no scriptlets. Does anyone have any idea how to do this?
Although its marked as resolved but I am editing my answer for future reference:
Apart from the javascript solution you can accomplish this with 2 more options using jsp tags:
Option1:
You can forward the request to the corresponding servlet.
Use jsp standard action jsp:forward, e.g.:
<jsp:forward page="ContentServlet?action=userContents" >
</jsp:forward>
You can replace your link with the above tag and the servlet will be called.
Option2:
you can redirect the request to your servlet using JSTL tags:
<c:redirect url="ContentServlet?action=userContents" />
Again you can replace your link with the above tag.
In Option1 browser's url will not change.
In Option 2 browser's url will change to "ContentServlet?action=userContents"
Hope it solves your problem.
Why don't you use JavaScript?
<script type="text/javascript">
function redirect(){
window.location = "/ContentServlet?action=userContents"
}
</script>
...
<body onLoad="redirect()">
Related
I need to pass params to included jsp
<%# include file="second.jsp" %>
i need to use it several times in one jsp page, so i need to pass id.
I can't use jsp:include because jsp can't see struts action in this way
Can u help me? Thank you
Set them as attributes on the request object (which makes them visible to the ${var} syntax) before invoking your include.
See How to access a request attribute set by a servlet in JSP? for a similar question.
I have a hyperlink for one of the column which looks like this.
<td align="ct"><a href="<%=getContext()%>/otp/taxView.do?call=first&taxId=<bean:write name="otping" property="taxNumber" />"><bean:write name="otping"
property="taxNumber" /></a>
</td>
Can I use the same to redirect to a different uri? When I google for redirect there are options to move action to controller and use sendRedirect and meta-refresh. Will the above work? or should I use a different method?
$(document).ready(function(){
$("a").click(function(e){
e.preventDefault();
window.location.href = 'Different uri'
});
});
Hope it helps.
This is referred as post-redirect-get pattern. You send POST to one uri, it performs action and then returns 'redirect' to the browser, which presents result page. This prevents re-posting when user is going through back/forward in history. This requires using respnse.sendRedirect(), as it looks like using Struts it can be defined via configuration.
Check here for more details:
post-redirect-get-prg-pattern-in-servlet-jsp
post-redirect-get-when-passing-data-to-the-form
Implement-Post-Redirect-Struts
How can pass json values from jsp to javascript as object via ajax?
I can not use global js variables in jsp because this will lead to json content to be visible in page's source
Here is the scenario that I want to achieve:
url of jsp is opened in browser.
Data is being created in scriptlet and coverted to JSON format
json is "sent" to javascript as object
From above scenario, i understand that javascript must initiate the ajax call to jsp.
The issue with this, that jsp's code will be invoked 2 times:
When page is opened in browser - data is prepared
on each ajax call same code will be called again
Constrains: No jquery, no other libs, no servlets, no additional jsps. :(
EDIT:
There is additional problem, I need to pass multiple json objects to javascript.
I wont be able to do it with response.getWriter().write();
I don't think concatenating all json objects and sending is the correct solution.
The parsing of the received object in javascript http.responseText will be overwhelming.
Why do you need ajax here? If you know that you need to populate some things from server onto jsp page you can do that through scriplets itself:
EX
<%# page import="com.mypackage.PersonDAO" %>
<html>
<body>
<table>
<th>Name</th><th>Email</th><th>Contact</th>
<%
List<Person> myList = PersonDAO.getAllPersons();
for(Person person:myList)
{
%>
<tr>
<td><%=person.getName()%></td>
<td><%=person.getEmail()%></td>
<td><%=person.getContact()%></td>
</tr>
<%}%>
</table>
</body>
</html>
This is a very simple example. You can do more complex things using JSTL.. :)
So there is no jquery, no Servlets, no ajax and no extra jsp's :)
UPDATE
Since you want data in your javascript before page loads you can use jQuery's holdReady() method.
$.holdReady( true );
$.get( url, function() {
// Perform something
$.holdReady( false );
});
See but all modern browsers have developer tools like firebug for mozilla, so any ajax call made will be trapped by them. The only way you can secure them is my encrypting them.. which will complicate things for you... IF you can explain the scenario you are trying to implement may be I can come up with it..
I want to call servlet when radio button is clicked, how can I make it?
EDIT
I tried to add URL to servlet in javascript function like this
$.post( "ParentManagementServlet", "isActivated");
and like this
$.post(<%=getServletContext().getContextPath()+"/ParentManagementServlet"%>, "isActivated");
and like this
$.post("/ParentManagementServlet?isActivated=true");
but both does not call servlet!
here's the url mapping of the servlet in web.xml
<servlet>
<servlet-name>ParentManagementServlet</servlet-name>
<servlet-class>ps.iugaza.onlineinfosys.servlets.ParentManagementServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ParentManagementServlet</servlet-name>
<url-pattern>/ParentManagementServlet</url-pattern>
</servlet-mapping>
I usually add the servlet through its name, but I read that it's better to get the servlet absolute path form servlet context.
As per the comments:
about fireBug, here's what returns $ is not defined
The $ is definied by jQuery. You've apparently not declared it in your page. jQuery is a JS library and not something already builtin the browser or something.
Put the following line before any other <script> in your <head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
(you can of course also download and serve a copy from your own domain)
<script src="jquery-1.5.2.min.js"></script>
See also:
Update current page with Servlet - contains several complete jQuery-Servlet examples
Use ajax. For example, with jQuery:
...onclick="invokeServlet()"
function invokeServlet() {
$.post("/path/to/servlet", params);
}
To call servlet in time when user select radio (without submit button) use:
Add appropriate mapping to your Servlet - Bind some URL to servlet.
use onselect attribute to call javascript function, which will redirect to the URL
example:
<input type ="radio" Value="blah blah" onSelect="yourFunction()"/>
In other situation the idea is the same: bind Servlet, choose event which will be trigger the servlet
Is there any way I can redirect to a different page from a Spring Controller that gets called from a JSP using <c:import>?
Scenario is as follows: I have a Spring WizardFormController, that handles a multi-page form and is included into the website using a JSP and <c:import>. After the wizard is finished, I would like to redirect to a different page, but that seems to be impossible from the Controller. At least, if I could get a message to the surrounding JSP, it would already help.
It seems, the only way is to use JavaScript to create a client-side redirect like this:
<script type="text/javascript>
window.location.href = '<URL of Target>';
</script>