In my application I am using struts2 and hibernate. In one of my service classes I used struts2 session for set an attribute and got that attribute in my jsp like this.
here is the java class portion:
Map fileInformation = ActionContext.getContext().getSession();
fileInformation.put("checkFirstPart"," Hello ");
here is the jsp scriptlet:
<%
Map recordedData2=ActionContext.getContext().getSession();
String checkFirstPart = (String)recordedData2.get("checkFirstPart");
%>
<%
if (checkFirstPart != null)
out.println(checkFirstPart);
%>
the problem is by every time i run my project, session is not clear and shows all previous messages until I restart the application server.
I used this also but it was not useful:
fileInformation.clear;
please help.
put this code in your jsp page after getting the attribues:
recordedData2.clear();
I think this will solve your problem
Related
i have multiple web pages jsp and i use for the resubmit in refresh response.sendredirect("blabla.jsp") but one page work good , another page wen i press submit it go to a blank page and the row added to database, any solution for this problem ? thank you
`
<% String UC1 = "INIT";
if (request.getParameter("add_spec") != null) {
UC1 = "ADD_SPEC";
}
if (UC1.equals("INIT")) {
List<Speciality> specs = SpecialityController.INSTANCE.findAll();
%>
<%#include file="./WEB-INF/Add_Spec.jspf" %>
<%#include file="./WEB-INF/view_all_specs.jspf" %>
<%}
if (UC1.equals("ADD_SPEC")) {
String spec = request.getParameter("speciality");
SpecialityController.INSTANCE.create(new Speciality(spec));
List<Speciality> specs = SpecialityController.INSTANCE.findAll();
response.sendRedirect("main_admin.jsp");
%>
<%#include file="./WEB-INF/Add_Spec.jspf" %>
<%#include file="./WEB-INF/view_all_specs.jspf" %>
<% }
%>
`
You can use the Post/Redirect/Get pattern.
When a web form is submitted to a server through an HTTP POST request,
a web user that attempts to refresh the server response in certain
user agents can cause the contents of the original POST request to be
resubmitted, possibly causing undesired results, such as a duplicate
web purchase.
To avoid this problem, many web developers use the PRG
pattern - instead of returning a web page directly, the POST operation returns a redirection command.
In other words, when you submit the data, you should redirect to the page on which you can view (get) the data you've just added.
That way, refreshing will not resubmit the data.
Alternatively, you could use a CSRF/XSRF-like token.
Though this example is in PHP, you should understand the gist of it.
Update
Even better, you can check out this example for CSRF https://services.teammentor.net/article/00000000-0000-0000-0000-000000040a2e
In my servlet (after login) I set the session timeout interval as 30 seconds and also note the sessionID as say X
session.setMaxInactiveInterval(30);
The servlet then forwards to a JSP page (intermediate1) which has a link to the second intermediate page (intermediate2). I don't do anything on the page for around 30 seconds (timeout interval) and then forward to the second jsp page (intermediate 2). Here I print the sessionID and it is NOT X. It is another value. How did this happen? Does the container automatically assign a session object to a JSP page if no session already exists? Kindly help.
JSP spec (for JSP 2.1/JEE6 it is found in chapter "JSP.1.10.1 The page Directive") describes that the session implicit object is on by default, so that every call to a JSP will participate in an existing session/create a new session if needed. It can be turned off as:
<%# page session="false" %>
Because of jsp implicit-object, then see one of them is 'session'.
So, jsp's implicate object always be there.
In your case, already current session object went off, but jsp api make it newly available.
Here is an interesting link I found on the web that answers my own question. FYI.
http://www.xyzws.com/jspfaq/can-i-prevent-the-creation-of-a-session-in-a-jsp-page/20
Here's my environment. Windows 7, Tomcat 8.x and Java 7.x
I'm writing an application, that has an entry point in the servlet, as opposed to a JSP. In this servlet I create a session, assign a variable to it, and then redirect the user to a JSP page. Here's the JAVA code:
HttpSession session = request.getSession();
logger.debug("Session Id: "+session.getId()+
"New Session? "+session.isNew()+
"Created: "+new java.util.Date(session.getCreationTime()));
session.setAttribute("implementation", sImplementation);
session.setAttribute("RequestId", sRequestId);
response.reset();
response.sendRedirect(request.getContextPath()+"/jsp/ProductSelection.jsp");
In the JSP page that I just redirected to I try to retrieve the session attribute that I just set in the Servlet and the value comes back as null, but only the first time that I use the JSP page. If I call it again by resubmitting the URL in the browser then everything works. :O :( Here's the relevant JSP code, the value of sImplementation is null the first time this JSP is hit.
<% System.out.println("Session Id: "+session.getId()+
"New Session? "+session.isNew()+
"Created: "+new java.util.Date(session.getCreationTime()));
String sImplementation = (String)session.getAttribute("implementation"); %>
Also if I make an entry point into my system to be a JSP page that does nothing but redirect the user to the Servlet everything works as expected. So the session created in the servlet is not valid until. Only when a JSP page is hit. :(
Lastly I tried using dispatcher.forward instead of response.sendRedirect and the session variable is there, however, the bootstrap framework that I'm using to render my pages do not render properly at all. :( I tried this in both Internet Exploder 11.x and Chromium 33.x
So my question is whether the behavior that I'm seeing is normal and expected or if there's something wrong here and there's a solution out there somewhere? :)
Thanks to all in advance, and let me know if anything is unclear or needs more code.
If im not wrong (I might), you should redirect to your jsp using
request.getRequestDispatcher("ProductSelection.jsp").forward(request, response);
This way it will be the same request and you will be able to get your session. By the way if you are creating sessions on your own you should disable the default session made by all jsp's
<%# page session=“false”%>
Make me know if it worked. :·)
I have a JSP page from which I'm calling another JSP page using response.sendRedirect(recordUrl2). I have some session variables from the first JSP page which get lost after the redirect. Is there anything that can be done in the web.xml configuration xml file to preserve the session variables?
I added the following in the xml, but the variables weren't maintained:
<session-descriptor>
<persistent-store-type>memory</persistent-store-type>
<sharing-enabled>true</sharing-enabled>
</session-descriptor>
I also tried response.encodeRedirectURL(recordUrl2) instead, but sessions variables weren't maintained.
Are you by any chance redirecting to a JSP hosted in a different JavaEE container or within a different WAR/deployment than the one doing the redirect? That's the only reason, short of you having code somewhere that clears the session, that the second JSP would not be able to access the session.
To remove the language toggle from the page view(Comfirmation Page)
I found this code but it doesn't work in Spring MVC
<c:if test="${!fn:contains(pageContext.request.servletPath,'/comfirmation')}">
//Other Code
</c:if>
My actual url is (ShoppingCart.jsp).
It is used when /viewCart.htm,/updateCart.htm,/Confirmation.htm,etc.
So, the user go to the /Confirmation.htm, it also redirect to the ShoppingCart.jsp but the url path in the browser is /Confirmation.htm.
I want to remove the language toggle when call the /Confirmation.htm in the above mention.
Finally, I got it. Here we go
<%
String url=request.getAttribute("javax.servlet.forward.servlet_path").toString();
if(url.equals("/Confirmation.htm")){
%>
//Language Toggle code
<% } %>
I decided to use this. Another way is that storing url path in session since front controller.
the pageContext.request.servletPath will give you the path of the jsp (and not the url your browser shows).
The request is forwarded to a controller, which returns a path to a view. The view ist called using a second internal request