I have two jsp, let's say A.jsp and B.jsp. A.jsp has the following code:
<c:import url="B.jsp" >
<c:param name = "page_title" value = "Title" />
</c:import>
In B.jsp i need to check some conditions and do a redirection to Login.jsp. I achieve this by doing response.sendRedirect(Logn.jsp)
The problem is that the redirection is made on B.jsp so the result is that the browser displays the content of Login.jsp and A.jsp but i need to redirect the whole page to Login.jsp. That is, the browser should only show Login.jsp.
Consider that the redirection must be made on B.jsp unless there is a way that B.jsp can tell A.jsp the url to redirect to.
EDIT: A.jsp and B.jsp belong to different projects
In your B.jsp, instead of doing a redirect, set a flag on the request as
<c:set var="login" value="true" scope="request" />
Then in A.jsp check the flag and redirect if present.
<c:if test="${login == 'true'}">
<c:redirect url="/login.jsp" />
</c:if>
Try doing a jsp:include instead.
<jsp:include page="B.jsp" >
<jsp:param name="page_title" value="Title" />
</jsp:include>
There are two parts to solving this:
1) Making sure that you are not sending any HTML to the client in A until you have performed the logic in B. That means that you must call the B.jsp early (part of good MVC design).
2) Use the var parameter in your c:import:
<c:import url="B.jsp" var="output">
Now the output of the call to B will be in a buffer called "output" instead of being sent directly to the client. That allows you to make decisions in A (after the call to B). If you decide you want to redirect, you can, because you have dumped nothing (except some white space probably) to the browser. If you decide you want to send the output of B to the browser instead you can simply do this after the c:import:
${output}
There is even a trick to get rid of the white space being sent to the browser if that becomes necessary, but it rarely is (if you send too much white space the buffer will commit and then you cannot redirect, but that's rare and an entirely different question).
Make sense?
Related
I need to jump from first JSP page to second JSP page without using jsp:forward. The reason I want to do this is, whenever I refresh my second page I get a confirmation message asking "do you wish to re-submit form". Also the URL of the second page shows as first page's URL. Also I want this to happen without clicking anywhere.
Below is what I have did. I need to do something else:
<jsp:forward page="home.jsp">
<jsp:param value="<%=uid %>" name="uid1" />
<jsp:param value="<%=des %>" name="des" />
</jsp:forward>
Please help!
YES!! I did it.
I used
response.sendRedirect("home.jsp")
And for pass hidden paramerters, I created a session attribute in first page and took the values in second page using
session.setAttribute("login_id", uid);
and
String uid=session.getAttribute("login_id").toString();
respectively.
How I can match the current page URL with a certain pattern. For example I want to make if statement that makes one region on the page appears or disappears depends on the URL pattern of the current JSP page.
What I know , I have to use the tag :
<c:if test="the conditional test">Region</c:if>
for example I want the region appears if the end of URL is matching /me/*
?
You can use
HttpServletRequest#getRequestURI()
to obtain the request URI. The
getServletPath()
as suggested by the other answer is not necessarily helpful as it represents the servlet path (the matching part in the JSP/Servlet URL pattern), not the request URI (as the enduser sees in the browser address bar). If the JSP was been forwarded by some front controller servlet, you would get the JSP's own path instead of the virtual path as in the browser address bar.
Assuming that you have a menu which is represented by a List in the application scope where the Page class has url and name properties
you can use this code to find current page url, and then do your task
<c:set var="active" value="${fn:endsWith(pageContext.request.requestURI, page.url)}" />
Something along the lines of:
<c:set var="url" value="${pageContext.request.requestURL}" />
<c:set var="pathinfo" value="${fn:split(url, '/')}" />
<c:set var="pathnode" value="${pathinfo[pathinfo.length - 1]}" />
<c:if test="${pathnode == 'me'}">
<p>Show this region</p>
</c:if>
I have a login.jsp form that I have written for a website that just contains a form for logging in that I can include and re-use in different places. The first place I am including it is in my index.jsp homepage.
I have an HTTPServlet that the form submits to, and if the username/password is invalid the Servlet sends a message back to display to the user, otherwise it forwards them on to their homepage.
My problem is that I want to forward them back to the same page they're on if their details are incorrect, but display the message, but forwarding to the login page displays only that form in the browser, outside of the page it was included in. Is there any way I can forward back to the current page? Or is this perhaps not the best way to go about this?
Thanks for reading.
I am presuming you are using a <jsp:include>. Here is something you might do:
<jsp:include page="loginForm.html">
<jsp:param name="currentPage" value="${whateverTheCurrentPageIs}" />
</jsp:include>
Include the currentPage variable in the loginForm. Then, in your servlet, you want to redirect/forward to the URL you passed in that currentPage variable.
I want to call my web site like:
http://localhost:8080/?co=grav
When doing this the authentication page configured in web.xml comes up and the login is done through form login. After that it will go to index.jsp where I am doing a redirect to one of my main pages of the site:
<c:redirect url="index-userapp.jsp" >
The problem is that, this redirect does no longer forward my co parameter, and I really need to not loose it...
Do you see a workaround?
I think it can be done like:
<c:redirect url="index-userapp.jsp">
<c:param
name="co"
value=""></c:param>
</c:redirect>
but how I put in the new value, the old parameter value from the initial request?
try this:
<c:redirect url="index-userapp.jsp">
<c:param name="co"
value="${param.co}"></c:param>
</c:redirect>
I'm writing a testing utility- I want to show multiple finished HTML "pages" in a single browser window.
Is this possible? I'm using Java Servlets.
For example, normally the user goes to a utility screen, fills in a bunch of fields and POSTS this to my servlet, which builds up a full HTML stream based on their input and writes it to HttpServletResponse.getWriter(). When the user views source, they get a <html> ... </html>.
What I want to do is allow users to request multiple "screens" and get the results in a single web page where you'd scroll down to see the 2nd, 3rd, etc. screens, maybe there is some kind of divider in between. I thought of frames or iframes, but didn't have luck. I have seen where I can write my big html stream to a javascript variable, then use document.write to dump it into the iframe. But that seems pretty awkward, and I'd have to be really careful about escaping quotes and stuff.
You will have to use iframes or frames to do this. A single web page can only contain one set of html tags and thus one html page.
Another idea would be to render the page by your script and then capture a picture of it and then have a page containing images. You will of course loose all interaction with the page.
I'm not sure what you're trying with your frames, but I imagine frames should work OK for what you've described.
Instead of trying to post to more than one URL from your form, you just post to a servlet that returns a page with the frameset, and each frame has a source that points to one of the URLs you want to test. For example:
<form action="testServlet" method="post">
<input type="text" name="someValue" />
</form>
The testServlet then returns a page with this content:
<frameset rows="33%,33%,33%">
<frame src="testUrl1?someValue=value">
<frame src="testUrl2?someValue=value">
<frame src="testUrl3?someValue=value">
</frameset>
The only problem with this is that you're doing a GET instead of a POST, but that's easy to get around. All you would need do is to implement the doGet method within your servlets and just call doPost from within doGet.
Just leave out the <html>/</html> tags for each page and wrap the whole thing inside a single large ....
Like this maybe:
<html>
[page1Content]
<hr />
[page2Content]
<hr />
[page3Content]
<hr />
</html>