I'm running Eclipse 3.4 java enterprise adition and writing JSP pages with it. It does not appear to support quick fix, for example ArrayList ourList; comes up as an error but there isn't a quick fix option to add the import java.util.ArrayList statement. Is there a way to improve quick fix capabilities, or another set of Eclipse Plugins that provides quick fix for jsp?
I tried the Ctrl+spacebar and it automatically added the import for me. Maybe that's good enough?
MyEclipse is something that you could try for improved JSP editing. I think it's only about $30 for a version with the JSP editing.
Apart from this particular problem (which you could solve by using Eclipse for Java EE which has the WTP integrated), this implies that you're writing raw Java code inside a JSP file. This is considered bad practice. JSP is a view technology wherein you ought to control the flow and output using taglibs (e.g. JSTL) and to access the data using EL. Raw Java code actually belongs in a real Java class, like a Servlet, Filter, Javabean, DAO, Utility, etcetera. Keep the JSP clean from scriptlets. If you ever need to do something which isn't doable using taglibs/EL, then the particular code most likely belongs in a Java class.
Creating an ArrayList ought to be done inside a Servlet class, either directly or indirectly (business class). Use the doGet() to preprocess data for display and use the doPost() to postprocess data after submit. Inside the JSP you can iterate over an ArrayList using the JSTL c:forEach tag.
Good luck.
Related
I am migrating my java web-application project from JSP to thymeleaf.
I want temporarily to have ability to reuse some simple custom JSP tags in thymeleaf pages. It seems not a problem with custom tags defined in old fashion, as java files. I just instantiate tag, set fake PageContext, request and response, attribtes - and call doStartTag / doEndTag.
However I could not find a way how to instantiate object representing JSP 2.0 tag (i.e. defined in a ".tag" file. How can I achieve this?
Thanks in advance for any ideas!
It looks there are two ways:
Use jspc-maven-plugin to get precompiled classes from jsp and tag files - I checked that works - however latest version of this plugin is 2.0-alpha-3 and it is 4 years old now.
Access servlet-container jsp compiler (in most cases jasper) and do the same thing as in first variant, but on demand, in execution time. However, this makes application container-dependent.
(My personal opinion now is that it would better to avoid the idea at whole - all solutions looks too unreliable to use in production)
If I'm using HttpServlet's for my controllers, and I've got my models setup and in a specific package, what about the views? The last thing I want is to dump all of this HTML into my controllers. Where do I put it? What file types?
I'm new to Java :)
Update
If I should be using jsp files, wouldn't having jsp files within my "Web Pages" section make them publicly viewable? Or should they go somewhere else? How do I include them on my page and pass parameters to them?
If you are using servlets (which seems to be the case), your view should go in JSP files. If you are using JSF, you put your view in facelets, but it is not the case since you are using servlets. JSF is the most recent specification, but I bet it is better to start by JSPs and servlets - maybe following the official tutorial.
EDIT: how to dispatch a request from the servlet to a JSP? Just get a RequestDispatcher from the ServletRequest passing the JSP path as parameter:
RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
If the dispatcher is different from null, just call its include() forward() method:
dispatcher.forward(request, response);`
The dispatcher can be null (for example, if the JSP does not exist) so it is a good practice to verify if a proper dispatcher was returned.
jsps or javascript if you are going for a rich internet application (RIA).
You most likely want jsps.
JSPs are for views. So they should be public. JSPs dont expose anything except for html that your output just as you would in PHP. The source does not show unless you have configured your server incorrectly.
Also you can pass objects from servlet to jsp through shared objects as they are in same vm. JSP is servlet reversed so instead of printing HTML from java you embed Java in html which saves you from writing out.print statements....
So servlets are more suited to write actions. JSP for views.
You might also look into spending some time learning JSTL too. It makes your JSPs clean and readable: http://docs.oracle.com/javaee/5/tutorial/doc/bnakc.html
Keep in mind, a user will not be able to see the code in your JSP, the web container actually compiles the JSP file much like the JVM (actually in a very similar fashion) compiles source code. If you are using something like Tomcat, you can look at a compiled JSP in the work directory of your web container. It will look surprisingly like a normal class file with a lot of out.write's in it.
I would like to invoke the JSP parser programmatically. That means that I want to be able, in Java, to 'run' a jsp page and get its output, without sending it back to the client (I actually want to save the output to a file). I don't want to 'forward' the request to the JSP page. I want to be able to do that on several JSP pages in a row.
What is the best way of doing this?
I have found this question, but BalusC doesn't really answer the question directly.
In case you are wondering, I need to do this is because I want to 'precompile' the JSPs for using on other platforms than a Java servlet container.
EDIT
What I need is not the .class file, but the HTML output. Indeed, that will be static once generated but I have some custom jsp tags and I want to leverage the JSP parser to expand them.
I'm not sure that I understand the point of all this.
JSPs are parsed and precompiled to .class files. They're Java servlets at that point. You need a servlet engine to execute them.
If your intent is to capture the generated HTTP response as the "precompiled" response, it would suggest that there's no dynamic content and the response is the same every time you send that particular request. If that's the case, what you've got is static HTML.
If I'm correct, this would seem to be a poor way to generate such a thing.
If your wish is to precompile JSPs to .class files, the problem is that different Java EE app servers use different JSP precompilation engines. You can't precompile JSPs using Tomcat and use them on WebLogic.
The best way to get the html output from a jsp page is to actually deploy it to a real webserver and then call the page and save the rendered output.
If you want to automate some part of this, you might want to look into using a testing tool that exercises through the real interface, such as Selenium or that emulates the browser, such as HttpUnit.
But this is doing much more than just invoking the JSP compiler.
Maybe it would be more practical to use template engines like http://freemarker.sourceforge.net/ or http://velocity.apache.org/
Freemarker even seems to support JSP Taglibs: http://freemarker.sourceforge.net/features.html
Is your JSP dynamically generated. If so then you are stepping into a potential disadvantage situation wherein your JSP will be compiled again and again, leading to performance issues.
However if you could have a single large JSP with all the rules that you need to prepare your display, you could use HttpClient to make a call to your own JSP and that would return the HTML. This would ensure that you are not app-server dependent. If you use JSP Parser you are going to be vendor dependent.
But if your JSP is being dynamically constructed then you should look at options wherein your HTML can be generated on Java side. But if it involved rule based HTML creation, you are better off creating it in Java. You can use Apache Jakarta ECS library for this.
And yes JSPs are not meant for this purpose.
My code in my JSP is something like this:
<tag:loggedin>
logout
</tag:loggedin>
How can I call a class or put scripting code inside tags when I can't use <% scripting elements?
There probably is a reason why you can't use scriptlets (<%).
Maybe the architect on your team has forbidden to use this via a declaration in web.xml, or you are using jspx (jsp documents, aka the XML version of jsp)? The latter technically allows you to insert scriptlets, but it's too awkward in practice to really work.
The answer to your last question is thus a simple: don't.
Do not try to find a way to put scripting code in a JSP, it's a bad practice.
Instead, use a Servlet to put something in the request scope. Then whatever has been put there can be referenced via an EL expression on your JSP page. The Servlet can call any Java class it wants or execute any kind of java code directly.
In most cases it's probably easier to use a web framework like JSF, Struts or Tapestry then to cobble something together yourself using Servlets and JSP pages. For the smallest of web projects the 'old way' might still be interesting to learn a little how the lower level stuff works.
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).