Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem redirecting page servlet to jsp  RSS feed

 
Dinesh Ravi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"))
 
Dinesh Ravi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was used this code also getting same problem if (msg.equals("error")). the page was navigated but the url showing servlet page url.
 
Dave Tolls
Ranch Hand
Posts: 2826
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dinesh Ravi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Dave Tolls
Ranch Hand
Posts: 2826
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dinesh Ravi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know what is the problem.
 
Dave Tolls
Ranch Hand
Posts: 2826
30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66142
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!