Link to home
Start Free TrialLog in
Avatar of prasad2000
prasad2000

asked on

Servlet Redirection.


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
   //Invoke the otherservlet and pass the requestobject as an argument to it
   //for ex invoke a servlet called MyServlet(request).
 }


can u please put the relevant code needed in the place of comments in the above code.
Thx in advance
Avatar of palandre
palandre

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
  javax.servlet.http.HttpSession session = request.getSession();
  javax.servlet.ServletContext aContext = session.getServletContext();
  javax.servlet.RequestDispatcher rd  = aContext.getRequestDispatcher(yourSevletPath);
  rd.forward(request, response);
}
Avatar of prasad2000

ASKER

Also
I want to invoke Myservlet as an HTTPS request.
That means
http://servlet/MyServlet(request)
The above request should be made

sorry in the above comments https is missing
https://servlet/MyServlet(request)
Avatar of Mick Barry
you may need to try a redirect then:

response.sendRedirect("https://servlet/MyServlet");
will try that option and let u know if it works
Objects..
ur code invokes Myservlet but I also want my requestobject to be passed to Myservlet..
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
 javax.servlet.http.HttpSession session = request.getSession();
 javax.servlet.ServletContext aContext = session.getServletContext();
 javax.servlet.RequestDispatcher rd  = aContext.getRequestDispatcher("https://servlet/MyServlet");
 rd.forward(request, response);
}

doesn't it work?
> but I also want my requestobject to be passed to
> Myservlet..

don't think u can do it if you want to switch to https.
the browser needs to resend the request.

Perhaps you could store the relevant parts of the request in the session, or add then to the request url.

just try to user

response.sendRedirect("https://yourserver/servlet/MyServlet?" + request.getQueryString()):

this won't work when using multipart forms but for simple parameters it should work out.
am not able to compile the above code.
getting the error in the below line.
javax.servlet.ServletContext aContext = session.getServletContext();

There is no getServletContext() method in the session object.


public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
javax.servlet.RequestDispatcher rd  = request.getRequestDispatcher("https://servlet/MyServlet");
rd.forward(request, response);
}

I tried the above code.
and am getting the below exception when i run the program
ServletInstan X Uncaught service() exception thrown by servlet {0}: {1}
                                 "frontservlet"
                                 javax.servlet.ServletException: javax.servlet.ServletRequest: method getRequestDispatcher(Ljava/lang/String;)Ljavax/servlet/RequestDispatcher; not found
     at javax.servlet.ServletException.<init>(ServletException.java:73)
     at
ASKER CERTIFIED SOLUTION
Avatar of squeezeMe
squeezeMe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
squeezeMe> I already mentioned trying that.
are you sure ...


as far as i understood you were trying to accomplish this using the RequestDispatcher, but what you want won't be working with the RequestDispatcher... you're leaving the ServletContext when you want to call that https resource.

anyway, this is what I thought you tried:

javax.servlet.RequestDispatcher rd  = request.getRequestDispatcher("https://servlet/MyServlet");
rd.forward(request, response);

this is what i suggested:
don't use the ServletContext not the RequestDispatcher...
just use response.sendRedirect...

response.sendRedirect("https://www.microsoft.com?" + request.getQueryString());


i testest my solution on tomcat 4.x and orion successfully.
which server are you using. the error you're getting is quite strange...
so either i dont understand your problem, or you did not try my solution and mixed it up with some ServletContext and RequestDispatcher action...

cheers
Comment from objects  12/11/2002 02:19AM PST  
> but I also want my requestobject to be passed to
> Myservlet..

don't think u can do it if you want to switch to https.
the browser needs to resend the request.

Perhaps you could store the relevant parts of the request in the session, or add then to the request url.
 
Squuezeme
I tried ur code..Its workiing fine..
Will close the question a little later
Objects,
I couldnt try using request dispatcher because it is giving an error in my websphere.
I have seen the jsdk jar in the lib dir of websphere.
It doestnt have any RequestDispatcher class in it..

(Websphere 3.5)
> I couldnt try using request dispatcher

For the second time, I never mentioned anything about RequestDispatcher.
What I suggested was exactly what Squeezeme said, though I suggested first :-)

Comment from objects  12/11/2002 02:19AM PST  
> but I also want my requestobject to be passed to
> Myservlet..

don't think u can do it if you want to switch to https.
the browser needs to resend the request.

Perhaps you could store the relevant parts of the request in the session, or add then to the request url.
would like to give 50 ppints each to squuezeme and objects.
how to do that???
prasad2000

You asked to split points between objects and squuezme.

I have reduced the points on this question from 100 to 50 as indicated by your request at Community Support. Please copy the URL and create a new question in this topic area for the other Experts to whom you wish to award points. The title of the question should read "Points for", followed by the Expert's name. In the question itself, you should paste the link to the original question and perhaps a comment stating that the points are for their help with that question. Once you have created the new questions, you can go back to the original, and accept the comment from the Expert for whom you did not create a new question. The Experts will  comment in your new "Points for" question(s), which you then accept and grade to close.
If you have any questions, please don't hesitate to ask.
Thank you.

** Mindphaser - Community Support Moderator **