EJB3 Interceptor- Setting attributes and reading them on JSP - java

In an enterprise application, I'm trying to set some attributes in an EJB3 interceptor and then show them on a jsp. Is it possible? How can I achieve that? The use case is as follows- Some values are read from the database which are required to be shown on the error page. This error page is the default error page, no controller for it. I couldn't find anything in the code regarding this error page, nothing in web.xml. Need a solution for this asap.

I couldn't find a way to use an interceptor. I ended up using a filter, which was already there in the application. I set an application scope variable map in the init method of the Filter. That ways, I had to get and set the values only once, i.e., at the server startup. Then I used this map in the JSP to read the values. Like, ${applicationScope.appParams.someKey}.

Related

Is updating Bean property in filter possible?

Lets say i am redirecting to some jsp. The request goes through the filter(class that extends javax.servlet.Filter) in Spring. Now in that filter i'd like to set some bean properties with values from cookie that came with request. Afterwards when request passes filter i would like to use that bean properties in the jsp I was redirecting to. I've been trying to make it work for a while but wasn't successful. I am able to get the bean in the filter so I call setter, but when that jsp loads I can see only the default preset value and not the one that came in the cookie. Is something like this even possible? (I will add code snippets if needed.)

Displaying Error Messages from a Web Service Call to JSP

I am calling a web service to validate an email address. In case of an invalid email address this service returns messages and I need to display them in a JSP. I am using Jersey with Struts2 and I have a form which submits to an action which takes care of this logic at the back end. I am not using a servlet to get the messages from the HttpServletRequest object.
I get a list of messages and I need to display the error message text on the screen but I am not sure how to do it. On click I get the form id which goes to the back end using struts2 action.
Thanks...
I'd strongly recommend using the Struts2 validation interceptor. The documentation is very straight forward and it's very robust.
https://struts.apache.org/docs/basic-validation.html
https://struts.apache.org/docs/email-validator.html
You simply need to call the interceptor stack in your action in struts.xml, then specify what validations you want. You can then display your error on the JSP should the validation fail.
You can either use the default validators provided by the XML validation or set your own by creating a validate method in the action class that is called by the stack, or both.
http://www.simplecodestuffs.com/struts-2-fielderror-example/
Here is an example for you to work with.

How servlet handle multiple request from same jsp page

Sorry friends if this question is very easy but i am confuse i unable to find out solution.
As we all know in spring MVC framework we create controller which will handle multiple request from same page using #requestmapping annotation.
but same thing i want to do in servlet how can i do ?
Suppose i have a jsp which which will contain a jqgrid,and two forms i want to use only one servlet to load the data into jqgrid and that servlet only will handle request from both the form . Since we have only doGet and doPost in servlet how one servlet fulfill all three request. Hope you understand my question if you have and link where i get sample or and tutorial link plz reply me
Well, the only easy way to do this would be to use a request parameter to control how the processing happens.
In a very basic example, you may have something like a requestType value that gets passed as either part of the query string or the request body. You would assign values of 1-3 (or 0-2) with each value indicating a different type of request. Your servlet would then parse the request accordingly.
This actually is how the DispatcherServlet in SpringMVC works. There's only one servlet class instance and when a request comes in, it examines the query string along with other parts of the request to determine which controller should handle the request.

passing values between servlet and java file

I've one spring controller which is setting some values to request and shows a jsp page. For the view part we use tiles. The result page has 3 parts, header , content and footer jsp's.
This header jsp use a java file and i want to access the attributes created by the first spring controller from this file. Is there any way to do that without using session?
When I tried request.getAttribute, it gives null. I think it's because it's not an immediate file after the request values setting.
As long as everything runs in the same request and the controller code is executed before the view part, setAttribute() should work. To debug issues like that, use a Filter which dumps the request URL and attributes to the console or the log.
If those calls are in different requests, you have two options: The session and a Spring bean (use a session bean or your own implementation). I prefer beans since they are type safe and they allow me to separate my code from the Servlet API which is complex to test.
You'll really need to put some code to get a code answer but unless you're using JSP scriptlets I'm guessing this is a Java bean that you're using in the header. This of course cannot access the request (hence the session) nor should it really. What you probably want to do is convert it to a tag library if you want it to have access to the request/session.

JSF Servlet Arch Help needed

i want a mechanism in my web app as described below:
user will enter mydomain.com/CompanyName , depending upon the CompanyNameit will show its logo and its customized page, and i will take that parsed parameter in session again upon each request i will compare the parsed CompanyName and one stored in session , and if they matched then application will show the requested page with user's data.else it will be redirected to login page.
and the main thing is i want this thing in JSF arch.
i tried taking a servlet that will resolve all request and it will parse and then will dispatch the request to prefered servlet,but problem is it goes in loop as again it resolves to the same controller servlet,
You can do this via a phase listener. You can define a global one in faces-config.xml (or using annotations, if JSF 2.0 is used)
<lifecycle>
<phase-listener>com.yourcompany.CompanyPhaseListener</phase-listener>
</lifecycle>
There you have access to the FacesContext, from which you can obtain the current request URI. Parse it and store the appropriate attributes in the request, which you can later read on your pages.
The phase listener is executed on the specified phase(s), and perhaps you should choose RENDER_RESPONSE
For affecting the way your URLs appear in the browser, check PrettyFaces.
i tried taking a servlet that will resolve all request and it will parse and then will dispatch the request to prefered servlet,but problem is it goes in loop as again it resolves to the same controller servlet,
Use a Filter instead. It by default doesn't listen on forwarded requests, so you won't get an infinite loop on forwarding.

Categories