I have a very strange problem, I have a useBean loginCheck which is stored in session and I want to use it to display a different menu according to the user logged. So here is my code in the JSP :
<jsp:useBean id="loginTest" type="java.lang.String" scope="session"/>
...
...
...
<ul id="menu">
<li>Faire une demande de dérogations</li>
<li>Voir mes demandes</li>
<%if(loginTest.equals("admin")){ %><li>Espace admin</li><%} %>
<%if(loginTest.equals("manager")){ %><li>Manager</li><%} %>
<%if(loginTest.equals("paie")){ %><li>Paie/Poste</li><%} %>
</ul>
When I run into this page the first time I have a page completely blank, no error, no logs, nothing. Then I run the page without the menu and the useBean, so it works great, I can see the rest of the page. And then I put back my useBean and my menu and here, it works, I can see the menu with the good link according to the user.
I'm very confused because my code works but only when I remove the code, access to the page, put back the code and refresh. Any idea about this problem ?
The part that is useBean is required, because without it the rest of the code doesn't work.
When you modify the code of JSP page you need to rebuild, redeploy this page or your application to make the changes effect.
Related
I have a JSP home page with a menu. I want that menu to be on multiple pages so I created a menu.jsp template. I imported, all static source code appears but the <c:forEach /> tag is not picking it up. When I print (just put it plainly in the html) out my ${allCategories} variable I get all the data correctly.
In a controller, I set model.addAttribute("allCategories", categoryService.getPrimaryCategories());
home.jsp where the categories were before is importing <jsp:include page="menu.jsp"/>
in menu.jsp I am unable to get <c:forEach items="${allCategories}" var="category"> to work but when I just put <p>${allCategories}</p> at the beginning of the file it shows stuff like [Category(id=1281, name=Bundles, image=https://images...
How do I get the data correctly in the sub-template so it works like directly on the page?
You could try using the JSP include directive. It is used to include a file during the translation phase. For example,
<%# include file = "menu.jsp" >
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.
I am currently working with JSP pages and would like to know what would be the easiest way to comment HTML/Java code from being able to see it when you open source on the web. As for now, I was used to use
<%-- html/Java code --%>
However, later on it screw up all highlighting of JSP file. Maybe there is another way of commenting multiple lines without possibility to see them in HTML source on web.
Look:
<%-- Comment --%> - commenting out html
<% /*java code*/ %> - commenting out java code
<!-- comment --> - html comment, ignored by the browser
I would like to quote Oracle on this one: Code Convention
Please note the following (excerpt from the link):
JSP comments (also called server-side comments) are visible only on the server side (that is, not propagated to the client side). Pure JSP comments are preferred over JSP comments with scripting language comments, as the former is less dependent on the underlying scripting language, and will be easier to evolve into JSP 2.0-style pages.
Examples:
<% /** ... */ %>
<% /* ... */ %>
<% //... %>
<% //... %>
Client-side comments can be used to annotate the responses sent to the client with additional information about the responses. They should not contain information about the behavior and internal structure of the server application or the code to generate the responses.
Let's note that these comments:
<!-- ... -->
ignore all elements except #import statements.
I know it must be simple, but still I am not able to figure it out.
I have a link on a jsp page.
When this link is clicked I want another tab (of browser) to open up.
The view of this new page is defined by action class, which needs a form field value.
The problem I am facing is that I can not get the values of form fields to the action class without submitting the form. And submitting the form changes the view of the original jsp as well, which defeats the whole purpose of opening a new tab.
So I need a way to get the form field values to action class without resetting the view of original jsp page.
One way I came across was URL re-writing but that would be my last option.
Please suggest something!!
Thanks!!
Firstly I would like to point out that currently possible (to my knowledge anyway) to force a new tab to appear, it is dependent on the users' browser and the settings that they have see here for more infomation.
Now onto your question, since links cannot send form data (on their own) you have 2 options:
You can use a form "submit" button pointing to the URL you want to send the data to and to and add the target="_blank" to the form which will cause a new page to open up when the form is submitted.
You can add a javascript event to your link so that when it is pressed you append the value of the input to the URL and open a new window with that URL.
Personally I would choose the first option.
Here is a simple example of option one which doesn't remove the input value when you submit...
<html>
<body>
<form action="test1.html" method="post" target="_blank">
<input type="text" name="bob" />
<input type="submit" value="Hello"/>
</form>
</body>
</html>
You could do an ajax call, or dynamically build the link url with get parameters in it.
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).