• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call servlet from java class and redirect to jsp page

 
limitha adulla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I am very new to this site.

just want to call servelt from java class where servelt redirectd to jsp page.

I have tried to call servlet from java class using following code

URL url = new URL("http://localhost:9090/servlet");

URLConnection conn = url.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

but i am redirecting servlet to jsp page which i can't do it. Can please suggest me how to do it.
 
carina caoor
Ranch Hand
Posts: 300
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To dispatch a request from servlet to JSP you can use RequestDispatcher.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RequestDispatcher does a server-side forward, not a client-side redirect.

You can cast the URLConnection object to an HttpURLConnection, and then use the "setFollowRedirects(true)" method.
 
limitha adulla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ulf Dittmer

Can please send me detailed code where to use setFollowRedirects(true) method i have tried but it doesn't work.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried, and what happened? Post the code.
 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that following redirects is the default behaviour for HttpURLConnection, isn't it? So the problem may be that the servlet isn't designed to redirect to the JSP. If that's the case then your client-side component (the URLConnection) can't affect how the server side is designed to work. But you could follow up by sending a second request to the JSP.

That's speculation, though. We don't know how things are supposed to work on the server, we don't know what you are doing on the client, and we don't know what is happening that shouldn't be happening. Or vice versa.
 
limitha adulla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ulf Dittmer

can please tell me how to use setFollowRedirects(true) method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic