My company is redoing our website over the next few months, going from a ColdFusion website to one written in Java. I am just learning Java and I am curious as to how I can set application scope variables in a Java web application. ColdFusion has the application.cfm file that holds variables that are accessible by all ColdFusion pages/components within the app. Java obviously does not have a direct equivalent to that file, so I was wondering how to recreate something similar in Java. I want to have one central place that all jsp pages, servlets, classes, etc. in the webapp can access.
So what is the best way to do something like that?
Does anyone have any examples of something similar that they did?
Should application scope variables be placed in a class? an xml file? a jsp page? something else?
Is it even feasible to have application scope variables a in java webapp?
Example:
It would be a place that holds say the path to an images folder, so in individual JSP pages or classes or whatever would need acces to that images folder, you could reference the application scope variable for that path instead of writing it out in each place. Then if for some reason we needed to move the location of that images folder, we would just change the path variable in the one location and not have to update 20 places that reference that images folder.
I have had trouble finding any information on this type of thing in my research online, which is why I am asking about it here. I know it is a lot to ask for an explanation to this type of thing, but I figured I would ask and see what type of responses I could get.
Thank you for any help you can provide about this topic.
The equivalent is probably the ServletContext
In your controller servlet:
ServletContext context = request.getSession().getServletContext();
Just like in the session object you can store attributes in the servlet context as well
context.setAttribute("someValue", "aValue");
Object attribute = context.getAttribute("someValue");
If you're starting out with Java and you have a non-trivial application to build, I would recommend using a popular framework like Spring or Struts
Related
I created a tutorial that incorporates various components of J2EE in them. The app is an ear module that has an ejb project and a web project.
the project structure is
john-app
john-ear
john-ejb
john-web
the ejb project has a couple dao that perform basic crud operations using jpa. I'm trying to learn/understand jax-rs to gain a better understanding of json and handling json objects. the project is loosly based on
this project:
http://www.developer.com/java/creating-restful-web-services-with-jax-rs.html
so, i created my BookResource here and made it a stateless ejb. i have everything compiling and deploying without any obvious errors - other parts of the app work (the jpa stuff) but i can't get the jax-rs stuff to
work. i have a couple of"Books" that I created in my database and am wanting to test this by making a rest call through the browser.
so i deploy my .ear file in wildfly (v10) with no obvious errors, I see JNDI mappings for my EJB's...etc
initially, i'd like to be able to test this through my browser, but am not certain what url to use -- the class i have extending javax.ws.rs.core.Application has an application path of /rest and my
BookResource has a path of /library, and for getting all books from the library, the sub-resource is books. I've tried every combination of the url below,
http://localhost:8080/john-[app|ear|ejb|web]/rest/library/books
all to no avail. every call results in a 404 error, and the only time i got ANYTHING is when i tried john-web combination, it threw some ugly exception in the browser. so the questions is with a rest service living inside an ejb module within an ear, what should the url be given the above information. nothing i've tried
seems to work!??
I've not included any code samples to try and keep the explanation short -- i didn't want to include every java file in my little project, but can add anything as requested.
Thanks,
JG
I have a couple of web applications whose source code is missing. The project is compiled to a dll and is hosted on a IIS.
I have couple of questions to make.
What is the best way to recreate the
project from the dll file??
We are planning to change the
database server, and the database
connection strings are specified in
the Global.asax ( I mean the public
class Global : HttpApplication ). Is
there a way I can subclass this
Global and override the connection
strings? If yes, how can I make the
IIS refer to the new dll
Thank you all for any suggestions!!
For first part, use decompiler tools such as Reflector/ILSpy/dotPeek to convert IL code from DLL to higher level language such as C#. However, tools cannot get back comments, local variable names and project structure. You need to manually organize the code into files and project structures. From aspx files, you have to figure out the code-behind classes and then link up the source of the class into a correct named code-behind file - for example, if default.aspx says that it inherits from MyApp.Default then create file default.aspx.xs and put the source code for the class into that.
For second part, you can create a new class derived from Global and modify Global.asax to use that class - you need to put the assembly containing new class in bin folder and overwrite inherits clause in asax file to point to the type name of new class. You anyway need to inspect the code of your current Global class (using tools sighted above) to see if you can override connection strings by sub-classing.
Probably the best tool available to reverse engineer a dll into code is .NET Reflector. Unfortunately, the latest version is no longer free, but it is worth the money.
I am not sure exactly if this still applies if you can already reverse engineer your source code. However, I would recommend moving your connection strings outside of your project into web.config as a best practice. This way you can make the change in the future without changing any code.
I want to know a bit more about 'xmlns:mx="http://www.adobe.com/2006/mxml". Generally namespaces acts as the pointers to the component location, but I've always seen them directing resources within local directory structure. When 'xmlns:mx="http://www.adobe.com/2006/mxml"' is used than is a new connection is set with adobe server or is it just a convention?
If an actual connection is set then the application should not get compiled without internet connection but in reality we can compile and run our application without internet connection as well !! Plz correct me if I am wring somewhere.
Please help me understanding its significance,
Thanks in advance.
Ashine.
It's just an identifier that with the use of flex-config.xml file (you can find it in your $SDK_HOME/frameworks folder) points to mxml-manifest.xml file which contains definitions of classes that you can use by "importing" specific namespace.
It's just a convention. Try actually following the URI, the page doesn't exist!
Namespaces aren't the same as directory structures btw... The actionscript compiler cheats a lot to make it look that way.
The URL is known as a namespace URL. Not all namespaces are directory structures. But, it takes quite a bit more work to create a namespace URL; whereas directory namespaces are almost automatic.
To create a namespace URL, you need to use a library project and add a manifest.xml file.
The documentation is really light on this topic. But, I demo about this in the last episode of The Flex Show's screencast series on creating custom components and in an episode of the Flextras Friday Lunch.
What is the best of displaying environment specific information (Dev, QA etc.) on the view JSP in Spring? I am thinking of reading that information from a properties file. Whats the simplest way of making that properties file data available to the view JSP?
So far looks like I need to use JSTL to read the properties, but I don't want them localized!
I used the Spring 3 themes for this and having a different property file for the themeResolver per environment.
I need to make a call to a web service written in .NET. The application making the call is written in ColdFusion. One of the parameters the web service expects is a DataSet object. I can't instantiate a .NET DataSet object in ColdFusion, how can I pass the web service something it will accept? I have no problem writing the SOAP request in raw XML, I just don't know what the XML for a DataSet object would look like.
All objects that .NET expects are serialized by Axis and are available to you. Unfortunately ColdFusion does not make it easy to get to.
To get to the stubs you must:
Access the WSDL in any way with coldfusion.
Look in the CF app directory for the stubs. They are in a "subs"
directory, organized by WSDL.like:
c:\ColdFusion8\stubs\WS\WS-21028249\com\foo\bar\
Copy everything from "com" on down into a new directory that exists in
the CF class path. or you can make one like:
c:\ColdFusion8\MyStubs\com\foo\bar\
If you created a new directory add it to the class path. and restart CF services.
Use them like any other java object with or CreateObject()
MyObj = CreateObject("java","com.foo.bar.MyObject");
Your dataset object should be in there somewhere in whatever java format Axis decided it should be. Most likely you're going to need to do almost all of this in cfscript
EDIT FOR QUESTIONS
THe SOAP object will define the object structure and Axis will create methods for manipulating it. Take a look at the Java object that axis creates. Remember that you can use CFDUMP to look at the methods and properties.
Now I HAVE seen .NET objects that Axis gets confused by, like the dreaded non-generic collection that turns into a "ArrayOfAnyType". It's important for .NET developers to use Generics in their services so that Axis can define the arrays properly....if they don't then it sucks and you may not be able to work with it in soap.
but have no fear obi-won...there is another way. You can always interact with .NET web services in a XML/RPC kind of style. It's not automatic, its a lot of hand parsing of XML, it sucks, but sometimes it's the only way to do it. You should be able to get some help from .NET by hitting up the .asmx file without the "?wsdl" on the end. If you do that .NET will generate a bunch of documentation and examples of what the calls and the XML look like. In that case, you can just create the XML and pass it over the wire as specified by using cfhttp. Good Luck!
P.S. I should note also that as far as I know there is no way to mix hand rolled XML with the ColdFusion/Apache Axis objects, there is also no way to model your own object for use with CF/Axis...you must use the stubs or nothing
Could you use JSON?
http://json.org/