Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

Help with two servlet questions

Thread view: 
Miss Michelle. Heigardt - 29 Jan 2006 10:45 GMT
Hallo, I have two questions. I am new to Java programming in Servlets.

Question 1. I have a servlet that validates input from another servlet
"GetDetails" that generates a HTML page. If the stuff user entered is
correct I want to go to another page "ShowDetails". If the stuff the
user has entered is not correct I want to sent the page back to have
the information corrected. This is my code

public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException{
 RequestDispatcher dispatcher;

 if (session.getAttribute("password")==null){
  dispatcher=request.getRequestDispatcher("GetDetails");
 }
 else{dispatcher=request.getRequestDispatcher("ShowDetails");}
 dispatcher.forward(req,res);
}

"ErrorPage" and "ShowDetails" are servlets that create out more HTML.
When this code executes it seems to works but the browser URL does not
change. Therefore I am not doing this right so what is the right way?

Question 2. Rather that putting "GetDetails" and "ShowDetails" in lots
of servlet class, is there a tool to put them in somewhere else and
have them looked up. By a tool I mean the way people do this, not what
I could write.

Thank you
Michelle
Venky - 29 Jan 2006 11:50 GMT
Answer 1: There is nothing wrong in what you are doing. If you forward
your request to someother servlet, the url will not change, if you want
to change the url, then do redirect instead of forward. Read more about
these forward and redirect options.
http://www.javapractices.com/Topic181.cjp

Answer 2: May be its a good time for you to read about Struts
framework.. Just google it out.. Its pretty good and widely used
framework.. You will love it..  Start from here..
http://struts.apache.org/
ozgwei - 29 Jan 2006 11:57 GMT
Most don't work with servlets directly these days. Please consider
using a web MVC framework, such as Apache Struts Action
(http://struts.apache.org/) or Spring Framework's Web MVC
(http://www.springframework.org/).

Regarding your first question:
RequestDispatcher.forward(HttpServletRequest, HttpServletResponse) does
NOT change the URL of the browser. Therefore, your code is working
fine. If you prefer the URL to be changed, you should use
HttpServletResponse.sendRedirect(String url). When using redirection,
the response is sent back to the browser with a redirect URL, which the
browser will make another HTTP GET. Thus, all the parameters in the
original request that you want to use or examine in the next stage of
processing must be saved to the HttpSession object first.

The second question:
You can simply create a Helper class and put those methods in the
Helper class. Alternatively, put them in a base abstract servlet class
and let all your concrete servlets inheriting from this abstract class.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.