Showing posts with label jsp. Show all posts

How to Start Embedded HTTP Jersey server during Java Application Startup

Jersey Embedded HTTP Server Example
Sometime back I’ve written a tutorial on How to build RESTful Service with Java using JAX-RS and Jersey. After all detailed steps you have to deploy your project to Tomcat Web Server and you should be all good. With that tutorial you should be able to simply create RESTFul service in ~20 mins. But what about doing it in ~5 mins? Also sometimes it’s very difficult to setup Tomcat for your web application.
There are number of reasons:
  1. You may need to have Tomcat binaries copy over to development environment.
  2. If you don’t have all setup correctly configured then your Tomcat application may not work.
  3. You must have to export your project as .war project
  4. What if you don’t want to deploy your application on Tomcat and run simple .jar?
  5. With .jar project – how will you start http server? Will your application accepts incoming REST call?

Extra: How to add CORS Filter to your Jersey Web Application?

You must add Cross-origin resource sharing to your Web Jersey Application. Please follow this tutorial for the same: http://crunfy.me/1DZIui5
Well, there is a simple solution. There is a way to create a very basic HTTP server (supporting only GET/POST) in Java using just the Java SE API, without writing code to manually parse HTTP requests and manually format HTTP responses
Using com.sun.net.httpserver.HttpServer, we should be able to achieve all above.

Let’s get started:

Step 1:

Create new Java project “CrunchifyJerseyEmbeddedHTTPServer“.
Java-Project-CrunchifyJerseyEmbeddedHTTPServer

Step 2:

Convert it to Maven Project. This will simplify our life for adding dependencies. Please follow this tutorial to convert existing Java Project to Maven Project.

Step 3:

In this project we just need to one jersey-server dependency.
Jersey Server - Maven Dependency

Step 4:

Create JerseyEmbeddedHTTPServerCrunchify.java under “com.crunchify.tutorial” package.

Step 5:

Create your REST API. Create java class CrunchifyAPI.java under same package “com.crunchify.tutorial

Step 6:

And that’s it. Now right click on JerseyEmbeddedHTTPServerCrunchify.java and “Run it as Java Application“.

Step 7:

Validate Result. Visit this URL: http://localhost:8085/api and you should see result on browser.
Jersey HTTP Server Browser Response
Hope you enjoy this tutorial. Need to share any comment? I’m here to listen. Thanks.
Have anything to add to this article? Please chime in and join the conversion.

How to use AJAX, jQuery in Spring Web MVC (.jsp) – Example

Recently I’ve to use jQuery, AJAX in Spring MVC Java example. In .jsp (View) I wanted to update specific field every 3 second. Let me share this simple example.
This example will help you if you have any one of below queries:
  • Spring Framework + jQuery AJAX Request Example
  • Spring MVC 3 and jQuery Integration Tutorial
  • How to get a new value from a Spring Controller using Ajax every n second?
  • Spring 3 MVC, Ajax and jQuery Magic Tutorials
  • How to use Ajax with Spring MVC 3 using Annotations and JQuery?
We will do a simple web application which will show Random Number with Current Time every 3 seconds.

Let’s get started.

Step-1

Pre-Requisite: http://crunchify.com/hello-world-example-spring-mvc-3-2-1/ (Deploy this project successfully on Tomcat)

Step-2

Create 2 new files. ajax.jsp and CrunchifySpringAjaxJQuery.java

Step-3

Make sure you have below package structure.
Crunchify Spring MVC tutorial with Ajax and JSP

Step-4

Open Web Browser and visit this URL: http://localhost:8080/CrunchifySpringMVCTutorial/ajax.html
Another must read: http://crunchify.com/how-to-write-json-object-to-file-in-java/

Step-5

Checkout your Result..
Checkout Ajax and JSP result on browser

Step-6

Let’s do some debugging. You should be able to see below debugging statements in Eclipse Console to verify that your AJAX call is working and hitting Controller…
Spring MVC - AJAX and JSP console output
And you are all set.. Cheers.. And as usual let me know for any query..
Have anything to add to this article? Please chime in and join the conversion.