I have a servlet which can get requests by multiple JSP's.
But when I use the RequestDispatcher in the servlet, I don't know how to forward to the JSP that sent the request.
req.getRequestDispatcher("page.jsp").forward(req, resp);
I know there is something like this in html: javascript:javascript:history.go(-1)
I just need something like this:
req.setAttribute("originalRequest", req.getRequestPage());
req.getRequestDispatcher(originalRequest).forward(req, resp);
That piece of code is probably very noob but it gives you the idea of what I need.
So: I need to forward to the page that sent the original request (basically reload the page), but because multiple jsp's use the servlet, I cannot simply forward to "page.jsp"
You can do following
Create a hidden parameter for every jsp named jspName and give value for respective JSPs. e.g. for JSP A, parameter name is jspName and value is a, for JSP B, parameter name is jspName and value is b
Read this hidden parameter in the servlet using following code.
String jspName = request.getParameter("jspName");
RequestDispatcher rd = request.getRequestDispatcher(jspName);
rd.forward(request, response);
When you are calling the servlet from JSP A, then it will have paramter japName=a, when servlet code is running, it will retrieve the value a from request.getParamter("jspName") and a getRequestDispatcher(jspName) will create the dispatcher for the same and rd.forward(request, response) will forward to the jsp.
I have not tried the following, but I hope this may help you to solve your problem.
req.setAttribute("originalRequest", req.getRequestPage());
req.getRequestDispatcher(req.getAttribute(originalRequest).toString()).forward(req, resp);
I have tried in this way, It's working for me...
In JSP
You need to set this in every JSP. For ex: index.jsp is my jsp name.
<% session.setAttribute("jspName","index.jsp"); %>
In Servlet
request.getRequestDispatcher(session.getAttribute("jspName").toString()).forward(req, res);
Related
Yes, I know that this is for sure duplicate question.
But... I have read all of answers for earlier asked questions, tried to fix everything... and my code still doesn't work.
In index.jsp:
<form action="/Login"method="post">
and
<input type="submit" value="LogIn">
In post method of servlet "/Login":
if (result == 1) {
RequestDispatcher view=request.getRequestDispatcher(url);
view.forward(request, response);
} else {
response.sendRedirect("index.jsp");
return;
}
I was trying with:
"http://localhost:8080/home.jsp",
"/home.jsp",
"home.jsp"
response.sendRedirect
trying to do with and without "return;".
And nothing.
This what I saw is only blank page and:
If doPost()
method: url is
"http://localhost:8080/Login".
If doGet()
method: url is the same as abovementioned plus parameters input into a form.
Could you help me?
If you want to redirect user to another page with a brand new request then using sendRedirect function, but you must send redirect to the servlet, servlet will process and return to you a page. You can not sendRedirect directly to the jsp page (you send redirect to a jsp page, it's like you right click on jsp page and run it on server). Please refer to this post to see the use of sendRedirect method.
I have a Servlet, which sends values to a JSP1. I am setting the values in the servlet as
request.setAttribute();
and i am using
dispatcher.forward(request, response);
and i am able to access these values in the JSP1 using expression language.
Now, I also need to use this in another JSP2.
These two JSP's are clubbed together to display in a single page. So, I need to access the values of the servlet in the second jsp as well. How can i do that?
As you say you have both the jsp clubbed in a single page , you can simply get the attribute as,
${attributeName}
it refers to the object set in the HttpServletRequest scope . I suggest to take the tutorial for Expression language
It is quite simple. If you are setting an attribute "attr1" in a servlet and then forwarding to a JSP (file1.jsp):
request.setAttribute("attr1", "first attr");
request.getRequestDispatcher("file1.jsp").forward(request, response);
And suppose in the first JSP (file1.jsp), you are including another JSP (say file2.jsp):
<body>
.....
<jsp:include page="file2.jsp"></jsp:include>
.....
</body>
Then in file2.jsp, you can access attr1 by calling the request object's getAttribute method:
<body>
....
${attr1}
....
</body>
In case of jsp:include action, when file2.jsp is included in file1.jsp, both request and response objects are passed to file2.jsp as parameters. So file2.jsp can access the attributes of file1.jsp's request object.
Is it possible to pass value of variable from Servlet in to JSP. Say i have a JSP something like welcome.jsp and I have some variable initialized in welcome.java servlet.
When I run welcome.jsp it should take value from welcome.java and display it on page.
When I set the RequestDispatcher and forward it its still going to show the servlet URL.But I want the URL to be JSP file.But the Variable I display in JSP file should be taken from servlet.
You can archive it using session attributes.
.java
request.getSession().setAttribute("attrName", "value");
request is instance of javax.servlet.http.HttpServletRequest
.jsp
${attrName}
To get more about session attributes, read docs. You can set attribute of any type.
request.getSession().setAttribute("attrName", new ArrayList());
You can put the variable in Session and redirect the user to welcome.jsp page.
How can I send my response and request object from a jsp file to a servlet by code? I don't want to submit a form or so.
I tried it with:
response.setRedirect("my page"):
But then it says:
Exception in thread "main" org.apache.http.client.HttpResponseException: Moved Temporarily
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:68)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:945)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:919)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:910)
at com.xx.xx.client.Client.sendPOSTRequest(Client.java:185)
at com.xx.xx.client.Client.main(Client.java:46)
As clarification: I have a client that sends a post request to a JSP file. This JSP file parses a file and puts the needed information into the session. I want to call a servlet from this jsp file to add something into the database. I think this error code is thrown by this line String responseBody = httpclient.execute(httppost, responseHandler);
You can just use <jsp:include> on a servlet URL.
<jsp:include page="/servletURL" />
The servlet doXxx() method will just be invoked with the current request/response. Note that the servlet cannot forward to another JSP afterwards. It has to write directly to the response, or to set some request/session attributes which the JSP can intercept on after the <jsp:include> line.
Note that this is bad design. You're abusing a JSP as a front controller. It should be the other way round. The servlet should act as a front controller and the JSP should act as a view. The client should send the request to the servlet URL directly instead of to some JSP file. The servlet should perform the business job and finally forward to a JSP to let it present the results in HTML. See also our servlets tag wiki page for some Hello World examples.
you can use the RequestDispatcher forward(ServletRequest request, ServletResponse response)
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
You can do it like this:
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/YourServlet");
rd.forward(request, response);
UPDATE
Also note on your code that you have response.setRedirect instead of response.sendRedirect(...) but note that this method will not work as you want it to because it just asks the browser to make a new request to your servlet rather than forward your request and response object to that servlet . See RequestDispatcher.forward() vs HttpServletResponse.sendRedirect() for more info
in how many way can we redirect to next jsp page from one page to another page apart from following two ways,
RequestDispatcher rd = request.getRequestDispatcher("MyPage.jsp");
rd.forward(request, response);
and
response.sendRedirect("MyPage.jsp");
when i go to second page(MyPage.jsp) it has to load as fresh page from server side,
RequestDispatcher and sendRedirect are different things. If you want to
load as fresh page from server side,
then RequestDispatcher won't work. The client (browser) still thinks the content comes from the original request.
Take from JGuru:
http://www.jguru.com/faq/view.jsp?EID=376&page=2
You can also physically alter the Location HTTP header attribute, as shown below:
<%
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
String newLocn = "/newpath/index.html";
response.setHeader("Location",newLocn);
%>
Redirect has two types permanent and temporary redirect.