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.
Related
JSP page in its lifecycle translated into .java file, but is JSP page itself a java class?
Confused and need help.
JSPs are compiled into Java Servlets, and Java servlets are classes. So yes, the JSP is compiled to a Java class. The name is usually automatically generated, and is visible in any stack traces (if you throw an Exception).
From here:
A JavaServer Pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets.
And from here:
Java Servlet technology provides Web developers with a simple, consistent mechanism for extending the functionality of a Web server and for accessing existing business systems. Servlets are server-side Java EE components that generate responses (typically HTML pages) to requests (typically HTTP requests) from clients. A servlet can almost be thought of as an applet that runs on the server side
Since applets are classes, hence, a JSP is a class.
The intend behind JSP is Actually makes easier for JAVA developer + front end Developer to
write not only Java code also need to write HTML code and other which
is easily stuff with html code too for full fill the task complete.
so, let's say, my jsp page is home.jsp, then it's translating from .jsp to .java by home_jsp.java which will enclosed all non-java code into java.
and finally home_jsp.class file generate which will run on JVM.
JSP page life cycle and many of the capabilities of JSP pages (in
particular the dynamic aspects) are determined by Java Servlet
technology.
Now, i hope you are little bit clear on your query that was,
JSP page in its lifecycle translated into .java file, but is JSP page itself a java class?
Best of luck...!!!
A JSP pages compiler is a program that parses JSPs, and transforms them into executable Java Servlets and that java Servlets is a simple java class.
I am developing an application using Java and Spring MVC. As usual, stored one JSP file in /WEB-INF/view/ folder which is working as the View for all the requests.
Normally we have this JSP hard-coded that also has some codes to process the Model (tags and EL). Things are working fine till this point.
Now instead of hard-coding the JSP, I want to populate this JSP file dynamically from the database. So the user can upload and select different templates/themes/layouts to display his pages.
Here is the code to explain what I am trying to do (I know this is not the way but for illustration purpose only).
/WEB-INF/views/index.jsp
<%# page import="com.example.domain.Template" %>
<%# page import="com.example.dao.TemplateStore" %>
<!-- Following code is supposed to return complete JSP template from the database as uploaded by the user. -->
<%= TemplateStore.getUserTemplate("userTemplate") %>
I searched web for this topic but could not find anything.
Any help on how to accomplish this would be highly appreciated.
Thanks in advance.
IMPORTANT: I have asked this question a few days ago but marked as "off-the-topic" by some members. I am yet to understand how this question is off-the-topic - https://stackoverflow.com/questions/18026092/creating-content-of-jsp-views-in-web-inf-views-dynamically-from-the-database.
If view templates are to be dynamically fetched from a database, you shouldn't think of JSP. JSPs are compiled into servlet classes and there's little support for doing that other than the standard way (static files somewhere under your webapp root).
Therefore, just consider switching the view technology (at least for the dynamic part) to some general-purpose templating library like Velocity or Freemarker. This comes with a security bonus, since there's less one can break from within such a template than from within JSP code.
You could even support multiple view technologies (perhaps anything that Spring MVC supports out-of-the-box, except for JSP) and allow your users to choose the type of template when uploading.
Then you can write a custom view resolver which would delegate to the appropriate standard resolver (Velocity, Freemarker, XSLT, whatever...) with the user-selected template.
However, if JSP is a hard requirement, I guess one ugly workaround for JSP (which should work in any servlet container) could be to fetch content from the DB and create an actual file (like WEB-INF/templates/${primarky-key}.jsp) under your exploded webapp root, then RequestDispatcher.forward() to it.
You might or might not be able to do this with JSP, but you can certainly compile Java code in memory and then call it.
http://www.java2s.com/Code/Java/JDK-6/CompilingfromMemory.htm
Of course going from JSP to Servlet would just be another step.
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 use Java mostly for GUI programming and PHP for web programming but I really don't like not having a statically typed language. For my next project I would like to use Java instead of PHP. What is the difference between JSP and servlets?
JSP basically allows you to write your Java code around HTML, superficially seeming like PHP or ASP. The result is just compiled to a servlet though.
Under the covers JSP and Servlets are esentially the same, both compile to servlets and both execute as Java code. The difference between them is in authoring and usage. You author Servlets as Java code, i.e. you write a Java class that derives from HttpServlet and override the appropriate methods. JSPs on the other hand are authored using a template based language, this looks a lot like HTML with code snippets thrown in, similar to many other template based languages out there.
If you are building a web application in Java it is considered (very) good practice to use an MVC style architecture with Servlets as the controller and JSPs providing the view (and POJOs as the model)
I would really recommend reading through the first few sections of the Java EE 5 Tutorial. It really does a good job explaining the different Java technologies.
In short, servlets are an abstraction of an HTTP server that allow you to implement logic based on the HTTP request coming in.
JSP is more on the view side of things, allowing to mix in code with your html view and you'll find it similar to PHP or classic ASP.
You can use servlets without JSP and you can use JSP without servlets (kinda, they're still used in the background), but most often you'll want to use a good MVC controller with the Servlet filling the controller role, and the JSP filling the view role.
If you use mvc JSP would be the view, while the servlet would be the controller.
Althought JSP can contain java code, the lesser the better.
To compare it to PHP world,Zend Framework, JSP == .phtml and serlet == .php.
Servlets are classes that you can use to process any kind of request, but mostly HTTP requests. You write servlets by writing classes that extends javax.servlet.http.HttpServlet
JSP is a newer technology than servlets. It is used to combine HTML code with Java Code. At the end of the day, a JSP page is used to generate (automatically) a HttpServlet.
Usually what people do is, write the business logic portion of the WebSite on servlets, and then, forwarding control to a JSP page (similar to what's accomplished with MVC).
But, nowadays, a lot of people would use a framework like JSF or Spring on top of Servlet+JSP technology. So you might want to take a look at one of those frameworks as well.
Servlets are in java and allow http responses to programmed using Java strings. They are most useful for computation work.
Jsps as mostly html with small snippets of Java code, this is much more like PHP and is more useful for website
P.s. Have a look a google app engine, it's great for hosting basic Java apps.
JSP follow the MVC model.
The main difference between jsp and php at run time..
1.When a jsp page call's first time it converted as servlet class and than the servlet class is called every time ,it makes the jsp faster then php.
2.you can use the bean(Simple java classes) in the jsp page for business logic implementation.And make out put for that in jsp pages ,like a simple static html page.
There are more feature with jsp....