I have a login.jsp form that I have written for a website that just contains a form for logging in that I can include and re-use in different places. The first place I am including it is in my index.jsp homepage.
I have an HTTPServlet that the form submits to, and if the username/password is invalid the Servlet sends a message back to display to the user, otherwise it forwards them on to their homepage.
My problem is that I want to forward them back to the same page they're on if their details are incorrect, but display the message, but forwarding to the login page displays only that form in the browser, outside of the page it was included in. Is there any way I can forward back to the current page? Or is this perhaps not the best way to go about this?
Thanks for reading.
I am presuming you are using a <jsp:include>. Here is something you might do:
<jsp:include page="loginForm.html">
<jsp:param name="currentPage" value="${whateverTheCurrentPageIs}" />
</jsp:include>
Include the currentPage variable in the loginForm. Then, in your servlet, you want to redirect/forward to the URL you passed in that currentPage variable.
Related
I want to display something from one jsp page in another jsp page by clicking a button. I did it using request.setAttribute request.getAttribute but it doesn't work for me, for some reason the variable I send is null or the page is blank.
From your original question : When you are doing setAttribute(), its scope is limited to the request when the main page is loading and hence will not be available on the next page as it will be a new request.
<%Object product=ptp;
request.setAttribute("purchase", ptp.getId());
%>
What you can do is, submit this value in URL param as GET or in a form (get/ post) to fetch it on next JSP using request.getParameter().
Or you can use session scope by session.setAttribute()
Hope it helps
you can pass the variables through request scope or session scope.
request.setAttribute("variable name","value of its");
session.setAttribute("variable name","value");
Here a detailed exmple
http://www.jsptut.com/sessions.jsp
I am making a JSP/Servlet web app which starts with log in page.
After user enters log in information, a servlet, called LoginServlet, processes the data and if the log in is successful the servlet redirects user to MainPage.jsp like this:
RequestDispatcher rd = request.getRequestDispatcher("MainPage.jsp");
rd.forward(request,response);
It works. It takes me to MainPage.jsp, only the URL is NOT:
http://localhost:8080/RestauRec/MainPage.jsp
it is:
http://localhost:8080/RestauRec/LoginServlet
It is not an actual problem, but still, I want to know why this is and how can I change this?
Note: I don't know if it matters or not but, in the action attribute of the form element (in the log in page) I place LoginServlet. Like this:
<form action="LoginServlet" method="POST">
Thanks in advance!
forward is an action that happens within a single request-response cycle. It uses the forward-to resource to complete the response.
Your browser sends a single request to /someUrl and your server handles it, returning a response.
It is not an actual problem, but still, I want to know why this is and how can I change this?
You'd have to make your client, the browser, send a different request to another URL, possibly because of a redirect.
forward() method won't change the url. sendRedirect() in HttpServletResponse do change the url as well.
response.sendRedirect("MainPage.jsp");
Remember that a new request gets hit to the container when you do redirect. That means all the previous data vanishes and you'll get a brand new request.
Ok, so I've got an interesting case of login page redirection going on.
My webservice has a login page (login.html) with some javascript to handle logging in and redirecting to a hardcoded 'default' page. The webservice is written in Java with a servlet filter handling redirection if a user is unauthenticated (so if a user tries to access domain/statistics without being logged in, they are directed to domain/login.html). The redirection from the protected services works: I can redirect to the login page and once a user is authenticated, redirect them to a default page. I am having issues, however, redirecting to the previous page.
I know this is usually handled with the argument document.referrer in the Javascript, which I have tried, but due to the Java's redirection with response.sendRedirect, the Referer header is not sent.
How can I get these two aspects to redirect to the previously called page? Is it something I need to add on the Javascript side, the Java side, or both?
What I've done is to drop the original (redirected) URL into a hidden input field on the login form. The code that does the authentication should just check that parameter, and if it's not empty it can redirect after establishing the session.
You have to be careful doing this to prevent XSS attacks etc., but it's not that hard and it works just fine.
In my framework (Stripes), I can push the original URL (taken from the HttpServletRequest object, a combination of the servlet path, the "path info", and the query string) into a special holding box that will cause the framework to give it back to me on the next request as a parameter. Without that, the simple thing to do is add the URL as a parameter when you redirect. Just URL-encode the original attempted URL and tack it onto the redirect URL with some parameter name. Then, on your login page, you just check for it:
<c:if test='${not empty param.attemptedUrl}'>
<input type='hidden' name='attemptedUrl' value='${fn:escapeXml(param.attemptedUrl)}'>
</c:if>
Then your login action will get that parameter too when the login form is submitted, and it can act on it as appropriate.
Send Redirect will ask to the client to repeat the request to the resource you choose. Have you think of Spring Security with minimal configuration you can achieve this quite easily.
Take a look at this:
http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ns-config.html
I want to call my web site like:
http://localhost:8080/?co=grav
When doing this the authentication page configured in web.xml comes up and the login is done through form login. After that it will go to index.jsp where I am doing a redirect to one of my main pages of the site:
<c:redirect url="index-userapp.jsp" >
The problem is that, this redirect does no longer forward my co parameter, and I really need to not loose it...
Do you see a workaround?
I think it can be done like:
<c:redirect url="index-userapp.jsp">
<c:param
name="co"
value=""></c:param>
</c:redirect>
but how I put in the new value, the old parameter value from the initial request?
try this:
<c:redirect url="index-userapp.jsp">
<c:param name="co"
value="${param.co}"></c:param>
</c:redirect>
I am trying to figure out how to do the following.
I have webpage at a certian location called www.hello.com/logout.jsp
What I am trying to do with logout.jsp is delete all the cookies that were stored when initially logging in. The problem is that there exists a cookie for a website with a different domain that is stored when logging in. The one way I can delete that cookie is through the logout link for that website e.g. www.hello2.com/logout.jsp
Is there anyway I can call www.hello2.com/logout.jsp from www.hello.com/logout.jsp?
I am trying to just make a call for www.hello2.com/logout.jsp from www.hello.com/logout.jsp and then redirect the user to another page on www.hello.com
Thanks in advance :D
If I understand correctly, you are trying to do an HTTP POST( or GET ) to www.hello2.com/logout.jsp while processing HTTP request to your web application's logout.jsp.
You should really consider coding your logic in Servlets and using JSPs only to present data, but in the meantime you can create a scriptlet inside your logout.jsp and do a call to another web page in there ( just don't code the whole thing in JSP, only make a call to a static method ).
In that static method you can use HttpClient to do whatever HTTP request you need from www.hello2.com.
Here are additions to your logout.jsp
<%# page import="my.package.Hello2Call" %>
<%
Hello2Call.postLogoutRequest( );
%>