how to use HTML in servlets? - java

I'm interested in the front controller servlet:
https://martinfowler.com/eaaCatalog/frontController.html
I'd like to stick to servlet as much as possible, but am not terribly keen on using a multitude of system.out commands to print html.
How would I use "fragments" of html, stored literally as foo.html and bar.html, for different servlets to mix-and-match.
Yes, this a poorly researched question as it stands. I'll add research too it, only looking for general information please.

Related

JSP servlets and Threads or Java Applet

Basically there is a php based portal. I was asked to change one of the pages that is doing backward validation on xml files to java. My question is:
Should I use Java Applet or JSP servlets?
What are pros and cons of each one?
A lot of people will use it at same time. Does it mean that with JSP I will have do something with multi-threading?
NOTE that I am not reading or saving anything to server database etc. It is just all about compering two xml files and do it as fast as it is possible.

how to give dynamic nature to my jsp pages?

Question is pretty lame I know. And the answer is pretty simple - AJAX (I think, I'm not sure).
But before proceeding further, I would like to know if there's a better way.
My problem:
I have a web application, that takes in one statement from the user, and then in response to it,sends back another statement. The whole process goes on like this.
So how can I give a dynamic nature to my JSP then? If I keep reloading the JSP, my old responses will get deleted. So, that's not how it works.
I could use RequestDispatcher 's include() method. I find that as one solution. So, I can keep including the new pages onto my old JSPs. But again, I think this will have a limitation. Haven't tested it yet though.
Is there a better way around?
Thanks for your help. :-)
I'm in love with AJAX consuming Restful Webservices so that is what I would suggest. In addition, if I understand you correctly, I don't think include() will help you in this scenario unless you manage to keep streamming your responses in 1 open connection which isn't really ideal in most Web page scenarios.

Using plain JSP tags in Play

My apologies if this is a stupid question, however I can't find any information about this.
I would like to use a JSP tag (not play 'tags') in my application views - specifically, the Joda Time JSP tags for formatting purposes.
I can't work out how to call these tags - in JSP I just had to import the taglib and off I went.
<%#taglib prefix="joda" uri="http://www.joda.org/joda/time/tags" %>
Have I missed something simple - how do I do this in Play view definitions? At the moment my taglib calls are rendered as HTML. I understand that Play's view stuff is based on Groovy's - I have tried finding the relevant information there too but haven't been successful.
Thanks.
Edit For what it's worth I'm starting to suspect that I'm very much misunderstanding the situation here - am I correct in my assumption that the Play templating engine has nothing to do with JSP and is an alternative rather than an extension?
Indeed, you cannot use JSP tags. The Play framework uses (by default, although there are alternative modules that can replace the rendering engine) the Groovy templating engine to create the views. It is these views that are responsible for both the templates and for the majority of tags you will create.
There are also the concepts of FastTags, which may be the easiest way for you to expose the JodaTime functionality that you find in the JSP tag library. A good example of how to build FastTags can be found here...
Can someone explain how to use FastTags
However, if all you are trying to do is to neatly format dates, then you should take a look at the JavaExtensions for date formatting. http://www.playframework.org/documentation/1.2.1/javaextensions#date. To format dates is really simple, your code should be as simple as the following (assuming your date is passed into your view as mydate.
${mydate.format('dd MMMM yyyy hh:mm:ss')}
You can even create your own JavaExtensions if the ones built-in to Play don't offer everything you need.
Finally, It is important to understand that Play is not Java EE. Play has its own stack, its own set of rules and its own set of technologies. It can be difficult at first to separate yourself from the Java EE tools, but you almost certainly find that the Play approach saves you time and effort.
Yes, you are right. Play templates are based upon Groovy. That is why the syntax is altogether very different from jsp tags. And thus, I don't think it is possible to use jsp or replace the groovy system with a Jsp sytem.

What is the best way to screen scrape poorly formed XHTML pages for a java app

I want to be able to grab content from web pages, especially the tags and the content within them. I have tried XQuery and XPath but they don't seem to work for malformed XHTML and REGEX is just a pain.
Is there a better solution. Ideally I would like to be able to ask for all the links and get back an array of URLs, or ask for the text of the links and get back an array of Strings with the text of the links, or ask for all the bold text etc.
Run the XHTML through something like JTidy, which should give you back valid XML.
You may want to look at Watij. I have only used its Ruby cousin, Watir, but with it I was able to load a webpage and request all URLs of the page in exactly the manner you describe.
It was very easy to work with - it literally fires up a webbrowser and gives you back information in nice forms. IE support seemed best, but at least with Watir Firefox was also supported.
I had some problems with JTidy back in the day. I think it was related to tags that weren't closed that made JTidy fail. I don't know if thats fixed now. I ended up using something that was a wrapper around TagSoup, although I don't remember the exact project's name. Theres also HTMLCleaner.
I've used http://htmlparser.sourceforge.net/. It can parse poorly formed html and allows data extraction quite easily.

Concept and programming for accessing XML file that uses XSL file in JSP code

I am a beginner in accessing backend XML files (which act like a database) in JSP code. Can anyone please provide me the links and references that provide good understanding for beginners like me? Please help.
Some tips when working with JSP: Keep as much code as possible outside of the JSP. I've had very good results with creating a helper object at the top of the JSP. In the HTML of the JSP, I can then call the methods of the helper object to get at my data.
This way, I have a normal object (which doesn't depend on the JSP cra....framework) which I can test and use just like any other object.
So my suggestion is to create a couple of objects which allow you to access the database. In the JSP, have as little actual Java code as possible.
You may want to take a look how to implement typical webapp design patterns in J2EE (see e.g. Sun's blueprint describing the webapp designs). Depending on complexity of your application, make a decision which pattern to use. You may also choose to use some of existing MVC frameworks build on J2EE (although I'd not advise that to a beginner).
Building a model classes around your XMLs would be a good start (there is a variety of ways to process XML in Java, check e.g. JAXP). Once a model is ready, you can start using it in your JSPs (implementing the view and controller per the pattern you will choose).

Categories