Servlet Redirection.

prasad2000
prasad2000 used Ask the Experts™
on

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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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);
}

Author

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

Author

Commented:
sorry in the above comments https is missing
https://servlet/MyServlet(request)
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Mick BarryJava Developer
Top Expert 2010

Commented:
you may need to try a redirect then:

response.sendRedirect("https://servlet/MyServlet");

Author

Commented:
will try that option and let u know if it works

Author

Commented:
Objects..
ur code invokes Myservlet but I also want my requestobject to be passed to Myservlet..

Commented:
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?
Mick BarryJava Developer
Top Expert 2010

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

Author

Commented:
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
okay i took some time to get you the code :

you cannot use the RequestDispatcher if you want to leave the Servlets request contexts (which happens if you want to call another external resource)

calling
http://localhost:8081/servlet/RedirectingServlet?test=1

will lead you to
https://www.microsoft.com/?test=1

here's the code:

---------------------------
import javax.servlet.*;
import javax.servlet.http.*;

public class RedirectingServlet extends HttpServlet {
   
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
       
    }
   
    /** Destroys the servlet.
     */
    public void destroy() {
       
    }
   
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        System.out.println("QueryString is : " + request.getQueryString());
        response.sendRedirect("https://www.microsoft.com?" + request.getQueryString());
    }
   
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, java.io.IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
   
}
Mick BarryJava Developer
Top Expert 2010

Commented:
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
Mick BarryJava Developer
Top Expert 2010

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

Author

Commented:
Squuezeme
I tried ur code..Its workiing fine..
Will close the question a little later

Author

Commented:
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)
Mick BarryJava Developer
Top Expert 2010

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

Author

Commented:
would like to give 50 ppints each to squuezeme and objects.
how to do that???
Mick BarryJava Developer
Top Expert 2010

Commented:
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 **

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial