Forums Register Login

Problem redirecting page servlet to jsp

+Pie Number of slices to send: Send
i have jsp page that page have one form that get the department name and if form was submit to call the servlet page like addDept.java and here to check if department name already in database or not if already have give (a alert box usng jquery)after redirecting jsp page.

after database checking this code will executed
the jsp page was displayed but the url is servlet url.

servlet page:
RequestDispatcher rd = request.getRequestDispatcher("/AddDept.jsp");
request.setAttribute("msg", "error");
rd.forward(request, response);

jsp page:
after body section

<% String msg=(String)request.getAttribute("msg");


if(msg=="success"){ %>
<div><script> success();</script></div>
<%} else if(msg=="error"){ out.println("inside"); %>
<div><script lang="javascript">success();</script></div>
<%} %>

my problem is the alert box is showing but url is servlet example: http://localhost:8084/project/addDept => actual url is http://localhost:8084/project/AddDept.jsp

thanks
+Pie Number of slices to send: Send
 

Dinesh Ravi wrote:
my problem is the alert box is showing but url is servlet example: http://localhost:8084/project/addDept => actual url is http://localhost:8084/project/AddDept.jsp


Why is that a problem? That's normal behavior when using a RequestDispatcher; the url will remain the url of the servlet. And your jsp should be in a folder under WEB-INF so it can't be called directly.

And you need to get rid of that scriplet code. That's not the correct way to build a web application. I suggest you start by reading this.
+Pie Number of slices to send: Send
You might also take note of the fact that string comparisions must be done with the .equals() method.

if(msg=="error") --> if (msg.equals("error"))
+Pie Number of slices to send: Send
i was used this code also getting same problem if (msg.equals("error")). the page was navigated but the url showing servlet page url.
+Pie Number of slices to send: Send
 

Dinesh Ravi wrote:i was used this code also getting same problem if (msg.equals("error")). the page was navigated but the url showing servlet page url.



Well, first off, you are forwarding, not redirecting.
With a forward it is all done inside the server, so as far as the client is concerned there is no change in the URL (which is correct, there isn't).
With a redirect the client is told that they need to request a different "page", so the URL will change.
+Pie Number of slices to send: Send

Thank you,

like i have AddDept.jsp the user to enter department name and if submit the form to navigate to addDept.java(servlet) page. the servlet to check if the entered value already in database or not if already have to navigate to AddDept.jsp page with setAttribute("name","value"); jsp page can check the value and show the jquery alert box. This is my process flow.

+Pie Number of slices to send: Send
 

Dinesh Ravi wrote:
Thank you,

like i have AddDept.jsp the user to enter department name and if submit the form to navigate to addDept.java(servlet) page. the servlet to check if the entered value already in database or not if already have to navigate to AddDept.jsp page with setAttribute("name","value"); jsp page can check the value and show the jquery alert box. This is my process flow.



OK, so why is this a problem?
+Pie Number of slices to send: Send
i don't know what is the problem.
+Pie Number of slices to send: Send
 

Dinesh Ravi wrote:i don't know what is the problem.




I'm asking why the URL not changing is a problem?

Or is there another problem?
+Pie Number of slices to send: Send
Just to make it really clear: the URL will not change. This is the way it is supposed to work.

Why do you want it to work differently?
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1084 times.
Similar Threads
Display Message when I insert or update record
Peculiar problem with js and css file
how to keep register page data if register fails
Servlet Response
Same Request Calling again
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:44:35.