How to send object throgh request between diffent application - java

I want to send Java (POJO class) Object to other applcation which is installed on diffent or same tomcat.
I have tried with request.setAttribute("abc",javaObj) but getting null value while using request.getAttribue("abc") and scope="application" and My both application on same tomcat.
I am Using redirect jsp.

If you want to share a variable between two applications under the same tomcat then you need to set it in the servletcontext using setAttribute method of that.
For sending the POJO to a different tomcat or JVM, you either use RMI or HTTP.

Use RequestDispatcher and setAttribute
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("your url");
MyPojoObject m=new MyPojoObject();
request.setAttribute("abc", m);
dispatcher.forward(request, response);
to retreive
MyPojoObject mo=(MyPojoObject)request.getAttribute("abc");

Related

different url after calling a servlet [duplicate]

This question already has answers here:
RequestDispatcher.forward() vs HttpServletResponse.sendRedirect()
(9 answers)
Closed 5 months ago.
I have A servlet that does some stuff, when it has done puts some things in the request and after calls another servlet which in turn calls a jsp page.
I write the code:
first servlet (InserisciLezione)
request.getRequestDispatcher("/TakeDates").forward(request, response);
second servlet (TakeDates)
RequestDispatcher dispatcher = request
.getRequestDispatcher("GestioneCalendario.jsp");
dispatcher.forward(request, response);
This works properly but the problem is that in the url of the page I have yet:
http://localhost:8080/Spinning/InserisciLezione?data=20-02-2013
and If I refresh the page the first servlet is called again and I don't want this.
I would like to have
http://localhost:8080/Spinning/GestioneCalendario.jsp
Why?
thanks in advance!
If my memories are good (it's been a long time I didn't use raw servlets), you should use a redirect rather than a forward.
You can use the response.sendRedirect(url) method.
The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp.This interface can also be used to include the content of antoher resource also. It is one of the way of servlet collaboration.
In your case ,you are getting this
http://localhost:8080/Spinning/GestioneCalendario.jsp
because of this servlet
RequestDispatcher dispatcher = request
.getRequestDispatcher("GestioneCalendario.jsp");
dispatcher.forward(request, response);
Page refresh will always redirect you to that url by which servlet you have used. Its like calling an ajax event.
Anyway I can see you dont need to use forward method,try to use
response.sendRedirect(your_url);

Java equivalent of session_start(), session_destroy(), and $_SESSION['username']

