Hide form action value in the URL in JSP - java

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

Related

How to change the url after forwarding a servlet to jsp page?

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.

how post entire page in GWT form panel

While using GWT FormPanel, after submitting the form, it posts form but does not redirect to the action url.
Can any body help me?
formPanelObject.getElement().<FormElement>cast().setTarget("");
by this line you changing the target parameter of the form and now the main page redirected to the action url after calling formPanelObject.submit();.

Spring MVC redirect: new page is opened in div

Here is a problem I have encountered.
I have jsp page which has a javascript openForm function. This function fetches data from server URL that relates to dialog.jsp and inserts that code into the body of my page. I.e. it looks like
function showDialog(url){
$.get(url,function(data){
$("body").append(data);
})
}
OK, but after user deals with that dialog I want to redirect him to some better place. I use return createRedirectModelAndView(URL_CONSTANT) in the controller, responsible for handling POST from that dialog.
It works, but here is a problem: a new page (redirect target) is opened INSIDE that dialog div. So the parent page (on which user clicks showDialog) still exists and it looks like the redirect is redirecting user inside dialog. This effect can be cool in some situations, but not in mine. I want total redirect - close that page and open another.
Where I went wrong?
If you want to redirect from an Ajax call then you should return the redirect URL from the server and do the redirect/location change from JavaScript.

after back button, portlet does not go in to processaction

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...

prevent duplicate submission of forms on jsp pages

Hi all
I've got some jsp pages and im using struts to handle my forms.
After submitting a form by user, the url shown in address bar becomes somthing.action, so when the user refreshes the page, the forms gets submitted again.
How can I handle this?
after submission of a form, Is there any possible way to show a ".jsp" url instead of a ".action" in the address bar?
Yes, use redirect-after-post. Either response.sendRedirect("foo.jsp"), or see here or here (depending on what exactly your code is).
If you are using Struts 2, it has a Token interceptor, to prevent duplicate form submissions -
http://struts.apache.org/2.1.2/struts2-core/apidocs/org/apache/struts2/interceptor/TokenInterceptor.html
And an example: https://cwiki.apache.org/WW/token-interceptor.html
If you redirect user to some page (it can by same as form) after you make action, it will not send post data if page will by refreshed.

Categories