call servlet when radio button is clicked - java

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

Related

hyperlink redirect to another JSP

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

Wrong URL after clicking submit on JSP from

I am new to JSP and I am dealing with a confusing issue. I have a JSP form located in a sub folder called "admin" in my web-app (named "CMS").
CMS/admin/display_content.jsp
My form has the following values for action and method attribute
<form action="/deleteContent" method="POST">
/deleteContent is the URL pattern for a servlet named DeleteContentServlet. It simply deletes from the DB the user selections. Anyway, my problem is once I click on submit I notice that I get the incorrect URL in my address bar. Instead of getting
http://localhost:8080/CMS/deleteContent
I get
http://localhost:8080/deleteContent
How can I fix this ? When I have sub folders, are the files only used for imports maybe ?
Thank you.
Use the JSTL <c:url> tag for all your URLs:
it prepends the context path (whatever it is) to absolute URLs
it writes the session ID in the URL in case the browser doesn't accept cookies:
<form action="<c:url value='/deleteContent'/>" method="POST">
For links, it also allows passing parameters to the URL, and encodes them properly (via the <c:param> inner tag).
According to this, you can use request.getContextPath() in your servlet to get the context path. And you need not specify the hostname for the action unless it is different than yours:
<form action="<%= request.getContextPath() %>/deleteContent" method="POST">
Hope that helps...

how to identify which servlet called by which jsp file

I've a URL pattern like webroot/TellSomeoneMail and corresponding class,
<servlet>
<servlet-name>TellSomeoneMail</servlet-name>
<display-name>Tell Someone Mail</display-name>
<servlet-class>com.nightingale.register.servlet.TellSomeoneMailServlet</servlet-class>
</servlet>
but how to identify which JSP file calling this servlet?
You can identify during execution into our servlet by looking to the referer header in the HTTP body:
String referrer = request.getHeader("referer");
Edit 1: You can also use session to keep the last url acceded by the user (such mechanism is already present in framework like grails or Spring under the "flash" attribute, not to be confused with adobe flash). If you use simple Servlet / JSP, you need to code such support...
Edit 2 Last solution if cookie and referee is blocked, is to add a parameter in the URL with reference to the last page, for instance URL?from=home_pg or URL?from=/homepage.html but it could require rewriting of urls embedded in the page.
you can get the URL from which the request was sent. Take a look at the following code
if (request instanceof HttpServletRequest) {
String url = ((HttpServletRequest)request).getRequestURL().toString();
}
To find JSP pages that allow the user to make requests to your servlet : Check the path the servlet is mapped under in the <servlet-mapping> element(s) in web.xml.
Then do a full text search on all the JSP's in your projet for this string. Look for HTML <a> and <form> element with target containing your servlet path.

JSP onload call servlet without scriptlets

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()">

Redirect to different page from inside c:import

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>

Categories