In PHP when a user logs into her account, I do the following in order to remember the user as she navigates through the site:
session_start();
...
$_SESSION['username'] = $username;
On any other page that may require sensitive data, I check that $_SESSION['username'] is valid.
When a use logs out, I do the following
unset($_SESSION['username']
session_destroy();
How do I do the same thing in Java? I have a REST API which uses Jersey and EJB. In case the following is important, I am persisting with JPA, Hibernate, Glassfish, and mysql.
UPDATED FOR VERIFICATON:
Is this correct?
#Path("login")
public class UserLoginResource {
#EJB
private LoginDao loginDao;
#Context
HttpServletRequest request;
#POST
public Response login(JAXBElement<Login> jaxbLogin){
Login login = jaxbLogin.getValue();
loginDao.authenticateUserLogin(login);
HttpSession session = request.getSession();
session.setAttribute("username", login.getUsername());
return Response.ok().build();
}
}
Java is very different from php, so in java You will get session from only HttpRequest 's getSession() method, In php it is all time assumed, your code is run by some server(ie apache), In java, you will obtain it from ServletContainer(ie Apache Tomcat).
You do not have to start session in java unlike php, As long as you are in servlet container and giving request, for this client servlet container is responsible to start if there is not session for it
So for above actions:
reqest.getSession().setAttribute("udername","Elbek");
//later
reqest.getSession().removeAttribute("udername");
//destroy it
reqest.getSession().invalidate();
Here request is object of HttpRequest class
You may have a look to this HttpSession
I strongly recommend you to have a look java scopes
There is not this kind of thing in php, I wish there is, BUT there is NO
Here is how you get request object into your jersey action(method), ie by injecting #Context HttpServletRequest httpRequest
EDIT:
You do not create HttpRequest object by yourself, Instead you will get it from servlet container, Your server creates it from clients request and gives for your.
+elbek describes plain servlet situation - however, nowadays almost nobody writes plain servlet. It sickes soo much back then, that a lot of web frameworks evolved. There is a sh*tload of them, but good ones will utilize dependency injection techniques like spring
( for example , struts 2 ) and there are distinct scopes - application / session / request - containing plain java beans, which can in turn have authentication data.
J2EE also provides own authentication semantics with servlet container and JAAS - it also uses session tracking and useful when you need to access some backend resources like datasources or queues or EJBs - but most web application do not bother wth it.

How to get url request parameter from inside LIferay/IceFaces/JSF portlet backing bean

Is posible for a portlet to read a request parameter of its surrounding page?
E.g. the URL of the page the portlet resides in is http://example.com/mygroup/mypage?foo=bar Is it possible to read the "foo" parameter from a portlet that is on that page?
Portlet Container is Liferay 6.0.5.
P.S.
I have already tried:
com.liferay.portal.util.PortalUtil.getOriginalServletRequest(com.liferay.portal.util.PortalUtil.getHttpServletRequest((javax.portlet.PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest())).getParameter("foo")
but I always get null for productId
Thanks!
Have you tried
ExternalContext.getRequestParameterMap()
The following code will do the trick:
javax.portlet.PortletRequest pr = (javax.portlet.PortletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("javax.portlet.request");
java.lang.reflect.Method method = pr.getClass().getMethod("getOriginalHttpServletRequest");
HttpServletRequest httpServletRequest = (HttpServletRequest)method.invoke(pr, new Object[] {});
return httpServletRequest.getParameter(YOUR_PARAM_KEY);
In a partial submit, the icefaces ajax bridge (which replaces a usual jsf portlet bridge) is avoiding the normal portal action/render request, contacting directly the blocking servlet (in order to avoid invalidating other request-scoped portlets and in general, to be faster). Because of this, all those params/attributes which are set in a normal portal request are not set in ajax. They are set only in the initial GET type request for that page. So, actually what you should do is saving those params in the #PostConstruct or some other method of your controlling bean, and then reuse them later. (They wouldn't change in a partial submit anyway, right?).
Keep in mind though, that this will not work if you use IceFaces in conjuction with Spring (and their EL Resolver, since that eliminates your extended request scope).
if you are in JSF environment then try this:
String param = LiferayFacesContext.getInstance().getRequestQueryStringParameter("foo");

Sending redirect to another servlet/JSP without loosing the request parameters.

How do i specify a redirection to another servlet, in the doPost() method of a servlet.
at the moment I'm using
request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
which is wrong since, my parameters in the doGet() method of products are not being called and initialized.
So I'm left with an empty products page after logging in :/
You need to use HttpServletResponse#sendRedirect() to send a redirect. Assuming that the servlet is mapped on an URL pattern of /products:
response.sendRedirect("/products");
This way the webbrowser will be instructed to fire a new HTTP GET request on the given URL and thus the doGet() method of the servlet instance will be called where you can in turn load the products and forward to a JSP which displays them the usual way.
In your doPost you can always call:
return doGet(request, response);

RequestDispatcher

request.getRequestDispatcher("https://app.inpostlinks.com/login").forward(request, response);
I want to forward the request to the foreign URL like https://app.inpostlinks.com/login, not residing on the container where the servlet resides.
It's not getting forwarded. Are there any solutions on the above scenario?
Forwarding is for passing say a servlets request and response to a JSP to do the presentation of the result of some business logic.
If you want to pass the user to a different website, you need to do a "redirect" instead of a "forward", ie get the server to return a 301 code and a location:
response.sendRedirect(url);

Categories