I am working on "migration from JSP to FLEX, and java as back-end. I am novice in java and JSP.
I am stuck at getting values from a java servlet where it takes httprequest and there is a function called forward(request,response) which responds as JSP page with requried values in it.
Now I need to change that and get only data from that servlet and use that in flex.
Problem:
case1: When using httpservice it takes result as string, but unable to get as object.
case2: If I use RemoteObject , it needs method in java servlet to get return value, which is not present in existing servlet.
Can I get any suggestions on this problem.
Thank You
case1: you don't use forward anymore, you set the type of data you want to return, for example:
response.setContentType("application/json");
... and returning data in servlet is done by writing data to a stream like:
PrintWriter out = response.getWriter();
out.print(object);
but it's a while ago since I did this, so there may be some small problem you will face...
case2: if you are using servlet, it's correct to use HTTPService, RemoteObject will not work, it is used differently....
Now, if I can, I would suggest diferent thing to use than servlets - to obtain data from a java server to Flex - I love to use GraniteDS.
I will just state some benefits I see, in case you are interested:
It is easy to setup:
- in java, you will just add a graniteDS library, two config files (granite+services-config xmls) add a granite servlet config to web.xml
- in flex there is also granite library and services-config.xml
When set up, using it is also flowleslly easy - you have a class with a method (or simple bean or ejb) in Java which just return an object of any type! And that's it in Java
In flex, in this case you use RemoteObject which you just call that remote java method and in result handler you get your dataGranite will take care of serializing+transfer+deserializing and just give you the Object (either just dynamic {} or even exact class type)
Related
I have the following servlet code to store a data for a specific reason and later I retrieve the same data in another scenario in a project. It works fine as expected.
// setting
ServletContext context = request.getSession().getServletContext();
context.setAttribute("imageData", data);
// retrieving ...
ServletContext context = request.getSession().getServletContext();
byte[] data = (byte[])context.getAttribute("imageData");
Now, In an another project, where I use plain java program, where I want to store data similar to this servlet logic. I tried using the same code in plan java project, but it throws error, not accepting this servletcontext in plan java file.
Could someone please help, 1.) what is the equivalent in plan java to store like this temporarily or 2.) How to make that servletconext code working in plain java ?
Thank you in advance!
Getsy
How about simple HashMap<String, Object>
You can consider using simply a HashMap in the applet context.
If you need to use the same code in applet and web contexts, add a layer of abstraction and two implementations to manage the attributes depending on the context.
I have a customized JSP tag library with a Java class (extending TagSupport) that generates the output for my web application. It has some parameters that are formed into HTML code using a StringBuilder.
Now the generated HTML is becoming more complex and hard to handle with calls of StringBuilder.append, so I'd like to replace the code generation with a Freemarker template.
I already found out that I could use a generic Struts component tag instead, because the Struts tags already use Freemarker template files, so I could write a tag like:
<s:component template="/components/myStruct.ftl">
<s:param name="myParam" value="%{'myParam'}" />
</s:component>
Then writing the specified template file myStruct.ftl would probably solve my problem. I actually did not try if Struts really finds and uses that file correctly, but I optimistically expect it to work.
My question is, if it's also possible to retain the current code with the customized tag
<my:struct param="myParam" />
and only change the Java class linked to that tag.
I've found code that reads a Freemarker template:
Configuration config = FreemarkerManager.getInstance().getConfiguration(pageContext.getServletContext());
config.setServletContextForTemplateLoading(pageContext.getServletContext(), "/components");
Template templ = config.getTemplate("myStruct.ftl");
templ.process(params, pageContext.getOut());
but it seems very circuitously to me and I wondered what would be the "standard" way to do it. Additionally it seemed that you cannot use tags from the Struts tag library in a template used like this. (I ran into an ArrayIndexOutOfBoundException, caused by Sitemesh... I did not analyze it yet.)
My intention was to keep the Java class as some kind of wrapper around the Struts component tag. Maybe somthing like:
OgnlValueStack stack = TagUtils.getStack(pageContext);
Component c = new Component(stack);
c.addParameter("param", param);
But I don't know how to continue this code stub. It may be crap anyway.
Is there an easy/"standard" way to do this or do I simply have to get rid of the customized tag?
Thanks in advance.
A friend of mine sent me this link:
http://cppoon.wordpress.com/2013/02/27/how-to-create-a-struts-2-component-with-freemarker/
This is what I was looking for. The gist is to change the customized tag to not extend TagSupportbut AbstractUITag which makes it a Struts tag instead of a JSP tag, roughly speaking.
This enables the automatic linkage (by name and path conventions) to my Freemarker template. I basically followed the instructions on that page. I only added the methods that are abstract in the super class, so they had to be implemented.
IMO the site lacks of a description of how the UI bean class is linked to the tag class. But as the IDE forces you to implement the getBean method inside the tag class, you quickly get to this code (using the classes described on that site):
#Override
public Component getBean(OgnlValueStack stack, HttpServletRequest request, HttpServletResponse response)
{
Pagination pagination = new Pagination(stack, request, response);
pagination.setList(list);
return pagination;
}
This might not be completely correct for the recent Struts, but it worked for the ancient version I've got to use.
Thanks again to the guy who sent me the link :)
I'm trying to build a Java REST web service that will do some processing on a get request (eg. send get request with info, do some calculations, then send back an object with the results). Any ideas how I can set this up easily in Netbeans? I've been playing with the New->RESTful web service... feature, but can't seem to get it to return an object.
AFAIK you're supposed to return a string representation of the result. For example implementing the getXml() method:
/**
* Retrieves representation of an instance of services.GenericResource
* #return an instance of java.lang.String
*/
#GET
#Produces("application/xml")
public String getXml() {
return "<entry></entry>";
}
You could use an XML API to turn your objects into XML strings and return them.
What kind of object do you want to return...?
In java rest webservice you can return many kinds of objects like json,xml.
You can follow these tutorials for creating any kind of java rest webservice -
http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/
This link shows example of get request which returns a json object. You can browse there tutorials for any other requirements.
I haven't tried it in Netbeans, but I have done it using Intellij tho, using maven. Just used servlets to get the requests and used GSON to convert the outgoing java object to JSON and send it out.
This is the project I did with some of my colleges.
I have controller with
render(messages);
And i have route
GET / Application.index
I want to implement some rest features, and add this route
GET /api/index Application.index(format:'json')
I have template not found exception. How can i say to play use renderJSON() when format is json without any code changes?
As you use the render() method, Play! will search a template file with the name of the action (detail on Play! website : http://www.playframework.org/documentation/1.2.2/controllers#template).
You have to use renderJSON(params...), it will bypass the default template!
Your use case doesn't really make sense. In the standard render() call, you are likely passing some pojos to the template to use (or none at all), this is a varargs method. In the renderJSON() call you always need to pass an object which can be serialized to json by the Gson library, or a string with is already in json.
Add a new method to your Application class that handles json responses:
# normal index page
GET / Application.index
# api request
GET /api/index Application.indexJson
I'm trying to create a jsp tag file but it fails to compile when I try to use pageContext.getServletConfig().getInitParameter("myInitParam")
I'm using tomcat and when I try to view a page including the file I get a jasper compile error pageContext cannot be resolved. I've also tried just using getInitParameter but it fails also. I can use the request object so I know everything else is fine.
Does anyone know a way to access init parameters set in the web.xml from a jsp tag file, preferably from within a scriptlet?
I just found out the trick is to use one of the implicit objects, in this case config or application depending on the init-parameters scope.
they are list at http://today.java.net/pub/a/today/2003/11/14/tagfiles.html
Have you tried the request rather than the pageContext? Or just off the servlet itself:
getInitParameter("myInitParam");
Are you extending the TagSupport class?
If so, this class has a member named pageContext, the Tag interface declares a method setPageContext(PageContext pc), which the docs state
This method is invoked by the JSP page implementation object prior to doStartTag().
So you should be able to reference this.pageContext fine - unless you are extending a different class?
application.getInitParameter("<Name>");