• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to change url to jsp name after passing to doPost() method?  RSS feed

 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a log in page accessed by: localhost/Security/Login.jsp

When I click a log in button,
doPost() method is executed from Login.java and displays the contents from main.jsp. However,its url on my browser is localhost/Security/Login.
Generally, the pattern is the address of Login servlet.

So if I click a button from main.jsp, error occurs because it is trying to locate a servlet from /Security/[Servlet name].

What I want is to change the url as I go to main.jsp to localhost/Home/main.jsp.


Is this possible?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things to note: Firstly, it sounds as if you're doing a forward (or include) to the JSP. Using a redirect instead lets you have a different URL, and it also avoids the "reload" problem; it's called the PRG pattern.

Secondly, it sounds as if you're using page-relative paths when you should be using server-relative paths. See http://www.coderanch.com/how-to/java/TypesOfPaths, and get in the habit of using the contextPath in your JSPs. http://www.coderanch.com/how-to/java/RelativeLinks has the details.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:What I want is to change the url as I go to main.jsp to localhost/Home/main.jsp.

Why? You don't really want to do this as it will cause problems if the page is refreshed or the back button is later used.

What you want to do is to use the PRG pattern as explained in this article.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your replies. With regards to PRG pattern, is there a way to make 1 servlet to handle post and get methods?

Because after reading about the pattern, it uses 2 servlets/controller for each JSP page: 1 servlet for task controller (which has dopost() and do get() operation) and 1 servlet for page controller (1 get method)

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:With regards to PRG pattern, is there a way to make 1 servlet to handle post and get methods?

Yes, but again, why would you do that? It's a common mistake to have a GET and a POST do the same thing. Bad idea, and violates the very basic premises of HTTP.

Because after reading about the pattern, it uses 2 servlets/controller for each JSP page: 1 servlet for task controller (which has dopost() and do get() operation) and 1 servlet for page controller (1 get method)

Those do not need to be handled by the same servlet; in fact, it'd be strange to do so. The task controller is rarely that tightly bound to the page controller.
 
Winston Liek
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your response.

I have another question. Should the PRG pattern be applied to all jsp pages, meaning all of my jsp pages should have 2 servlets? I am using MVC2 framework.

So far I fixed my Log in to Main page url using sendRedirect() instead of forward().

However, I have another problem. On my main.jsp, I have a search functionality.

Initially, its url is localhost/Main/Home.jsp. However, if I click the search button and returns a result, the url becomes localhost/Main/SearchServlet where SearchServlet is the value of action associated with the form.

What I want is to retain localhost/Main/Home.jsp after returning the result. I tried to use sendRedirect() to Home.jsp but the page "refreshes" not displaying the result.


Here is my web.xml


And BTW, what does url-pattern mean? And also, should the value of url-pattern must match the value during forward() or sendredirect()?

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Liek wrote:I have another question. Should the PRG pattern be applied to all jsp pages, meaning all of my jsp pages should have 2 servlets?

You are thinking about it slightly wrongly. Task controllers do not "belong" to JSPs. Task controllers correspond to things that need to be done in a web app -- the "task". As such, they re independent from JSPs. It's easy to imagine that a task might be triggered from multiple places in a web app. By making task controllers independent, they can be reused in many places within the web app.

Each JSP should have a page controller that prepares any data that the page needs in order to display.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!