Display HTML page after servlet is finished - java

I have a Servlet which accept username and password and authenticates it.
After it is done, I want to display an HTML page.
It is a big HTML page so I read that using writer.println() was not a good idea.
How do I display the new page?
I tried using sendRedirect but that, for some reason, displayed the same page (login page) again.
Please help.

Create a JSP page contains your HTML, then use requestDispatcher to forward the request.
for Example:
if you created a JSP page named MyJSP, then in login Servlet use:
request.getRequestDispatcher("MyJSP.jsp").forward(request, response);

you can use this lines of code
create an html whatever the name you like i had created LoggedIn.html and
use this code in your servlet
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/LoggedIn.html");
rd.forward(request, response);

Related

Redirecting the page with Servlets Java

I have a jsp page with a form that when it’s submitted goes to a Servlet that inserts the form data into the database.
When the data is inserted into the database, I’m trying to get the browser back to my jsp page and show a javascript alert saying that the data was inserted successfully, my code is the following:
RequestDispatcher rd;
if(dao.insertClient(client)) {
rd = getServletContext().getRequestDispatcher("/pages/clients.jsp");
rd.include(request, response);
out.print(
"<script type=\"text/javascript\">"
+ "alert("Client inserted successfully!");"+
"</script>"
);
}
This code is doing exactly what I want, but this method getRequestDispatcher() redirects the page to the servlet itself, and the URL is like http://localhost:8080/Servlet, this way I can’t access any intern link of the page, since the links to the other pages obviously are outside of the servlet context, and the glassfish returns the 404 error.
Instead of using getRequestDispatcher(), I’ve tried using the response.sendRedirect(), this way I can insert the data into the database and access the intern links, but the javascript alert isn’t shown.
Somebody has a suggestion on how I can redirect the page to the clients.jsp and display the javascript alert?
Thanks!
you can try another approach :
Set parameter from servlet like this :
RequestDispatcher rd;
if(dao.insertClient(client)) {
rd = getServletContext().getRequestDispatcher("/pages/clients.jsp");
request.setAttribute("isSuccess", "success");
rd.include(request, response);
}
access the parameter in jsp to check whether to show alert or not.
<%
String result = request.getParameter("isSucess");
if("success".equals(result)){
%>
<script type="text/javascript" >
alert("Client inserted successfully!");
</script>
<%
}
%>

return from servlet to jsp to specific id in index page

I am working on java servlet project
I have index.jsp page that take input from user and send it withe post method to readXml servlet page that process the input then return the result to index.jsp
I wont to return to specific id in index.jsp
I used this code for foreword response to index.jsp
request.setAttribute("message", span );
request.setAttribute("Sent", input );
request.getRequestDispatcher("/indexjsp.jsp#color").forward(request, response);
request.getRequestDispatcher("/indexjsp.jsp#color").forward(request, response);
when I use this code I get resource error
and when I remove #color it will return the response to index.jsp but not with id I wont.
are there any way to return to specific id from servlet?
DO it something like this
instead of using #color try ?id=color.
Populate this parameter in the hidden input tag on 'index.jsp'. Now you have your tag id on client side. you can do what you want using javascript after onload event triggers.

Is there a way for requestdispatcher forward method show url of landing page

Is there a way for Requestdispatcher forward method show URL of landing page
RequestDispatcher r = request.RequestDispatcher("error.jsp");
r.forward(request,response);
Does not show URL error.jsp
No, It'n not. URL won't change with forward.
Use sendRedirect to see the url change in browser.
response.sendRedirect("error.jsp");

How to serve existing web page using a java servlet

I've been through a few tutorials for java servlets and they all show how to display a web page generating in the java code using a servlet. How can I display an existing html page using a servlet?
I figure I need to do something with HttpServletRequest.getRequestDispatcher but not sure exactly what?
you can do this in two ways:
Request dispatcher
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/somePage.html");
rd.forward(request, response);
Response sendRedirect()
response.sendRedirect("/someUrl.html");
See the difference between the two methods here: RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()

Hide form action value in the URL in JSP

I am creating a web application in Java. I created some JSP pages each having some form fields. All are post method type so it hides all the form fields. In each page it will call servlet and forward to next JSP page(like a step by step process.)) Welcome page is index.jsp. In the last JSP page also I am having form field which is also post method type. When I press sumbit button, it will call servlet and should forwartd to home page(That is index.jsp).
Last page action value is finish. In my servlet, I am using RequestDispatcher and forwarding to index.jsp. The the URL will be
http://localhost:8080/myproject/finish.
As it is the home page, I wanted to hide that action value. So instead of RequestDispatcher I used
response.sendRedirect("index.jsp"); And then URL becomes
http://localhost:8080/myproject/index.jsp.
This is not a big issue. But still I am asking is there anyway to hide this index.jsp in the URL? It should be like when we open the site for the first time(http://localhost:8080/myproject/).
I got the answer finally. Thanks to #Sayem Ahmed for his reply as comments.
I tried this only
response.sendRedirect("/myproject");

Categories