Java Questions
Java Questions

Reputation: 7953

How to call the servlet?

i have a logout link in jsp and i call the logout function like this

function logout(){
    document.getElementById("functiontype").value="logout";
    document.forms["frmTempcard"].submit();               
}

i call this like following : <li><a href="#" onclick="logout()">Logout</a></li>

when i click on the link i see the following url : http://localhost:8080/acct/notifier[the jsp file is there in this folder only ]/TempCardServlet

when i remove the folder name in url it is working fine. so how to redirect to desired servlet?

Please help me.

Upvotes: 0

Views: 919

Answers (3)

ioreskovic
ioreskovic

Reputation: 5699

This:
<a href="/myWebApp/servlets/logout">Logout</a>

... calls your own servlet mapped like this:

@WebServlet(name="logout", urlPatterns={ "/servlets/logout" })
public class LogoutServlet extends HttpServlet {
    ...
}

... in which you do your logging out logic.

Upvotes: 1

Rutesh Makhijani
Rutesh Makhijani

Reputation: 17225

You should look at the action attribute of form, as per the code you have shared and the problem description, I feel you need to update action of your form.

Currently you would have given action='TempCardServlet' which means submit the request to TempCardServlet residing in the same URL as the current JSP page. This is relative path reference.

When you specify action='SomePath' then browser submits request to http://server/currenturl/SomePath. If the path to which you need to submit the request is not in current path then you need to either specify relative path or absolute path in following manner:

Relative Path (in your case): action='../TempCardServlet' - this method is not recommended as relative path might break your application

Absolute Path with Application context: action='/acct/TempCardServlet' - this would refer to servlet on the same server, drawback of this is that you are hard-coding applicaiton context. To overcome this you can use Servlet API to get current application context and append it to your servlet path - you can also use standard tag libraries to get the context path appended.

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691635

Make the action attribute of your form point to the appropriate URL:

<form action="<c:url value='/TempCardServlet'/>" ...>

If you need to change it dynamically, add this to your JS code (before the submit, of course):

document.forms["frmTempcard"].action = "<c:url value='/TempCardServlet'/>";

Upvotes: 1

Related Questions