Help with two servlet questions

  • Thread starter Miss Michelle. Heigardt
  • Start date
M

Miss Michelle. Heigardt

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
 
V

Venky

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/
 
O

ozgwei

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,191
Latest member
BuyKetoBeez

Latest Threads

Top