I have to call a servlet written in Java from Clojure web application, and I don't understand how to do that.
Developing a webapp in Java, I had to describe all mappings in web.xml. In Compojure, I see, I must describe routes. So, can I bind the Java servlet to one such route?
Sorry if my question is stupid, but I've searched a lot and didn't find an answer; I'm just starting to develop for the web.
Two helpful pointers:
There's an example on how to generate the Vaadin servlet completely from Clojure on github
And here's a SO question on how to map a java filter to routes
I suggest, there is only one way to cope with my tasks. I have to manually instantiate Java servlets in Clojure web app and form their request and response parameters. Test package for ring.util.servlet library describes this approach in details, but in case of turning Ring handler into a Java servlet.
In case of deploying Clojure app and Java servlets to servlet container separately there's no need to define additional Compojure routes or Ring handlers to paths mapped by container.
Please tell me whether my suggestions are incorrect.
Related
I am working on SAML SSO Authentication.
I have created a servlet to generate SAML metadata and i deployed it and run it and I got the output.
Same time I have created a java class to generate SAML Metadata with the same code and tried to run it independently. I have added the same Jar files that I have used for that servlet application.
But I got the Exception given below. Can anybody help me to find the difference between running an application independently and by using java servlet??
Thanks in advance.
Exception:
Running as servlet in a web container means all sorts of stuff is on the classpath that is automatically provided by the servlet container.
Running using main() means you have to put all needed stuff on the classpath yourself. The ClassNotFoundException you got should be clear enough in that respect.
(Pls note that although I did say "the" classpath, in a servlet container things are typically not quite that simple. But that's not the point. Also note that running as a servlet, and using features of libraries provided for the container, may even mean your stuff cannot run as an independent java program simply because the library stuff was deliberately intended for servlet container use exclusively.)
I'm working with Redmine for the first time and was able to successfully install it locally. I haven't used Ruby before and I come from a Java background.
I am able to run the application by going to -
http://localhost:3000/projects
The question is, where can I find the html file (if it exists) that corresponds to http://localhost:3000/projects. In Java, we can do this by looking at web.xml or the relevant Spring configuration file and see how the URL is mapped to a servlet or controller. How to do this in Ruby?
The counterpart of web.xml is the routes.rb and the config.rb files in ruby. You'll find them in the config directory. The routes.rb defines which controller and action (much like servlets) will handle a certain request (URL). And since Rails has predefined conventions, all the html files go in the folders named after the controllers in the views directory and by convention the html file with the same name as that of the controller's action that has been invoked will be rendered as the response.
But all of this can be overridden if desired.
This is a nice place to start understanding Rails: http://guides.rubyonrails.org/
Note that Ruby is not the same as Rails.
Ruby, like Java, is a programming language. It appeared in 1995. For example, the following is a script/program you can execute from the command line.
#!/usr/bin/env ruby
puts "Hello World"
Rack is a web server interface for Ruby. It handles the HTTP protocol, and allows one to write web applications in Ruby by making it easy to parse HTTP requests and send HTTP responses.
Rails is a web framework with powerful conventions, patterns, and tools for developing web applications in Ruby. Some part of it uses Rack. It appeared in 2004. Sinatra is an example of another web framework that uses Rack.
What's the equivalent of web.xml in Ruby?
It does not exist.
What's the equivalent of web.xml in Rack?
Probably config.ru.
What's the equivalent of web.xml in Rails?
config/routes.rb and config/application.rb. Please refer to Configuring Rails Applications.
Routes
To figure out which html file corresponds to http://localhost:3000/projects, look in config/routes.rb. If you see
resources :projects
then it is handled by the index action in ProjectsController with the view at app/views/projects/index.*.
Rails follows convention over configuration principle so all views can always be found in
app/views/
and the one you are looking for should be (depending on Redmine template processor)
app/views/projects/index.html.erb
Also a convention is that view files are named like
path/to/view/_action_name_._content_type_._processor_
In Rails you can find all the mappings to the routes of your web app looking at the routes.rb file in the config folder inside your project folder.
For example, if you want to configure the index page in your project, remove the index.html.erb in the public folder and do like:
root :to => "yourController#someAction"
Understanding routes is not an easy task for someone coming from a Java background. But this should help.
I have a project due (actually overdo) for school and cannot get something simple working.
I am running Netbeans 7.2.1, Glassfish 3.1.2.2 and the latest Java JDK.
I have four JSF pages, index.xhtml ItemAdd.xhtml, ItemCatalog.xhtml, and ItemDetail.xhtml and a servlet SessionServlet. I have verified that I can directly reach each of the pages i.e
http://localhost:8080/CMIS440Spitzer-Project2/faces/ItemDetail.xhtml
works. However when I go from one of the JSF pages to my servlet and then forward the request on I get a glassfish 404 error.
Here is my extremely simple servlet code that is doing the forwarding:
if (request.getParameter("target").contains("Add Item")) {
String URL = request.getContextPath() + "/faces/ItemAdd.xhtml";
System.out.print(URL);
RequestDispatcher dispatcher = request.getRequestDispatcher(URL);
dispatcher.forward(request, response);
}
One the console I get the following which I can paste into my browser and it works:
INFO: /CMIS440Spitzer-Project2/faces/ItemAdd.xhtml
Since I get the console message I know the request is reaching the servlet and that it is hitting the right code block and because I get a Glassfish 404 error I know it is forwarding to my machine on the correct port.
What is the issue with the RequestDispatcher? Why is this not working?
Thanks.
<rant>
This encompases more then this issue but I have worked with PHP, perl, C++, C, Java and a few others. NEVER before have I felt that a language (in this case the JSF implementation of Java) was actively trying to make my life as difficult as possible. For example, many needed things in this environment is in the form of maps:
Need a list of all request parameters, you get a map, need a list of all session parameters, you get a map, etc. However in JSF you cannot easily LOOP THROUGH MAPS! Why, because it is slow. I found a link that was basically a Mea Culpa from a Java developer who mentioned it would be O(N) speed so it would be slow. In other word the Java JSF developers pre-optimized my code and require me to convert maps to lists.
At one point in this I was getting some weird error regarding reflection and accessing private methods/properties when now-where in my code was I doing anything of the kind. I was using standard getters/setters to return a map keyset.
Dont even get me started on the naming conventions of getters/setters (uppercase converted to lowercase, add a get/set in front)
Maybe its me but this whole setup seems overly obtuse,hard to understand and unnecessarily complicated.
Thanks for reading my rant. I have been at this WAY longer then I thought I should and have been nearly to tears trying to do simple stuff.
</rant>
Why are you mixing servlets for session management and JSF pages? when working with JSF, you have to stop thinking in terms of the http request/response paradigm, it's an event-driven framework with more in common to desktop GUI programming. Granted, JSF is not the friendliest web framework to work with, but you're making it more complicated than it needs to be.
The path you pass to getRequestDispatcher is relative to the context root.
By adding the context path to it, you're ending up with /CMIS440Spitzer-Project2/CMIS440Spitzer-Project2/faces/ItemAdd.xhtml, which doesn't exist.
(Actually, it's relative to the context root if it starts with a slash, as this one does. If it doesn't start with a slash it's relative to the servlet.)
I have a third party application which has lot of servlets and jsp. I wanted to debug that by putting breakpoints on my local jboss server. How do I know that, for a particular request, the request is being processed by particular java classes and jsp, so that I can put breakpoints in the right files? I am thinking of going through the code, before setting the breakpoints, to know where to put them. But I feel this is not an efficient way to do it (as it is a very big application). Can you please suggest if there is any better way to do this?
Thanks in advance.
The web.xml file contains servlet-mapping elements indicating which servlets are mapped to which URLs. So if you know the URL, you should easily find the corresponding servlet. Now you can read the servlet code to see which other classes are involved.
I think fastest way for debugging applications like this, is profiling application for specific usecase, in this way you can understand which classes used for this scenario and after finding classes, you can debug these classes.
for profiling application there are lots of tools.
commercial: Yourkit, JProfiler, JProbe
open source:VisualVM, Javacalltracer (create run-time sequence diagram)
I need to execute a JSP. But I need to directly from Java, without using Tomcat or any other servlet container. Compiling JSPs would be a good thing too, but not necessary. I think maybe org.apache.jasper package is good for doing this, but I can't find any good example or tutorial online.
I need something as:
Class compiledJSP = compileJSP(new File("helloWorld.jsp"));
String html = executeJSP(compiledJSP, httpServletRequest, httpServletResponse, ...);
html --> "Hello World, John!"
Thanks!
If you need to capture JSP's output as string it's reasonably straightforward (although far from ideal from the design point of view) from within Servlet Container:
1. Extend javax.servlet.HttpServletResponseWrapper and override getWriter() / getOutputStream() methods to provide your own buffered versions (e.g. StringWriter)
2. Invoke "include()" method of RequestDisparcher, wrapping original response in your own.
3. Grab buffer's content.
Now if you need to do the same thing outside Servlet Container, you really need to ask yourself "why?". Perhaps you should be using a template engine (FreeMarker / Velocity / StringTemplate / etc...) instead of JSPs? If your JSPs are well-formed XML files and are not using any java code inserts it may be reasonably trivial to convert them to FreeMarker templates (FreeMarker supports custom JSP tag libraries) which would greatly simplify your task.
Nevertheless, if it's an absolute hard requirement your most straightforward options are:
1. Run an external Servlet Container and let it handle JSP interpretation. Your program would submit HTTP requests to it and capture the output.
2. Same as above, but you can run embedded Servlet Container (e.g. Jetty).
If your JSPs are available at build-time you can precompile them via Jasper as suggested in other answers.
I would strongly advice against trying to implement your own servlet container - you'll be walking into a world of hurt.
You will need a container. A JSP is an abstraction on Servlet. Servlets have a dependency on a life cycle provided by a container.You need a container to provide the life cycle.
This is possible without a servlet container. There are two steps to it.
The first is to generate the source code. If you look at the source code of the jspc ant task it is possible to do it directly in code. Or you could just invoke the ant task.
The code that is generated is just a Servlet and it is possible to invoke the methods on a servlet outside of a container:
Just instantiate it and then call doGet(request, response). I'm not sure exactly what you need this for but your life will be made easier using spring mock objects for the http request and response.
This will populate the Response object. you can then get the output with:
res.getContentAsString();
See an example here:
http://ytreyvus.blogspot.com/2007/03/private-void-cloneplaneffectrelationshi.html
Try MockRunner to execute it. You'll need to compile the JSP first, though. I'm using Maven 2 for this (and the JSP Compiler plugin)