How to obtain Liferay session in custom servlet? - java

I wrote custom servlet in Liferay and want to know which user page calls it and know other parameters like theme. But the request's attributes and session fields are all nulls.
How to make custom servlet to receive request as if portlet does?
Thanks
P.S. I don't want to use this solution https://www.everit.biz/web/guest/blog/-/blogs/getting-current-liferay-user-in-a-standalone-webapp?_33_redirect=/web/guest/blog
which reads cookies manually. I want to do such as Liferay does, i.e. by using it's API. Is it possible?
Update 1.
I have a portlet and a servlet in one WAR. I can know who am I (logged in user) from within portlet JSP like this:
HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
themeDisplay.getUser()
Now I want to do the same from a servlet. Is it possible?
I am working in eclips which deploys automatically.

You either have to mimic what Liferay does in the portlet request handling (not recommended) or, alternatively, put your servlet code into a portlet - this can be the "resource handling" of a portlet - here you get full access to the http request and can do everything yourself with regards to data types transmitted in the stream.
I'd rather recommend this as it will be significantly easier to upgrade. Portlet Resource Handler are very similar to servlets from a logical point of view. There might be other (more advisable) options, but this is what comes to my mind for this type of problem.

Related

Portlet jsp + jquery datatable +servlet implementation

I have created a liferay portlet using mvc (jsp ) and i'm using JQuery for datatable.
I have succeeded to bring a list and display it on a table using Jquery.
Now, I'm trying to make it editable to use CRUD functions.
The problem that i'm using java not php and i couldn't assign a servlet function (doget).i didn't understand it well (how to use it) and they set the url of this class to jquery.
If i'm not wrong this would be configured in web.xml (servlet mapping). So i wonder how to properly configured and use it?
Also, i don't know jquery but it seems that the buttons needs to be manually included. is there any method to include them directly? (with jquery looks and pop up).
Servlet comes in 2.4 version i had troubles changing it to 3.0 using eclipse.
Best Regards
If you use web.xml and servlets, you are out of liferay and the implementation for user login, themeDisplay, permissions and other features. This way is dificult and improductive.
Better choice is override the method "serveResource" of your portlet, in this method you have all control like in servlets and you can use other features of liferay.
In this question you have an example and more information:
respond to http request with json object in portlet

JSP page Reload

In my spring mvc application,I am facing a problem of page reloading on every request.Is there any way to restrict that.To be more specific,When I return the name of index.jsp page from controller it will obviously reload the page.How Can I restrict that thing in the index.jsp page.Suppose some part of the page is required to interact with Database and it should be shown on the same index.jsp page as well.How I can achieve this requirement without reloading the index.jsp page completely.Could you please provide me some example for that.
You can use Filter for that. I am not providing any code here, just try to explain the concept.
Filters have a wide array of uses; the Servlet 2.3 specification suggests the following uses:
authentication filters
logging and auditing filters
image conversion filters
data compression filters
encryption filters
tokenizing filters
filters that trigger resource access events
XSL/T filters that transform XML content
MIME-type chain filters
Use a Filter when you want to filter and/or modify requests based on specific conditions.
Use a Servlet when you want to control, preprocess and/or postprocess requests.
Filter are best suited for authorization, because it can be configured to run for all pages of a site. So you only need one filter to protect all your pages.
Useful Links:
filter tutorial
filter in detail
referred answer
Oracle tutorial on Filter
sorry if i missunderstanding you!
i don't know what do you want to do but i can imagine the following solution.
first direct call to jsp should check a session value, if not exists, redirect from jsp to servlet, in the servlet do what you wand and finally put this session value, and redirect back to jsp.
a good concept depends on what you want to do exactly. You can manage this issue using a PhaseListener or a Filter. but if you just want to manage this one redirect, then no need of Filter or PhaseListener

Read jsp page output from my application

I would like to read jsp page from my application and save it to a file - it's output, not the code itself. Plus, my application has basic authentication (username+password).
If it was a Servlet, I could just access it's doGet method.
One solution I've found is this - Open URL connection, provide authentication details and read the stream.
I'm wondering if there's another option, maybe accessing the generated Servlet in the web container and then using reflection to call the class doGet.
You can precompile the JSP and then call the servlet (you don't have to use reflection even).
If you try to call the JSP's servlet without precompiling then it might not exit yet (because usually the server only compiles the JSP after it was called for the first time).
To precompile the JSP, check your web server documentation.
Personally I think you're better of using URL connection. Precompiling JSPs is not portable (as in you need to do it in a different way for each web server).
Edit
You can also use RequestDispatcher.include() method with a wrapped response object as described in this answer.

Does s:url (in Struts2) not do url-rewriting like c:url does in case cookies are disabled?

The Servlet API resorts to URL rewriting if cookies are disabled.. and every URL that we provide in our JSPs must be inside c:url for this to be in effect. But, in Struts 2, there is an equivalent tag s:url, but its documentation says nothing about automatically adding url-rewriting information (if required) to the url. Is its behaviour similar in this regards to c:url, or do we have some other means to achieve the same effect in Struts 2 ?
I am using Struts 2.1 url tag reference from here
Clarification : In case that the user disables cookies, the other option is to append the jseesionid to each url that is there on the page. c:url handles that for us. My question is that whether s:url does the same thing for us. I was worried as its not mentioned in the documentation link I provided above.
Yes struts2 will do this too. Simply disable cookies and you should see a session id is put into the url (when using s:a tags, I have not tested url tags). I'm glad you added clarification because this is url writing. Url rewriting is done as urls come into the server, where they are then rewritten by certain rules generally so they get directed to the right place within the server(which is not at all what is happening here).
Edit: Thanks to Daud, the correct term is url-rewiting. This is because session management is handled by the container level, although struts2 can manipulate what the session contains, it is the container which provides the session via a "session manager" object and determines how persistence is best managed (including generating the jSession Id).
For details on this for glassfish: http://docs.oracle.com/cd/E18930_01/html/821-2418/beaha.html#beahf
For details on this for tomcat: http://tomcat.apache.org/tomcat-7.0-doc/config/manager.html#Disable_Session_Persistence
For other containers simply google: Container_Name + "session manager"

Content of the ServletResponseObject or servletresponsewrapper

I have a simple web application running in Tomcat. There is a servlet which is forwarding the request and response to a jsp page which in turn prints something to the browser.
Now I have an aspect which captures the response. In this case a HttpServletResponse. Now what I want to do is that I want to capture the reponse and add some content in it to be displayed in the browser through JSP page.
I dont want to add the content in the jsp page rather I want to add some content without changing my jsp or servlet, using runtime weaving functionality of aspectJ.
I have not been able to figure out any solution yet.Please help me with this.Thanks.
We don't do that with the response. Request object is a better candidate for that. You must understand that JSP is a server-side technology, therefore your request used to be alive in the JSP page.
Moreover, I would suggest you to look at Servlet Filters, those are meant for this kinda scenarios.

Categories