2

This code snippet is from my CustomerController servlet.

@WebServlet("/CustomerController")
/*
.
.
.
*/

if(request.getParameter("operation").equalsIgnoreCase("search-customer")) {

            CustomerDAO customerDAO = new CustomerDAO();
            Customer customer = customerDAO.searchCustomer(Integer.parseInt(request.getParameter("customer_id")));
            request.setAttribute("Customer-Result", customer);
            RequestDispatcher requestDispatcher;
            requestDispatcher = request.getRequestDispatcher("/search-results.jsp");
            requestDispatcher.forward(request, response);
}


Instead of redirecting to search-results.jsp so as to print the result of the search, the results are printed on the servlet's (CustomerController) url itself.

Refer to the image. CustomerController


search-results.jsp

<%@page import="com.servlet.Customer"%>
<!DOCTYPE html>
<html>

    <body>


    Customer Found : 

    <%

    Customer customer = (Customer) request.getAttribute("Customer-Result");
    out.println(customer.getCustomer_name());

    %>


    </body>


</html>

What is wrong ?

  • you have out.println(customer.getCustomer_name()); so this is printing – SpringLearner Apr 27 '16 at 12:03
  • 1
    this is the intended behavior of `requestDispatcher.forward` if you want a client side redirection use `sendRedirect`. – awd Apr 27 '16 at 12:11
  • @awadheshv - What do I need to if I want to display the results on the url `search-results.jsp` ? –  Apr 27 '16 at 12:12

2 Answers2

0

@Maven Maverick. Hope this can help you.

If you want your search-results.jsp in your URL remove that file from your WEB-INF folder and deploy it under WebContent folder(IDE:Eclipse). Because WEB-INF is protected folder its not accessed directly by user call or url. For that you have to move that jsp file under WebContent folder and instead of RequestDispatcher use response.sendRedirect("//test.jsp");return;

Comment If this works for you or not .. because works for me.

Hope it works for you :)

Darshit
  • 361
  • 2
  • 13
  • 1
    Hmm, it will work, but you are advising OP to use bad practices without warning him of it. – Serge Ballesta Apr 27 '16 at 13:47
  • 1
    My suggestion had two parts - one was to edit the post to be an answer. An answer wouldn't need to say "Sorry for not commenting...". My comment was constructive - no need to flag it. – Mogsdad Apr 30 '16 at 13:22
0

You do not want that!

The URL displayed in the browser is the one for the last query sent by the browser. In your case, you send a query to /CustomerController and server side, the servlet forwarded the request to a JSP to display results. That forward is an internal detail in a web application, and you have no reason to show that to the client.

More, you should not let the client know about that. Common usage is to put the JSP used internally by the servlets (via includes or forwards) under the WEB-INF folder precisely to avoid direct queries from the client. What would be the sense to ask the browser to display an URL that it cannot query? The interesting part here is that if you later change from JSP to a template engine like velocity, as that part is hidden from client, nothing will change in the visible interface

I'm not sure that this is the expected answer but you really should wonder why you want to display an internal URL in the client browser. As that will require that you put the JSP outside of the protected WEB-INF folder, it would be definitely a bad practice.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252