1

i am using a servlet to bring in a login page for my web application.but once i login and click on "submit" button,i should be redirected to another page. how do i do this? i am currently trying to use

RequestDispatcher rd = null;
rd = getServletContext().getRequestDispatcher("/NextServlet");
rd.forward(request, response);

Also tried using

response.sendRedirect("http://localhost:8080/myProject/NextServlet");

but nothing happens on click of submit, I do not want to include a link with href and use. What should I do?

5
  • Are you trying to see a java server page or the content of a servlet? If you want to access to this NextServlet, at least make sure this servlet prints any HTML code or forward/redirects to a JSP. Commented Nov 29, 2012 at 5:31
  • i have updated the answer.. please check Commented Nov 29, 2012 at 6:24
  • have you mentioned this path in your web.xml? Commented Nov 29, 2012 at 6:28
  • yes i have included a simple hhtml code in my NextServlet and i have in mentioned the following in my web.xml <servlet> <servlet-name>RequestParamExample</servlet-name> <servlet-class>com.xyz.RequestParamExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>RequestParamExample</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> Commented Nov 29, 2012 at 10:37
  • RequestParamExample is the servlet where i have included dispatcher as said in the first post Commented Nov 29, 2012 at 10:38

3 Answers 3

1

try this

response.sendRedirect("NextServlet");
Sign up to request clarification or add additional context in comments.

32 Comments

path itself contains context path, i have only given an alternative to request dispatcher
Not necessarily, and you even need to use Response#encodeRedirectURL as shown here: Servlet: URL redirect.
it is required when you need to encode your url, otherwise you can directly send to that url
let us taken an example of any url... response.sendRedirect("google.com"); ... try it
Ok, what about path = "somePage.jsp";?
|
0

I think you need to add contextPath in your redirect URL as :

  String destinationURL = request.getContextPath()+"/NextServlet";
  RequestDispatcher rd = getServletContext().getRequestDispatcher(destinationURL);
  rd.forward(request, response);

6 Comments

@blackhole When you say "not working", what is happening? Are you getting some message/error? Also did you make sure, its reaching up to this code as I suspect the problem is somewhere else?
i am not redirected to the page, instead it stays in the same page,but the username and password fields reset. i dont have any errors also.i tried increasing my log4j error logging, but not able to trace
I suspect, its not reaching up to there. Can you share little more of your code?
i am now getting an error as below: Exception in thread "http-bio-8080-exec-4" java.lang.StackOverflowError at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:219) at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228) at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228) at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228) at org.apache.catalina.core.ApplicationHttpRequest.getAttribute(ApplicationHttpRequest.java:228)
@blackhole Can you look litte down in the stack trace and see which line of your code(it will have name of one of your classes) is cause this over flow error?
|
0

Have you set the "action" of your form in html?

<form action="your_jsp_script.jsp" method="post">

3 Comments

yes @sifeng, i have mentioned the action,but i have linked it to call a html page and not a jsp page!am i wrong there?
the script in "<form action=...>" will be executed when you click the submit button. If it is just a html page, nothing will happen.
hi,once i have changed to <form action="WelcomePage.jsp" method=POST>, i am redirected to some page but, its empty and only the out.prinltn() statements are visible...my jsp file is as below : <%@ page info="request param example" %> <html> <head><title>iPTS</title></head> <body bgcolor="#ffffff" background="background.gif"> <%@ include file="WelcomePage.html" %> <table> <tr> <td width=150> &nbsp; </td> <td width=250 align=right> <h1>Welcome to iPTS!</h1> </td> </tr> </table> </body> </html> again,where am i wrong?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.