I have a portlet which involves displaying several JSP pages.
In first JSP page A, when I click the Submit button on Page A the processAction() method takes action and a JSP page B appears.
Now if I use Web Browser's Back button to page A and click the Submit button again, the
JSP page B appears but I noticed the processAction() didn't take any action. (Usually clicking Submit button in a JSP page can result in the processAction() to take action).
Can anyone help for this problem? In my Porlet, it MUST go to the process action but it doesn't after back button.
This is the default behavior. Portal has "Multiple Action URL Protection " enabled by default. When a page loads, an action link is created and that link contains an action ID. The same action ID cannot be used again in the same session. So when you click on Back Button, if the page is loaded from the history cache, your Form contains the same action link which was used before. So portal simple reloads the page, rather than calling the processAction().
You can disable this by adding the following configuration for your portlet in portlet.xml file.
<init-param>
<name>wps.multiple.action.execution</name>
<value>true</value>
</init-param>
Without seeing any code, it sounds as if your form response may be cached. What is the method attribute on your <form> ? Forms submitted via GET (or no method attribute at all) are allowed to be cached; in which case neither the server nor your portlet's processAction(...) will be invoked - the browser will re-render the previous response from cache.
If you post some code there may be more offers to help...
Related
I have a search.jsp page that has some html content and a form. When the form is submitted, there is a servlet handle the form data and forward the results to the search.jsp page. However, the url in the browser after processing the form is changed to the servlet name:
http://localhost:8080/MyProject/SearchServlet
not the search.jsp page:
http://localhost:8080/MyProject/Search.jsp
How I can change the url to the search.jsp? In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?
You cannot do that by forwarding the request: you need to "tell" the browser to generate a new http request by using the response.sendRedirect() method.
Now the question is why do you want the url bar to display the name of the Jsp?
Hiding the real destination path is a desired feature when forwarding requests: users do not need to know the server side redirects (that's how they are also called) happening in your web app.
Think about it: to carry out its tasks a servlet potentially can forward the request a number of times before getting to the final destination: you don't want the url bar to change each and every single time.
Give a fancier name to your servlet like: "Search" rather than "SearchServlet" so that users will know they are on the search page of your web application and not in the "SearchServlet" page.
In addition to that, if you visit any professional website, you will hardly ever see the .jsp or .html or .php extension on the address bar. While that is not a requirement or specification and you are free to do so, I believe the first approach is best practice (it looks even better to me honestly). There is even a folder WEB-INF whose purpose is to hide your .jsp pages from direct access via url bar.
What I like doing is having a servlet as the landing-welcome page of the web app, that will be responsible to forward and redirect requests based on the user input and the inner working of the application.
Now back to your final request (pun intended)
"In other words, I just want to refresh the search.jsp page to display the results in the same page. How I can do that?"
What I would do is:
redirect the user to the "Search" servlet from the welcome/home servlet.
In the doGet method of the Search servlet I would forward the request to the search.jsp page (you could set attributes before forwarding if you need to).
In the search.jsp I would set the action attribute of the form to "Search" (the name of the servlet) and the method to POST.
In the doPost method of the Search servlet you would implement whatever logic you wish to implement and finally forward the request to the search.jsp
After hitting the search button (and even after the submit button is clicked) what the user will see on the address bar is simply
http://localhost:8080/MyProject/Search
Hope that makes sense.
Are you using the same search.jsp for searching and well as showing the result? It is possible to use the same jsp to perform both the functions but it's easier and desirable to make another jsp which will only show the results.
If you are not able to see the results on search.jsp then make sure that you are setting the proper response in the Servlet class before forwarding it to the jsp and also whether you are reading the response sent by the Servlet class properly in the jsp.
If you want, the page to not refresh at all, then go for AJAX.
I have a web page in jsp extension , which is built using jsf. There are command links for some user ids, i.e. every user id is a command link. When a user id is clicked, it goes to another page, where some action can be done.
<h:commandLink value="#{user.userId}" rendered="#{bean.myName == 'ID1'}" action="#{bean.userIdHasBeenSelected}" immediate="true"/>
Now if the action returns a string called "notDone", i want to show an alert pop up in the jsp and the page will not be navigated and will remain in the same page. But , if the action returns the afore mentioned string , the page will just be not navigated..it will not be reloaded or refreshed..then how can i use window.onload here ?
If not window.onload , then is there any other way of doing it ? One point to be remembered is that, the action is evaluated at java side.
Any suggestions will be deeply appreciated.
I am creating a web application in Java. I created some JSP pages each having some form fields. All are post method type so it hides all the form fields. In each page it will call servlet and forward to next JSP page(like a step by step process.)) Welcome page is index.jsp. In the last JSP page also I am having form field which is also post method type. When I press sumbit button, it will call servlet and should forwartd to home page(That is index.jsp).
Last page action value is finish. In my servlet, I am using RequestDispatcher and forwarding to index.jsp. The the URL will be
http://localhost:8080/myproject/finish.
As it is the home page, I wanted to hide that action value. So instead of RequestDispatcher I used
response.sendRedirect("index.jsp"); And then URL becomes
http://localhost:8080/myproject/index.jsp.
This is not a big issue. But still I am asking is there anyway to hide this index.jsp in the URL? It should be like when we open the site for the first time(http://localhost:8080/myproject/).
I got the answer finally. Thanks to #Sayem Ahmed for his reply as comments.
I tried this only
response.sendRedirect("/myproject");
In the base template for the pages of my Wicket application, there's a form I don't want Wicket to handle, like this:
<form id="myForm" action="">
<!-- input fields and submit button -->
</form>
I left the action attribute empty to always send it to the current page. On the application's main page, it works, but on other pages, Wicket adds a "../" in the action attribute, which seems to be meant well but is not what I want.
I'm using Wicket 1.4.17. How can I stop or change this behaviour?
The form is meant to enable the user to submit a short message as feedback to the site admin. It appears on every page and the input is collected from the PageParameters in the constructor of my pages' base class. If there is a more Wicket way to do this, I'll appreciate hints, but this should be a) stateless and b) very very simple.
I'd go the Wicket way and write a component for your feedback form which is then inserted into every page. As you have an (abstract) base class for all of your pages, you can simply add it there and it will appear on every page.
In your feedback form component, simply overwrite the onSubmit() method and send the message to the site admin.
Here is my situation: the user selects a section (for example from a dropdown) such as "Section1," "Section2" or "Section3." Then he clicks the OK button (or some link).
What I need to happen: after he clicks on that button/link, he will be redirected to the selected section, e.g. www.homepage.com/docs#section2.
So far, I have not been able to process the form from Link's onClick method, nor have I been able to call some clickLink on Link from the Button method onSubmit().
I would prefer not to use AJAX or JavaScript. How can I do this?
That's because a Link doesn't submit the form. It just acts as a link to somewhere. To access your formdata you'll need to submit the form first. Try using a SubmitLink instead of a Link and call
getRequestCycle().setRequestTarget
(new RedirectRequestTarget("www.homepage.com/docs#section2"));
from the onSubmit function of the SubmitLink.
Judging from the Javadoc this should work but I can't test it right now.
A RequestTarget that will send a redirect url to the browser. Use this if you
want to direct the browser to some external URL, like Google etc, immediately.
Or if you want to redirect to a Wicket page. If you want to redirect with a
delay the RedirectPage will do a meta tag redirect with a delay.
Did you try Link.setAnchor(Component)?