How to transfer parameter to redirect web - java

We know that we can redirect an website by putting this below the tag:
<head>
<meta content=0;url=http://www.google.com http-equiv='Refresh'/>
</head>
But what happen if the link I want to redirect is not google.com but it is stored in a string variable that I processed from my Java code? I know if it load from head down, then it will redirect immediately to that page without going through the following code right? So, how to combine the string that I extracted and let the website to load to that link?

From JSP (or servlet):
<%
response.sendRedirect(redirectURL);
%>
Where redirectURL is the variable containing the page you want to go to.

Ok, finally I figured it out:
First, need to construct the index.jsp and its java class in an web app:
https://netbeans.org/kb/docs/web/quickstart-webapps.html
then in the index.jsp, use this to get variable from that java class and redirect:
<%#page import = "org.mypackage.Webredirect.*" %>
<%Webredirect obj = new Webredirect();%>
<%response.sendRedirect( obj.getName());%>
thank you.

Related

JSP : How to get the URL Address of the page that redirects to another page in java?

I'd like to get an URL address of the page that redirects me to another page, in other words suppose that I have three jsp file with the names of one.jsp, two.jsp and three.jsp. the codes of each of them can be seen in the below section:
one.jsp
go to page two
two.jsp
<%
response.sendRedirect("three.jsp");
%>
three.jsp
<%
out.print(request.getHeader("referer"));
%>
the out put of these will be http://localhost:8080/one.jsp , but instead I expected to get http://localhost:8080/two.jsp as the result.
Now I've got two question:
why I get 'one.jsp' as a result?
How can i get the URL address of pages that do redirecting? ( in this case two.jsp)
This behaviour depends on the browser and seems not to be defined, see this question.
Therefore it is simpler to add parameter information about the redirect to the redirect URL and evaluate that parameter in three.jsp:
two.jsp:
<%response.sendRedirect("three.jsp?source=two");%>

jsp ${pageContext.request.contextPath} dosent get requested

Currently i have a .jsp project where my welcome page is a servlet
<welcome-file>frontpage</welcome-file>
The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>
request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
And forwards to the index.jsp page
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
My question is.
When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?
I tried the following
<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>
But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.
I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.
But how do i solve this?
add <%# page isELIgnored="false" %> in top of your JSP page, to enable expression language.
and to include a JSP page with other use <jsp:include like:
<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>
This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.

google app store datastore data in static file

So here is what I want to do. I want to have a static HTML and javascript file in Google App Engine (by the way, I can write it in either Java or Python). So I want the user to visit my index.html file and get information from the datastore, but how would I do this?
I understand you must access the datastore from Java or Python, but once I get the data using these languages, how do I make it appear on myindex.html page (preferably without refreshing). I have tried to find this information everywhere, but all I can find is how to access the datastore using the two languages, not how to take that data and put it into my HTML page.
Cheers
The Python or Java app runs on the server, while any Javascript functionality you have is run on the client (in the web browser). AJAX is generally the best way to communicate between the two.
I assume that you had access data from datastore and you would like to show it on index.jsp .
Suppose your data is greeting with property "user" and "content" . then you can write like this in your jsp file.
if (greetings.isEmpty()) {
%>
<p>Guestbook '<%= guestbookName %>' has no messages.</p>
<%
} else {
%>
<p>Messages in Guestbook '<%= guestbookName %>'.</p>
<%
for (Entity greeting : greetings) {
if (greeting.getProperty("user") == null) {
%>
<p>An anonymous person wrote:</p>
<%
} else {
%>
<p><b><%= ((User) greeting.getProperty("user")).getNickname()
%></b> wrote:</p>
<%
}
%>
<blockquote><%= greeting.getProperty("content") %></blockquote>
<%
}
}
%>
You can use GqlQuery to get the data as a python object. Use GqlEncoder to convert from GQL to JSON.
self.response.write() the data to a url.
Then, with javascript, you can make an XMLHttpRequest to get the data from that url, and include the javascript into the html.
A stackoverflow question/answer on the XHR doing a similar task.

Why won't my JSP remove cookie

I want to delete a cookie in my login page, no matter what I do, it just won't go away. in chrome developer, it shows the response from the server contains no cookie even though I had add it using response.addCookie().
Here's the JSP code that removes the code. It's at the beginning of the page, right after the content-type being set and before any output. this JSP is in the tile view (not sure if it makes any difference). I also tried moving this code to the containing JSP, but no help either.
<%# page contentType="text/html; charset=UTF-8" %>
<%
// remove cookie. all the properties (domain, path secure) match those those when the cookie was being created and set.
Cookie ck = new Cookie(someName,"");
ck.setMaxAge(0);
ck.setValue("");
ck.setPath("/");
ck.setDomain(someDomain);
ck.setSecure(true);
response.addCookie(ck);
System.out.println("wwwwwww 999991111");
%>
thanks.
Use
cookie.setPath(request.getContextPath());
your code only works if the application is deployed to the root.

Redirect pages in JSP?

I have to design several pages in jsp.
After clicking on the submit button on the first page, the page should be automatically redirected to the second page.
Can you help with a quick example or a link to a tutorial that demonstrates how to implement this?
<%
String redirectURL = "http://whatever.com/myJSPFile.jsp";
response.sendRedirect(redirectURL);
%>
This answer also contains a standard solution using only the jstl redirect tag:
<c:redirect url="/home.html"/>
Just define the target page in the action attribute of the <form> containing the submit button.
So, in page1.jsp:
<form action="page2.jsp">
<input type="submit">
</form>
Unrelated to the problem, a JSP is not the best place to do business stuff, if you need to do any. Consider learning servlets.
Hello there: If you need more control on where the link should redirect to, you could use this solution.
Ie. If the user is clicking in the CHECKOUT link, but you want to send him/her to checkout page if its registered(logged in) or registration page if he/she isn't.
You could use JSTL core LIKE:
<!--include the library-->
<%# taglib prefix="core" uri="http://java.sun.com/jsp/jstl/core" %>
<%--create a var to store link--%>
<core:set var="linkToRedirect">
<%--test the condition you need--%>
<core:choose>
<core:when test="${USER IS REGISTER}">
checkout.jsp
</core:when>
<core:otherwise>
registration.jsp
</core:otherwise>
</core:choose>
</core:set>
EXPLAINING: is the same as...
//pseudo code
if(condition == true)
set linkToRedirect = checkout.jsp
else
set linkToRedirect = registration.jsp
THEN: in simple HTML...
CHECKOUT
Extending #oopbase's answer with return; statement.
Let's consider a use case of traditional authentication system where we store login information into the session. On each page we check for active session like,
/* Some Import Statements here. */
if(null == session || !session.getAttribute("is_login").equals("1")) {
response.sendRedirect("http://domain.com/login");
}
// ....
session.getAttribute("user_id");
// ....
/* Some More JSP+Java+HTML code here */
It looks fine at first glance however; It has one issue. If your server has expired session due to time limit and user is trying to access the page he might get error if you have not written your code in try..catch block or handled if(null != session.getAttribute("attr_name")) everytime.
So by putting a return; statement I stopped further execution and forced to redirect page to certain location.
if(null == session || !session.getAttribute("is_login").equals("1")) {
response.sendRedirect("http://domain.com/login");
return;
}
Note that Use of redirection may vary based on the requirements. Nowadays people don't use such authentication system. (Modern approach - Token Based Authentication) It's just an simple example to understand where and how to place redirection(s).

Categories

Resources